From various reading over the last few days, I've come to learn that Global variables, should, where possible, be avoided - I assume mainly down to the memory that they require. However, I'm struggling to get my head around how to use mainly local variables.
I tend to have my programmes set up in the following order:
-General Setup & Variable / Media Declarations
-Main Loop
-Function / Type Declarations
The trouble is, at the moment, most of my code ends up like this:
Whereas, I can't recal a time I've seen this in anyone elses code. :(
Here's the current code I'm working on:
This works as expected, but relies on global variables again. I tried:
and removed the Global declaration, but it doesn't fade in as expected. I'm guessing this is because the local variable is being 'reset' every time the 'Local' line is read.
Could anyone provide me with some general guidelines on how to go about using mainly local variables?
Pwweeaassee! ;)
I tend to have my programmes set up in the following order:
-General Setup & Variable / Media Declarations
-Main Loop
-Function / Type Declarations
The trouble is, at the moment, most of my code ends up like this:
Global mvinylrotate Global introbackrotate:Float Global mvinylscale:Float=0 Global copyrightscale:Float=4 Global introfadeout Global introfadecounter Global introalpha:Float=1 Global yearalpha:Float=0 Global introplaysound Global intromvinylsound Global titlefadein:Float=0 Global titlestaralpha:Float=0 Global titlestarrotate Global titlecreditsticker=800 Global titlescale:Float=1 Global titlescaleflag Global titlerotate:Float=0 Global titlerotateflag Global s1flag Global s2flag Global s3flag Global titlevolume:Float=0 Global gamestate=1
Whereas, I can't recal a time I've seen this in anyone elses code. :(
Here's the current code I'm working on:
' Slider v0.1 (alpha) - By Matt Vinyl 2007 '----------------------------------------------------------- ' General Setup--------------------------------------------- AppTitle="Slider - By Matt Vinyl 2007" Graphics(1024,768) SetMaskColor 255,0,255 SetBlend(ALPHABLEND) SetAlpha 0 SeedRnd MilliSecs() Global GAMEMODE=2 'Global flag for master game flow '0=Titlescreen '1=Start game '2=Main gameplay Global GFX_L1BACK=LoadImage("gfx\back.png") Global FADEIN:Float ' Main Loop------------------------------------------------- Repeat Cls If GAMEMODE=0 End If If GAMEMODE=1 End If If GAMEMODE=2 DRAWGAME() End If Flip Until KeyHit(KEY_ESCAPE) ' Functions------------------------------------------------- Function DRAWGAME() DrawImage GFX_L1BACK,0,0 If FADEIN<1 FADEIN:+0.01 SetAlpha FADEIN End If End Function
This works as expected, but relies on global variables again. I tried:
Function DRAWGAME() DrawImage GFX_L1BACK,0,0 Local FADEIN:Float If FADEIN<1 FADEIN:+0.01 SetAlpha FADEIN End If End Function
and removed the Global declaration, but it doesn't fade in as expected. I'm guessing this is because the local variable is being 'reset' every time the 'Local' line is read.
Could anyone provide me with some general guidelines on how to go about using mainly local variables?
Pwweeaassee! ;)