Local vs local types
BlitzMax Forums/BlitzMax Beginners Area/Local vs local types
| ||
Doh.. The topic should really be Global vs Local.. When I do like this I get the result I'm after: Global a:Int = 1 Function Test() Local b:Int b = a b = 2 EndFunction DebugLog a Test() DebugLog a (Local b don't change the Global a) But doing the same thing with a Type: Type TTest Field x:Int EndType Global a:TTest = New TTest Function Test() Local b:TTest = New TTest b = a b.x = 2 EndFunction a.x = 1 DebugLog a.x Test() DebugLog a.x Why does the Local type change the global one..? |
| ||
In the second example B is a local pointer to a, (as apposed to has the value a in the first example). So in 1) b=A means let b have the value that a has b=2 meand let the variable held in b = 2 This is how it works for all the normal variables (Int, Float, double etc) in 2) b=a means let b point to the same instance of TTest as a does b.x = 2 means let the field pointed to by b.x = 2 This is how it works for anything extended from "Object" (Basicly all Types) b:int = a:int > Let the variable B have the value of a B:type = A:Type > Let the pointer b point to the same place as pointer A This is one of the reasons that you need to write your own copy methods (Edit: Of which Perts "Clone" method is one) |
| ||
because "b" becomes a reference to "a", it is not a copy of a, it simply refers to the same location.Type TTest Field x:Int Method Clone:TTest() Local myClone:TTest = New TTest myClone.x = x Return myClone End Method EndType Global a:TTest = New TTest Function Test() Local b:TTest = New TTest b = a.Clone() b.x = 2 EndFunction a.x = 1 DebugLog a.x Test() DebugLog a.x |
| ||
Just as I thought then.. Thanks for the clone tip/example. |
| ||
In your second function, the new instance of TTest you create for b is basically deleted right away and replaced with a's TTest. |