Advertisement

C'tor call problem using ScriptBuilder

Started by May 21, 2009 06:17 AM
1 comment, last by Friggle 15 years, 6 months ago
Hi, I seem to have found some bug/problem in the order of variable initialization when using ScriptBuilder (#include directive). Below is an example: File 'Position.as' contains the following.

uint16 COORDINATE_DEFAULT = 15000;

class Position
{
    // Default constructor
    Position()
    {
        mX = uint16(15000);
        mY = uint16(COORDINATE_DEFAULT);
    }
    
    // The position variables
    uint16 mX;
    uint16 mY;
}


File 'CtorTest.as' contains the following:

#include "Position.as"

Position globalPos;

void main()
{
    Position localPos;
    
    Trace("Global pos = " + globalPos.mX + ", " + globalPos.mY + "\n");
    Trace("Local pos = " + localPos.mX + ", " + localPos.mY + "\n");
}


Running the script 'CtorTest.as' I get the following text reported by (my) Trace function: ---------------------------------------- Global pos = 15000, 0 Local pos = 15000, 15000 ---------------------------------------- To me this indicates that the variable 'COORDINATE_DEFAULT' was not already initialized during the constructor call for the global object 'globalPos'. However, when constructing the local object 'localPos' everything is fine. This problem only occurs when the variable COORDINATE_DEFAULT is located in the included file. If it is declared in the main file everything works fine for the global and for the local object. I experienced the problem on AngelScript version 2.16.1 (May 9th, 2009) Any thoughts on this? Thanks and regards [Edited by - Friggle on May 21, 2009 3:20:03 PM]
This is not a bug in the CScriptBuilder. It is an unforeseen situation in the script compiler.

The script compiler initializes the global variables in the order they are declared, except if a variable references another variable declared after it. This way it normally doesn't matter in which order the variables are declared. The problem is that the class constructors are compiled after the global variables, so the compiler doesn't know which global variables the constructor references.

I have however made a minor change in the compiler just now to make sure the variables of primitive types are always initialized before any variable of non-primitive type. This way you're always guaranteed to get the correct value when referencing global variables from within script constructors, unless you're accessing another non-primitive variable, in which case you may get a null-pointer exception if the variables aren't initialized in the correct order.

Download revision 413 to pick up the improvements.

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

Advertisement
Thanks very much for the explanations and the modification!

Cheers

This topic is closed to new replies.

Advertisement