Jump to content
  • Advertisement
Sign in to follow this  
Jason Goepel

Global Variable Initialization Context

This topic is 1954 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

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.

Share this post


Link to post
Share on other sites
Advertisement

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());

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!