Globals

BlitzMax Forums/BlitzMax Beginners Area/Globals

H&K(Posted 2006) [#1]
Ive reposted this from "Globals in functions", because it seems to have got lost amongst the trees.

When are globals created.

IF I declare a global is it "Created" (Ie actual physical space given to it) at compile time or Run time.

If at run time Is the global created when program flow passes it, or when the program "Initialises"

(Cavat)Is a global created and initialised as a field for a type at the first instance of new for that type, or at the first refference to that type

(Its the last one I really like the info for)
Thanks


Dreamora(Posted 2006) [#2]
A variable is created when it is executed. (global, local, field)

On global in type: it is no field at all. It is a type global (fields are instance based, global = static are type based). And it is initialized when the first instance of this type is initialized.


H&K(Posted 2006) [#3]
Ok Thanks dream. Come back question

And it is initialized when the first instance of this type is initialized

If I go
Local Value:int = TType.AGlobal_AnyExist

If no instance of TType exists, Does AGlobal_AnyExist, exist or not.


Byteemoz(Posted 2006) [#4]
The global is bound to the type itself not to an instance. So you can access it either by using the type name itsself or by using any instance variable. It doesn't even matter whether the instance variable has been initialised of if it is Null so these all are valid ways to access "MyGlobal":
Print MyType.MyGlobal
a:MyType = New MyType ; Print a.MyGlobal
b:MyType = Null ; Print b.MyGlobal
(Assuming that your MyType-type contains a global MyGlobal variable...)


H&K(Posted 2006) [#5]
But it does matter as to WHEN it is initailised.
I have

Type AType

Global Logic:LogicforType[] = AType.MakeLogic:LogicForType[]()
etc..
EndType

Is Logic initialized 1)At the begining of the program. 2)When the first Instance of AType is created. or 3)When AType is first refferenced