I'm playing around with ChaiScript, thinking about integrating it as a scripting language into my game engine. (Because it's awesome and quite easy to integrate)
I want to be able to add a bunch of scripts to multiple "game objects".
Each ChaiScript file should just contain a class (with a name that match the filename), defining a constructor and an "update" method.
Something like this:
print("SuperScriptName setup called");
this.counter = 0;
puts("SuperScriptName update ");
Each instance of the "Script" class has an unique ID. After telling Chai to "use" each given file, I make chaiscript create an instance of the class for this particular script
var ScriptInstace*ObjectID here* = *ScriptClassNameHere*();
Then I get the instance back into C++ land as a Boxed_Value, and an std::function that point to the "update" method
ScriptInstance = chai.eval(std::string(scriptInstanceMarker) + std::to_string(ID));
updateScript = chai.eval <std::function<void(chaiscript::Boxed_Value&)>>("update");
For example, this would have been evaluated :
//Create Instance of the script
var ScriptInstance0 = SuperScriptName();
And I will get the Boxed_Value "ScriptInstance0" into C++ land, in the "ScriptInstance" object.
updateScript(ScriptInstace);" in C++ after that works.
Each "Script" constructor does that, and it works as expected on multiple Scripts (yay!), but once 2 scripts or more have been created this way, at each call of the update of the 2nd script (and subsequant scripts), I got this in the debug output (Visual Studio 2015)
Exception thrown at 0x00007FF99C827788 in TestingChaiScript.exe: Microsoft C++ exception: chaiscript::exception::guard_error at memory location 0x00000019347FF498.
I suppose it's because each Script "updateScript" attribute (the std::function used to call the method of the chaiscript class) is the result of the evaluation of the same "update" string.
This execption seems to be caught somewhere inside ChaiScript and don't crash the program, but it's probably there because I'm doing something wrong, I suppose.