Advertisement

Add a dynamic script section

Started by August 26, 2009 06:59 PM
12 comments, last by WitchLord 15 years, 2 months ago
Hey, My question is this: I have a module which is build successfully, I dynamically genereated some script code, I wanna execute those script code without recompile the script module, what should I do? I was using executeString, but it had too many limitations: no parameters, no return values. Thanks.
Version 2.18.0 will hopefully allow you to dynamically build new functions for already compiled modules. This will allow you to implement your own ExecuteString without the current limitations.

Until then it is not really possible to do what you need.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
nice future.
:)
for now, I may need to hack your executeString function.
int r = builder.BuildString(str.AddressOf(), (asCContext*)exec);

with this statement inside executeString(), does that means the new compile code will be released after builder is out of scope?

r = ((asCContext*)exec)->PrepareSpecial(asFUNC_STRING, mod);

with asCContext and PrepareSepcial, I still can set the arguements and get the return value, correct?

Cheers
Quote: Original post by WitchLord
Version 2.18.0 will hopefully allow you to dynamically build new functions for already compiled modules. This will allow you to implement your own ExecuteString without the current limitations.

Until then it is not really possible to do what you need.


No, the script code is not released when the builder goes out of scope. Only the next time ExecuteString is called (or the module is released).

If you call ExecuteString with your own context and the flags asEXECSTRING_USE_MY_CONTEXT | asEXECSTRING_ONLY_PREPARE, then the function will only compile the function and prepare the context but not execute it. This will let you set any parameters for the function you want just like any other script function call. Of course, you still need to change the code that ExecuteString wraps your script in so that the built function is prepared to receive arguments.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Correct me if I am wrong.

if every time I use the different name for wrapper function:

str = "void ExecuteString(){\n" + str + "\n;}"; // use differen function name.int r = builder.BuildString(str.AddressOf(), (asCContext*)exec);


the compile code will stay inside the script module, I only need to hold r value, and I can use r value to prepare on different context isn't it? Do I need to use PrepareSpecial() or prepare() is enough?

Cheers

Quote: Original post by WitchLord
No, the script code is not released when the builder goes out of scope. Only the next time ExecuteString is called (or the module is released).

If you call ExecuteString with your own context and the flags asEXECSTRING_USE_MY_CONTEXT | asEXECSTRING_ONLY_PREPARE, then the function will only compile the function and prepare the context but not execute it. This will let you set any parameters for the function you want just like any other script function call. Of course, you still need to change the code that ExecuteString wraps your script in so that the built function is prepared to receive arguments.


Not quite that simple. If you look into the BuildString you'll see that the r value that it returns is just a success or failure, not the function id. The function id is fixed to asFUNC_STRING, regardless of the function name.

I lied before, the function compiled by ExecuteString is not stored in the module but rather in the context. So when the context is released the function will be released as well, or when the context is used for another ExecuteString call.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
:)

I got it. Thanks




Quote: Original post by WitchLord
Not quite that simple. If you look into the BuildString you'll see that the r value that it returns is just a success or failure, not the function id. The function id is fixed to asFUNC_STRING, regardless of the function name.

I lied before, the function compiled by ExecuteString is not stored in the module but rather in the context. So when the context is released the function will be released as well, or when the context is used for another ExecuteString call.


em.. got an problem.

str = "int ExecuteString(int value){\n" + str + "\n; return 0;}";int r = builder.BuildString(str.AddressOf(), (asCContext*)exec);


The compiled function can not pass in any arguements.

asCScriptFunction *execfunc = asNEW(asCScriptFunction)(engine,module); // line 216 as_builder.cpp.

I found the execfunc does not have parameter list, how do I let engine to pass the function and add parameter list in? is this simple? will "ParseFunctionDeclaration" function do it?

Cheers

Quote: Original post by WitchLord
Not quite that simple. If you look into the BuildString you'll see that the r value that it returns is just a success or failure, not the function id. The function id is fixed to asFUNC_STRING, regardless of the function name.

I lied before, the function compiled by ExecuteString is not stored in the module but rather in the context. So when the context is released the function will be released as well, or when the context is used for another ExecuteString call.


ParseFunctionDeclaration(0, string, execfunc, false);


I try this: string is definitin or decl, but both of them are not working, it seems like break engine internal state.


Thanks
You don't need ParseFunctionDeclaration. You just do the following:

ctx = engine->CreateScriptContext();engine->ModifiedExecuteString(module, string, &ctx, asEXECSTRING_USE_MY_CONTEXT | asEXECSTRING_ONLY_PREPARE);ctx->SetArgDWord(0, value);ctx->Execute();ret = ctx->GetReturnDWord();ctx->Release();


Where ModifiedExecuteString is the version of ExecuteString that wraps the string in str = "int ExecuteString(int value){\n" + str + "\n; return 0;}";

Of course, I haven't tested this, so there may be some other things that needs to be modified as well to get it to work.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement