Advertisement Jump to content
Sign in to follow this  
Jason Goepel

Global Variables

This topic is 1888 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 Global Variables which the user configures in the UI.  These variables are not declared in the script the use writes, they are to be generated by the application before building the script.  Often, these variables link to objects which already exist in the application.

 

These Global Variables are typically handles (or constant handles) to reference types.  I do not register a factory for these types, because I do not wish for the user to be able to create his own instances of these objects (they would have no meaning unless they were properly linked to the application).

 

I have been creating an independent section for the declarations of these handles.  I add that section to the module with the user's section, and then I build the module without initializing global variables.  I then get a pointer to those handles and set them to point to the correct objects.  The problem is that if the user declares a handle to one of these types and initializes it in the global section, it will be invalid.

// Secret Global Section
const MyLinkedType@ v1;

// User section
const MyLinkedType@ u1 = v1

int main()
{
    const MyLinkedType@ u2 = v1

    if (u1 is v1)
        return 1;
    else if (u2 is v1)
        return 2;  // This is what returns.
    else
        return 0;
}

The user's global variable u1 is initialized to the initial value of v1, which is null.  Then I set the value of v1, so by the time main is called later u2 is properly initialized to point to v1.

 

If I defer global initialization and set the value of v1 before calling "ResetGlobalVars" then v1 gets reset to null during initialization.

 

Does anybody have suggestions as to how I might include these variables in the module so that they are available to the user's script when it is built?

Share this post


Link to post
Share on other sites
Advertisement

With the current version of AngelScript I can think of two ways of solving this.

 

 

1. By having some function for retrieving the existing object from the application, through a lookup. You can then have your 'secret' global variables call this function as part of their initializations.

// Secret Global Section
const MyLinkedType@ v1 = __GetExistingObject('whatever');

// User section
const MyLinkedType@ u1 = v1

int main()
{
    const MyLinkedType@ u2 = v1

    if (u1 is v1)
        return 1;
    else if (u2 is v1)
        return 2;  // This is what returns.
    else
        return 0;
}

2. Register the 'secret' global variables from the application, instead of declaring them as part of the script. You can use dynamic configuration groups to unregister the global variables in case they change.

 

 

 

 

 

 

 

As a future enhancement to better support what you want I can think of 2 solutions:

 

1. Allow resetting global variables selectively. The application could then manually initialize some of the global variables like you currently do, and mark them as finished. Then call ResetGlobalVars to have the script engine initialize the remaining variables.

 

2. Implement the already planned export-import feature. With this you could compile the secret global variables in a separate module and mark them for export. The user written module will then import the global variables already pre-initialized.

Edited by Andreas Jonsson

Share this post


Link to post
Share on other sites

I think I will go with your functional approach.  Registering the variables with the application would allow any script using that engine to access the variables.  In my application, these variables are specific to the script.

 

Both of your suggestions for future enhancements would work well here.  I was thinking that the export-import feature would have been a nice solution.  In fact, the user may change the script, but there is often no need to recompile the "global module".

 

Thanks!

Share this post


Link to post
Share on other sites

If the only reason you chose the functional approach is because of the access, then you may want to know that you can restrict which scripts can use the registered variables through the use of access masks.

 

 

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!