Variables...

BlitzMax Forums/BlitzMax Beginners Area/Variables...

Seb67(Posted 2009) [#1]
Hi. I don't understand all about variables. Example:

Goto DefVal

#MainLoop

Print a
WaitMouse()
End

#DefVal

Global a
a=15

Goto MainLoop




When I do this, I'll have a compiling error that says duplicate variable 'a'.
If I remove the "Global a", my program works and displays 15. Why ?


Zeke(Posted 2009) [#2]
first forget Goto command this "goto" is useless and should be removed from bmax .. and second start coding using Strict or SuperStrict


Seb67(Posted 2009) [#3]
I tried to make functions, but when I put the function at the end of the code, I have a message that displays "Identifier 'X' not found. I know in C you need to declare functions in a header file, so the compiler will recognize it. Is there the same thing in BlitzMax or do I need to put the functions at the top of my program ?

The example uses a goto, that doesn't mean I use this command very often (I use it sometimes), but was only to understand how the variable works.


Brucey(Posted 2009) [#4]
Hi, I suggest you look at this.
It links to a tutorial on BlitzMax, which is very good. Hopefully it will help you get an understanding of how things like Variables work in BlitzMax.

:-)


Zeke(Posted 2009) [#5]
first declare all global variables/const.. then it works... like

Global a:Int = 10


Function testme()
	Print a
End Function


then later you can set variable to what you want

Global a:Int = 10


Function testme()
	Print a
End Function

Print a

a = 40

testme



Seb67(Posted 2009) [#6]
Thanks, I'll declare my global at the top of my program, and it seems to work. Sorry, I knew BlitzBasic on Amiga, learned C and now I'm on BlitzMax, and I'm a little bit confused.


Czar Flavius(Posted 2009) [#7]
When you don't use Strict mode, it will automatically declare a variable as soon as it sees one used. So in the first code sample, when it comes across "Print a" it then silently creates a new "a" variable (with default value 0).. then later on it comes across the "Global a" but goes, hang on a minute, I already have a variable named a!

Using Strict mode, the code would not work because you cannot use a variable without explicitly declaring it first, and gotos are not allowed (they are dangerous and 99% of the time not needed). Strict takes a bit more work to get your code going, but believe me the time saved from not having to debug silly errors more than makes up for this!

Avoid using #labels - organise your code into seperate source files instead. An IDE like Blide will make this much easier for you.


Seb67(Posted 2009) [#8]
I rearranged my program. Now I declare my globals at the top, and I'm working with functions. Thanks for your help.


Czar Flavius(Posted 2009) [#9]
That's an improvement, but in the long-run think about avoiding globals to store run-of-the-mill game data and investigate Types and Methods - it's a much more powerful way to code.


lesslucid(Posted 2009) [#10]
I see this "avoid globals" advice quite a bit - does it apply to objects as well? Say you're making a "space invaders" style game - I can understand why you wouldn't want to have global values for the x and y position of the spaceship - you'd make a TSpaceShip type and stick the x and y variables inside of that - but should that spaceship itself by a Global? Is there some way to avoid making it a global?


Gabriel(Posted 2009) [#11]
Say you're making a "space invaders" style game - I can understand why you wouldn't want to have global values for the x and y position of the spaceship - you'd make a TSpaceShip type and stick the x and y variables inside of that - but should that spaceship itself by a Global? Is there some way to avoid making it a global?

It might belong to another object (for example a player object) or if there are more than one, it might go in a TList or TMap which themselves might either be Global or might be declared within the Type of the Ship. If it's the only human controlled ship and there aren't multiple players, there's no reason it shouldn't be a Global.


Czar Flavius(Posted 2009) [#12]
I tend to have a "TGame" object which contains these things as fields, and has the game loop and things in its methods. You can make this global. There's no right or wrong answer, it's just that overuse of globals quickly leads to a sticky situation hence the advice :P