Static variables
BlitzMax Forums/BlitzMax Beginners Area/Static variables
| ||
Does Blitmax support static variables? thanks |
| ||
Depends how you are using the term 'static variables'. You can use Constant variables, function variables and 'type/class' variables. Each could be considered static variables. |
| ||
Sorry I should have been more specific - I meant local variables specifically? |
| ||
That's confused me even more now. You can use local variables. Check under Language / Variables. How do you want to use these variables? You might want to check the Beginner's Guide to Bmax in the tutorials section. |
| ||
Does BMax have static variables? |
| ||
I think what you need to focus on at this point is the variables 'scope' (i.e where the variable can be accessed) a GLOBAL and CONSTANT can be accessed anywhere a Local can be accessed only within it's scope (Function, Type, Method etc) |
| ||
Apologies, I'm getting tangled up in my own confusion. I meant - is there any way to declare a local variable as static (that is next time the variable is encountered its previous value is stored) - but I guess the answer i no and the solution to this is in tonyg's first response. Apologies for the confusion |
| ||
Blimey... THIS IS A LINK >>> Does BMax have static variables? <<< THIS IS A LINK I'm not going to post it again. ;o) |
| ||
Melvin, your first link just looks like you are asking the question again. |
| ||
I meant - is there any way to declare a local variable as static (that is next time the variable is encountered its previous value is stored) - but I guess the answer i no and the solution to this is in tonyg's first response. The answer is actually yes (as Melvin points out).Function staticTest() Global myVar:Int myVar:+1 Print myVar EndFunction staticTest staticTest staticTest staticTest staticTest staticTest |
| ||
FlameDuck's example DOES preserve the value of the variable upon further calls (because it's global), so in that respect it is static. But if Ant is refering to local variables with the same characteristics, then no. I'm surprised, actually, that BM allows the declaration of a global variable within a function like that. Russell |
| ||
I'm surprised, actually, that BM allows the declaration of a global variable within a function like that. It doesn't have global scope, thus is not a global. Try it, it works. So does this:SuperStrict Graphics 32,32 Local terminate:Int = False While Not terminate WaitEvent If EventID() = EVENT_KEYDOWN Global myVar:Int Select EventData() Case KEY_ESCAPE terminate = True Case KEY_SPACE myVar:+1 Print myVar EndSelect EndIf ' Print myVar ' Enable this, and the program won't compile. EndWhile |
| ||
LOL @ this thread...It's not rocket science! |oDPrint Count() Print Count() Print Count() Print i Print Count() Print Count() End Function Count() Global i i :+ 1 Return i End Function |