Problems with States


#1

Hey everyone, I’m new with Chaiscript (started using it some days ago).

I’m trying to create a 2D Platform game and I wanted to embed a script language in my game.
I chose Chaiscript because it looked like it had the best integration with C++.
I had some difficulties with some parts (and there isn’t much docs online) but I always found what I wanted.
Now I’m having difficulties again with States, it seems that saving / loading states does not have any effects.
My script is running in a thread so I stop the thread before saving the state, and then I load the state while the thread is still not running.
I store states in a map :

this->stateDatabase[stateToSave] = this->scrExec->get_state();

And I restore them like this :

this->scrExec->set_state(stateDatabase[stateToApply]);

Then I can start the thread again :

this->scrThread = new std::thread(&ScriptEnv::run, this);

Did I do something wrong ? Tell me if I forgot to add some details required to answer :smile:
Thanks !


#2

Because the problem I currently have is that I can only run a script once, if I run it once again, it will say I can’t redefine variables…

The script does look like this :

var scContinue = true var cX = 0 var cY = 0 var changed = false var message = stream.print("Cursor Position : " + cX.to_string() + " / " + cY.to_string()) var colorDiv = 0.0 while (scContinue) { colorDiv = float(cursor.getX() + cursor.getY()) if (colorDiv > 3000) { colorDiv = 3000 } message.setColor(int(((3000.0 - colorDiv)/3000.0) * 255), int((colorDiv / 3000.0) * 255), 0) if (cursor.getPressed("Right")) { scContinue = false } if (cursor.getX() != cX || cursor.getY() != cY) { cX = cursor.getX() cY = cursor.getY() changed = true } else { changed = false } if (changed) { message.setMessage("Cursor Position : (x:" + cX.to_string() + " / y:" + cY.to_string() + ")") } } stream.print("Cursor Script Ended !")

Here’s what I got in my console : Console Video


#3

Problem resolved using get_locals rather than get_state, maybe it’s not the best way to do it but it works :slight_smile: