beginner question

BlitzMax Forums/BlitzMax Beginners Area/beginner question

Ashes(Posted 2013) [#1]
I am initializing a variable inside a function

like this: Global CurrentPiece:TPiece=New TPiece


then in another function I use the variable:

CurrentPiece.yPos:+1

I get a compiler error: "Identifier 'CurrentPiece' not found.
yPos has implicit value in type definition.

Why does this happen? If I initialize the varibale outside the function it compiles without errors.


jsp(Posted 2013) [#2]
A global inside a function is the same as a static local, it's not global to the whole program, thus only available in that function.


Ashes(Posted 2013) [#3]
thanks


Hardcoal(Posted 2013) [#4]
another begginers question

If i have a TList
and i want to get the last object on the List. how can i get it?


jsp(Posted 2013) [#5]
SuperStrict
Global MyList:TList=New TList

ListAddLast MyList , "One"
ListAddLast MyList , "Two"
ListAddLast MyList , "Three"

If Not MyList.IsEmpty() Then
Local s:String = String( MyList.last() )
Print s 'prints Three
EndIf


therevills(Posted 2013) [#6]
And another way to populate the list:
SuperStrict

Global myList:TList = New TList

myList.AddLast("One")
myList.AddLast("Two")
myList.AddLast("Three")

If Not myList.IsEmpty() Then
	Local s:String = String(myList.Last())
	Print s
EndIf

This is more in the OO style of coding.


TomToad(Posted 2013) [#7]
Global can be a bit confusing as it acts different ways depending on where it is in the program. A Global at the start of a program acts the way you expect, accessible to any function or routine. A Global defined in a function acts more like a static variable, one which retains its value with multiple function calls as oppose to a local which gets re-initialized with each call.
SuperStrict

For Local i:Int = 1 To 10
	MyFunction()
Next

Function MyFunction()
	Global a:Int = 0 'this will retain it's value over several calls
	Local b:Int = 0 'this will be re-initialized to 0 each call
	
	a :+ 1
	b :+ 1
	
	Print "A = "+a+", B = "+b
End Function


If a Global is used in a Type, then the variable is shared among all instances of the type, can even be accessed without creating an instance, as opposed to a Field which belongs only to that type itself.
SuperStrict

Type THello
	Global a:Int = 10'This will be shared with all instances
	Field B:Int = 5'Each instance will get it's own B
End Type

Local AWorld:THello = New THello
Local BWorld:THello = New THello

'Print the initial values
Print "AWorld.a = "+AWorld.a+", AWorld.b = "+AWorld.b
Print "BWorld.a = "+BWorld.a+", BWorld.b = "+BWorld.b+"~n"

AWorld.a = 50 'Change the variables in AWorld
AWorld.b = 120

'Bworld.a changes because it shares with AWorld.a, but BWorld.b
' retains its own value
Print "AWorld.a = "+AWorld.a+", AWorld.b = "+AWorld.b
Print "BWorld.a = "+BWorld.a+", BWorld.b = "+BWorld.b+"~n"

'THello.a can even be accessed without creating an instance of the type
Print "THello.a = "+THello.a

'This would create an error because Fields need an instance of the type
'Print "THello.b = "+THello.b



UNZ(Posted 2013) [#8]
@global in types
have also a look at http://www.blitzbasic.com/Community/posts.php?topic=100761

@global in functions
I didn't know that! With this you can create encapsuled functions in some very cool ways. That was one of the things I missed in BMax. I once had the homework to write a "tamagotchi" in some scheme dialect in college.
Here it goes in BMax (plus a sum example):



EDIT:
No wait, I just realized that there can only be one instance of that global in a function. That means that for example a second tamagotchi:
local tama2(action:String) = create_tamagotchi()

will be identical with the first. So it is still not the same as local variables accessible by inner functions. Too bad


Ashes(Posted 2013) [#9]
thanks TomToad - very useful, and also adds more clarity to jsp's answer.

@unz
I didn't know you can use nested functions in Bmax... pretty neat.


UNZ(Posted 2013) [#10]
Just wanted to mention that you can define constants inside a function as well