Sign in to follow this  
Rain Dog

Question with script execution.

Recommended Posts

Rain Dog    136
Hello, Today i have successfully tested my first AS script.
int sum = 0;
char *Script = 
"for(int i = 0; i < 10; i++)\n"
"{\n"
"\tsum += i;\n"
"}";

engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
engine->AddScriptSection("module", "section", Script, ScriptLength, 0);

engine->Build("module", 0);
engine->RegisterGlobalProperty("int sum", &sum);
int r = engine->ExecuteString("module", Script);


What I have need of now is how i can reliably execute the script one statement at a time so that if I want I can pause the execution of the script by pressing a button, or stop the execution altogether. something along the lines of
while(r != 0)
{
    while(b_PauseScript)
        Sleep(100);
    if(b_StopScript)
        break;
    r = execute_next_statement();
}


Share this post


Link to post
Share on other sites
WitchLord    4678
You can use ExecuteStep() to execute a part of the script and then return. Unfortunately, the steps taken by ExecuteStep() is not very well defined at the moment. Currently it only guarantees that it will suspend the execution at least once per loop. I'm working on making it stop after each statement, like it did in version 1.8.x.

You could also call either Suspend() or Abort() on an active context from another thread. However AngelScript's support for multithreading is still under development and may not be that stable.

One note about your sample code. You call Build() and then RegisterGlobalProperty(). Normally you would register the property before, since the compiler wouldn't know about it otherwise.

Share this post


Link to post
Share on other sites
Rain Dog    136
I have a new problem that i am unable to figure out.

this is the code:

ASEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
ASEngine->AddScriptSection("module", "section", Script, ScriptLength, m_lPrevScriptLength);
COutStream out;
int a = ASEngine->Build("module", &out);







these are the errors, and follwing the errors, is the script
section (564, 2) : Error : Unexpected token 'for'
section (564, 20) : Error : Expected identifier
section (564, 27) : Error : Expected identifier

//void main(void)
//{
int sum = 0;
for (int i = 0; i < 10; i++)
{
sum += i;
//m_PlayerX = sum;
//UpdateData(0);
}
//}


EDIT-->

I appear to have corrected the problem by changing the scriptto something along these lines:


int sum1(int a, int b)
{
return a+b;
}

void main()
{
for (int i = 0; i < 10; i++)
{
sum +=sum1(sum,i);
m_PlayerStatus = sum;
//UpdateData(0);
}
}


and using the following code:


ASEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

ASEngine->RegisterGlobalProperty("int sum", &sum);
ASEngine->RegisterGlobalProperty("const int m_PlayerStatus", &m_PlayerStatus);
ASEngine->RegisterGlobalProperty("const float m_PlayerHeading", &m_PlayerHeading);
ASEngine->AddScriptSection(0, "section", Script, strlen(Script), m_lPrevScriptLength);

COutStream out;

int a = ASEngine->Build(0, &out);
ASEngine->CreateContext(&ASCtx);
ASCtx->Prepare(ASEngine->GetFunctionIDByDecl(0, "void main()"));
int r = ASCtx->Execute();


[Edited by - Rain Dog on October 17, 2004 12:21:04 AM]

Share this post


Link to post
Share on other sites
WitchLord    4678
All statements must be located inside functions, this is why you got the errors with unexpected tokens in when compiling the script without the function declaration. Of course, global variables can be declared outside functions.

Note that if you use ExecuteString() the library automatically wraps the code in a function so with ExecuteString() you should pass the statements directly. It isn't possible to declare a function in ExecuteString() but ExecuteString() can call functions previously compiled in the module.

I also noticed that you pass m_lPrevScriptLength to AddScriptSection(). This is probably not what you want to do, what is happening is that are compiler messages have their line numbers offset with this argument. This is why you're getting errors on line 564, instead of 4 as would be more reasonable.

Regards,
Andreas

Share this post


Link to post
Share on other sites

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