Guard_error with multiple classes having the same method name?


#1

Hi,

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:

class SuperScriptName
{
    var counter;
    def SuperScriptName()
    {
        print("SuperScriptName setup called");
        this.counter = 0;
    }

    def update()
    {
        puts("SuperScriptName update ");
        ++this.counter;
        print(this.counter);
    }
}

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<void(chaiscript::Boxed_Value&)> 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.

Calling “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.


#2

You aren’t doing anything wrong, it’s a known issue that was recently brought up by another user.

I need to check into it, so I just created a github issue: https://github.com/ChaiScript/ChaiScript/issues/298

-Jason


#3

Mh. In my case it’s not at shutdown like in the issue on github, but each time I call "update " on a class instance. I tried running the chair.exe interpreter in the vs2015 debugger and this cause exactly the same behavior :

class A
{
def A(){}
def update(){print ("this is a!")}
}

class B
{ 
def B(){}
def update(){print ("this is b!")}
}

var a = A();
var b = B();

//calling this is fine 
a.update()

//calling this print something about a guard error exception bug do not crash
b.update()

Sorry for the formating, I’m on a phone.

So, each time update() is called from 2nd (and subsequent) class/class-instance couple defined above, it gets a message in the debugger output. Still runs fine.

So it may be something different, I have no idea ^^


#4

I updated the issue to include your notes.