Sign in to follow this  

ExecuteString() doesn't work on multiline scripts

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

The following script fails to run correctly:
cfg.FullScreen = false;
cfg.OpenGL = true;

The error is:
ExecuteString (2, 19) : Error   : Expected expression value
If I place the two commands on one line it works correctly. I'm aware that this isn't exactly how ExecuteString() was meant to be used, but it provides a quick method of executing small scripts. Some more info: "cfg" is a global object of type "Config" with two members, "FullScreen" and "OpenGL" which are both bools. The script is placed in an external file and is used as a configuration file.

Share this post


Link to post
Share on other sites
ExecuteString() should be able to execute more than one statement, even on multiple lines. What happens is that the string you pass to it is wrapped inside a function like so:

script = "void ExecuteString() {" + string + ";}";

Then that script is compiled and executed.

I'll try to verify why your script doesn't work. It seems that it is a bug when there are ;; (the library adds one extra ;) at the end.

Share this post


Link to post
Share on other sites
I ran the following test without problem with 1.9.1 WIP 2 (not yet released). Either you are doing something differently from me, or I've already managed to fix the bug you found without knowing it.


//
// Tests ExecuteString() with multiple lines of code
//
// Test author: Andreas Jonsson
//

#include "angelscript.h"
#include <stdio.h>
#include <stddef.h>

#define TESTNAME "TestExecuteString"

struct Obj
{
bool a;
bool b;
} g_Obj;

class COutStream : public asIOutputStream
{
public:
void AS_CALL Write(const char *text) { printf(text); }
};


bool TestExecuteString()
{
bool fail = false;

asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

engine->RegisterObjectType("Obj", sizeof(Obj), asOBJ_CLASS);
engine->RegisterObjectProperty("Obj", "bool a", offsetof(Obj,a));
engine->RegisterObjectProperty("Obj", "bool b", offsetof(Obj,b));

engine->RegisterGlobalProperty("Obj g_Obj", &g_Obj);

g_Obj.a = false;
g_Obj.b = true;

COutStream out;
engine->ExecuteString(0, "g_Obj.a = true;\n"
"g_Obj.b = false;\n", &out, 0);

engine->Release();

if( !g_Obj.a || g_Obj.b )
{
printf("%s: ExecuteString() didn't execute correctly\n", TESTNAME);
fail = true;
}

// Success
return fail;
}

Share this post


Link to post
Share on other sites
I've made a couple of changes to the config file loader, and the error doesn't occur anymore. I was using some non-standard stuff, so that would probably be the problem (registering an array as an object).

Share this post


Link to post
Share on other sites

This topic is 4866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this