Use Case: Store class instances in C++


#1

I’m trying to figure out a way to construct script defined classes using C++ and store them for later use (mostly just calling methods)

In my script I have the code:

class test_obj
{
    var gameObject;

    def test_obj()
    {

    }

    def print_test()
    {
	    print("Test Printing!");
    }
}

Now I want to be able to do this in C++

auto t = scriptEngine->eval("test_obj()"); // The Constructor

Is this a valid way to store this class instance within C++? I don’t want to liter the chaiscript environment with a bunch of instances of the same class, so I want to avoid eval entirely after creating an instance.

How can I get attributes and call methods from this class, if I can at all. If not, I’m not sure how I can organize this for my game engine.

Thanks!


#2

I’ve figured out a solution to my use case:

std::string moduleConstructor = "TestObj()";
auto scriptClassInstance = scriptEngine->eval(moduleConstructor); // Can save this value for later
auto caller = scriptEngine->eval<std::function<void(chaiscript::Boxed_Value&)>>("function_name");

// Later in program/in another class method
caller(static_cast<chaiscript::Boxed_Value>(scriptClassInstance));

#3

Excellent that you found a solution. Let me know if you have any other questions and sorry for not responding sooner.

-Jason