Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

Global Variable Initialization Context


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Jason Goepel   Members   -  Reputation: 366

Like
0Likes
Like

Posted 16 May 2013 - 07:25 AM

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.



Sponsor:

#2 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 16 May 2013 - 02:20 PM

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



#3 Jason Goepel   Members   -  Reputation: 366

Like
0Likes
Like

Posted 16 May 2013 - 05:53 PM

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.



#4 saejox   Members   -  Reputation: 714

Like
0Likes
Like

Posted 16 May 2013 - 07:03 PM

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.



#5 Andreas Jonsson   Moderators   -  Reputation: 3197

Like
1Likes
Like

Posted 17 May 2013 - 07:35 PM

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

#6 Jason Goepel   Members   -  Reputation: 366

Like
0Likes
Like

Posted 17 May 2013 - 07:53 PM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS