🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Global Variables

Started by
2 comments, last by WitchLord 10 years, 7 months ago

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?

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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

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!

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

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