Local variables allocation question

Started by
0 comments, last by WitchLord 13 years, 4 months ago
I wanted to know if local variables are allocated or their constructors - if present - are called at the start of script function or when they are used?

Below simple example:
float a = 4.2f;float calc(float a, float b){    Log("Received: " + a + ", " + b + "\n");    if (a == 4.2f)    {	int a = 6;    }        return a * b;}void main(){	Log("Result of calc function: " + calc(5, 5));}

When breakpoint is set at line with "Log" in calc function, I can see that local nested variable a (line: int a = 6) already exists, so we have 2 local variables named "a", before even nested "if" is executed (which btw. is never executed with this condition).

[Edited by - virious on November 30, 2010 7:25:08 AM]
Advertisement
All the necessary memory for local variables is allocated when entering the function. The constructors/factories for object types are only called when the declaration is reached, before this the GetAddressOfVar will return 0. GetAddressOfVar will always return a valid address for primitive types.

Currently AngelScript doesn't give you the scope of each variable, so when enumerating local variables you will see all local variables, whether they are currently in scope or not.

I plan on adding this information for debugging purposes for a future release. So if you need this information you must unfortunately determine this from the source script by yourself at the moment.

Observe that variables in different scopes may share the same location on the stack but only if the type is compatible.

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