Problems with Variables, sort of... ;0)
BlitzMax Forums/BlitzMax Beginners Area/Problems with Variables, sort of... ;0)
| ||
Hi all, My game is coming along nicely (thanks for guidance in at least one other thread!) but I notice I have a huge amount of global variables at the top of my code, as I don't know how to use local variables effectively. Say I want to do a quick 'fade' loop (using alpha): If var_coinalpha<1 And var_coinalphadir=0 Then var_coinalpha:+0.02 End If If var_coinalpha>=1 Then var_coinalphadir=1 If var_coinalpha>0 And var_coinalphadir=1 Then var_coinalpha:-0.02 End If If var_coinalpha<=0 Then var_coinalphadir=0 This is a minimal part of code inside my main loop. Where do I define the local variables? If I do them inside the loop, they initialise each iteration and of course, the values from the previous iteration are lost. Would someone just be able to advise me with a simple example, maybe? I've looked through various other code but can't immediately work it out. Cheers! |
| ||
One way to do it is to declare them just outside of the main loop as local and pass them as parameters to functions. my prefered way is to declare them as a fields in types. |
| ||
Written off the bat so who knows if there are typos..Type TFadeThing Field col=255 Field rate=1 Method updatefade() col = col + rate if col > 255 rate = -rate col = 255 elseif col < 0 rate = -rate col = 0 endif end method Method drawthething() ....................... End method End type The advantage of this way is each thing can fade at its own rate, but if you don't want or need that you can change the fields to globals. However, globals stored inside a type are considered more tidy than globals in main code... var_coinalpha is a terrible name for a variabe :) |
| ||
Cheers. Yeah, I've 'over-egged' my variable names I guess, but I'm more of a VB man, so tend to be used to it. ;) |