Advertisement

Global Variable Initialization Context

Started by May 16, 2013 01:25 PM
4 comments, last by Jason Goepel 11 years, 6 months ago

I have several native functions which rely on user data stored in the Context before execution. If I initialize a global variable with one of these functions, I find the Context does not contain any user data. This makes sense, because the function is called when building, which is before I create a context.

Is there any way I can supply user data to the context that is used to call functions that initialize global variables? Alternatively, is there a way I can access the current module during the execution of a native function (similar to asGetActiveContext)?

I'm open to suggestion. My basic design is to have scripts which attach to "objects" in my application. These objects execute a particular function (entry point) in their scripts. The scripts, therefore, run in the "context" of my objects. Native functions called from the script engine need to be aware of that context. For instance, a "string GetName()" global function might return the name of the object owning the script. Currently, I accomplish this by setting user data in the Context immediately before executing, but I don't see a way to set my user data when building a module. I would prefer to avoid defining some sort of global variable in the module which points back to my application object, but I do see that as a possible solution.

Contexts do not have special storage for executing function to access. You should use classes. Associate your C++ objects with script classes then you have what you need.

Get module of the current function. (wrote this from memory might be a syntax error)

asIScriptModule *mod = asGetActiveContext()->GetEngine()->GetModule(asGetActiveContext()->GetCurrentFunction()->GetModuleName());

Advertisement

Contexts do have user data.

http://www.angelcode.com/angelscript/sdk/docs/manual/classas_i_script_context.html#a78e92b53c248534cd278a1498b88680d

It does look like your suggestion to get the module of the current function would work, but it seems expensive to find the module by name every time.

What i mean is you can't add variables to context and make them available to function.

SetUserData is just to associate it some of your own c++ object, just a convenience method.

If you have one module per script file than you should use globals. They are belong to module they are created.

If not use script classes, your c++ class owns a asIScriptObject then you can operate on that.

You'll want to turn off the initialization of the global variables upon build, and then supply your own context when manually initalizing them.

engine->SetEngineProperty(asEP_INIT_GLOBAL_VARS_AFTER_BUILD, false);
....
module->Build(); // now the global variables won't be initialized
 
// Manually initialize the variables after the script has successfully completed the build
asIScriptContext *ctx = engine->CreateContext();
ctx->SetUserData(myUserData);
module->ResetGlobalVars(ctx); // now the global variables will be initialized using the supplied context that has the desired user data
ctx->Release();

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

Perfect. I should have seen those functions in the documentation. Thank you.

This topic is closed to new replies.

Advertisement