Call methods of a script object inside C++


#1

Hi

I couldn’t find an answer to this in the docs/tests/samples, so I came here :slight_smile: If I create an object inside a script, with some methods in it, how can I then grab a reference to that object in my host program and call these methods? I know I can use eval to grab things from a script, I can even use that for getting lambdas, but does it work with objects? Something roughly like this (half-pseudocode):

Script:

class Test
{
    def func(str) { return str; }
}

var t = Test();

C++:

auto obj = script.eval<something_here>("t");
auto result = obj.get_method_or_whatever("func")("some string);
// OR
auto method = script.eval<something_here>("t.func");
auto result = method.invoke_or_whatever("lalala");

Thanks in advance for any help!


#2
// Get object from engine
auto obj = script.eval("t");
// Get typed std::function wrapper of method
auto func = script.eval<std::function<std::string(Boxed_Value &, const std::string &)>>("func");
// Execute function 
auto result = func(obj, "some string");
// result should be of type string and equal to the return value of `func` now

-Jason


#3

Exactly what I needed, thank you!