Advertisement

swapping out the current script

Started by March 22, 2010 01:44 PM
2 comments, last by WitchLord 14 years, 8 months ago
Hi I'm trying to provide a function for the script engine that replaces the current running script with another one. When it is called I'm calling asIScriptModule::RemoveFunction on all the functions and then just loading the new script that they asked for. Everything seems to work fine. The only issue is that I was hoping to be able to keep the same global variables once I switched the script but it doesn't seem to work. In the first script if I declare some global. I get a compile error in the second script if I try to access it. Unless I declare it again in the second script but then it isn't the same variable. Is there a better way to do this? creating module: mModule = mEngine->GetModule("m", asGM_ALWAYS_CREATE); Later this is how I'm loading the code: if( mModule ) { if(mModule->AddScriptSection("", codeStr, codeStr.length())>=0) if(mModule->Build() >= 0) return(true); } return(false); [Edited by - jedjed on March 22, 2010 2:15:34 PM]
When you call Build AngelScript will always discard everything that is currently in the module, and then compile the script added with AddScriptSections as if it was a completely new script.

RemoveFunction and CompileFunction can be used to replace individual functions, but they weren't really designed to be used to replace an entire script.


What you want to do is to enumerate all the global variables in the original script, store away their values, then compile the new script, then enumerate the global variables in the new script and overload their values with the previously stored values. This is quite easily done when the global variables hold simple values, but can get quite complicated if the variables store handles or complex objects.



Regards,
Andreas

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

Is there a section of the docs that talks about this "Dynamic binding between modules" part of asIScriptModule? Could I just use that? Load the new script in a separate module and strip all the functions from the old module and then just bind to the functions in the new module?
That would be the 'import' statements, along with BindAllImportedFunctions. This works similarly to loading a DLL from an application.

It is not the same as recompiling the script on the fly that you want.

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