### Author Topic: problem with local variables  (Read 5543 times)

#### bricky91

• Mc. Print
• Posts: 47
##### problem with local variables
« on: 2008-Mar-24 »
hi, i'm making a game with many mini-games... so i use a sub for each minigame... i have a variable  called game, and i assign a minigame to its values... so i wrote:
Code: (glbasic) [Select]
select game
case 1
gosub minigame1
case 2
gosub minigame2

...
endselect
in every minigame i must have variables, so i defined them local; i have a variable called phase that has values 0,1 or 2:
i wrote in sub minigame 1:
Code: (glbasic) [Select]
if phase=0
LOCAL score; score=0
LOCAL time; time=2000
...
phase=1
endif

if phase=1
//show the instructions(here is the code to show the instructions)
if key(28)
phase=2
endif
endif

if phase=2
//play the game(and here's is all the code of the real minigame)
if time<0 then phase=0 //go back to the loading phase and to the instructions if there is no more time
endif
but the LOCAL variables don't work,.. the time should be 2000 at the beginning of the game, and it is 0. so it returns to the instructions... why does it happen? if i define the variables as global variables then it works ok... but if i define them LOCAL the game doesn't work...

#### AndyH

• Dr. Type
• Posts: 383
##### problem with local variables
« Reply #1 on: 2008-Mar-24 »
You've declared the score and time to be local in the IF phase=0 block.  They'll probaly cease to exist after the ENDIF.  I've not ever tried that, so I could be wrong, but based upon GLB converts the BASIC to C that would be my guess.  It's fairly common in most languages of that type (c, c#, etc)

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10780
• here on my island the sea says 'hello'
##### problem with local variables
« Reply #2 on: 2008-Mar-25 »
Exactly. And outside of the IF block, a GLOBAL variable will be created. In debug mode you get a message for this.

#### bricky91

• Mc. Print
• Posts: 47
##### problem with local variables
« Reply #3 on: 2008-Mar-25 »
oh, okay. but i tried to take them outside the IF block, and to assign them the start value in the first IF block(phase=0)... but it doens't work... so how should i do it? must i use global variables??

#### AndyH

• Dr. Type
• Posts: 383
##### problem with local variables
« Reply #4 on: 2008-Mar-25 »
GLOBAL if you want to access the variable anywhere in your code.  Generally not a good idea to rely on globals too much, but they can be useful and you'll probably discover the best time to use them with experience.  Use LOCAL to declare variables that you wish have limited scope, as they'll reduce memory usage and can reduce the number of runtime errors you create for yourself as you'll have local blocks of functiontality.  Generally declare your locals in functions and remember to pass the local variables to other functions as required.  Remember locals will not exist outside of your functions or scoped blocks.

#### bricky91

• Mc. Print
• Posts: 47
##### problem with local variables
« Reply #5 on: 2008-Mar-25 »
ok, but why my LOCALs variables don't work if i declare them outside the IF blocks? should i use functions and not subs? or it doesn't matter? i can't understand why if i declare them outside the IF blocks, and then i assign them a value in the IF block(when phase=0), when i go to another IF block they havent the value i assigned... example:

Code: (glbasic) [Select]
LOCAL time
LOCAL score

IF phase=0
time=2000
score=0
phase=1
ENDIF

IF phase=1
//show instructions
if key(enter)
phase=2
ENDIF
ENDIF

IF phase=2
time=time-1
//code for the game
ENDIF
what's wrong this time?

#### AndyH

• Dr. Type
• Posts: 383
##### problem with local variables
« Reply #6 on: 2008-Mar-25 »
Not able to run it here at work, but first the variable phase is not declared so GLB will make that GLOBAL.  I'm assuming that code is as-is and not in a function?  Perhaps LOCAL scope in main program is not working?

Does it work if you do this?

Code: (glbasic) [Select]
test()

FUNCTION test:
LOCAL phase
LOCAL time
LOCAL score

IF phase=0
time=2000
score=0
phase=1
ENDIF

IF phase=1
//show instructions
if key(enter)
phase=2
ENDIF
ENDIF

IF phase=2
time=time-1
//code for the game
ENDIF
ENDFUNCTION

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10780
• here on my island the sea says 'hello'
##### problem with local variables
« Reply #7 on: 2008-Mar-25 »
It's working. You might have a design flaw here. Please insert some PRINT messages to show what you have and what you expect, please.

#### bricky91

• Mc. Print
• Posts: 47
##### problem with local variables
« Reply #8 on: 2008-Mar-25 »
the code is in a sub, i tried to put it in a function but i didn't solve anything... i tried to write only this code in a new file, to test it out of my game, but it doesn't work... i added "PRINT time, 100, 100" in the IF phase=2 block, to check if the values were right, but the time goes to 0 when the block starts, and it decrease the time value of 1, then it stops...  so the time value stays to -1... this is strange too... the value should be decreased of 1 over and over again... but it stops... the variable phase is GLOBAL, but if i set it as LOCAL when i release the enter button it returns to phase=1... i used a variable called press to check if the enter key is pressed, if it is pressed the variable has the value 1... this is the code i used:
Code: (glbasic) [Select]
if key(enter) and press=0
phase=2
press=1
ENDIF
so when i press enter the mini-game should start, and should go on without holding the key down... and it shouldn't return to the instructions, right? i don't know why, it doesn't work... it's like the variables assume the value 0 in every new IF block, even if the are delared outside the IF blocks...

#### AndyH

• Dr. Type
• Posts: 383
##### problem with local variables
« Reply #9 on: 2008-Mar-25 »
I've not had any problems with local or global variables, and as Gernot says the Local scope in the main program (outside of any functions / subs) is working ok so the next place to check is your logic.

Have you tried single stepping through every line using the debugger, to see if the program flow is moving as you expect it to.  You can also watch the content of global and local variables.  You can use the debug command to print out data (eg: in types) at various stages of your program execution.

#### bricky91

• Mc. Print
• Posts: 47
##### problem with local variables
« Reply #10 on: 2008-Mar-25 »
ow, now it works!! the function was called in a WHILE loop, so every time the variable were declared at the beginning of the function!! i solved the problem with a GLOBAL variable called declared: i wrote
Code: (glbasic) [Select]
if declared=0
LOCAL time
LOCAL score
...
declared=1
ENDIF
so now the variables are declared only 1 time... thanks for the help, the single step mode helped a lot! i feel so stupid now ... thanks again, bye!

#### Kitty Hello

• code monkey
• Prof. Inline
• Posts: 10780
• here on my island the sea says 'hello'
##### problem with local variables
« Reply #11 on: 2008-Mar-25 »
Though about somehting like that. Glad you found it out yourself - gets burned to ROM-brain this way

#### bricky91

• Mc. Print
• Posts: 47
##### problem with local variables
« Reply #12 on: 2008-Mar-26 »
ow, i didn't solve the problem... i did the same as before... when it come out the IF block, the LOCAL variables don't exists anymore... so i'll use GLOBAL variables... i can't understand how to use LOCALS in my case... if anyone has ideas, please post it thanks

#### AndyH

• Dr. Type
• Posts: 383
##### problem with local variables
« Reply #13 on: 2008-Mar-26 »
This is how I use them:

Code: (glbasic) [Select]
TYPE tGame
score; lives
ENDTYPE
GLOBAL game as tGame
game.score = 0
game.lives = 3

MainGame()

FUNCTION MainGame:
LOCAL localLives, localScore
localLives = game.lives
localScore = game.score
WHILE localScore < 10
IF localLives > 3
DisplayLives( localLives )
IF MOD(localScore,3) = 0 then DEC localLives ,1
IF MOD(localScore,3) = 0 then INC game.lives ,1
ENDIF
INC localScore,1
SHOWSCREEN
WEND
// localLives and localScore will cease to exist from this point on
ENDFUNCTION

FUNCTION DisplayLives: _lives
// localLives and localScore do not exist in here, but we pass the value of localLives over in _lives parameter
// game is global so exists everywhere
PRINT "Lives past via function params="+ _lives, 0,0
PRINT "Lives in game.lives="+ game.lives,0,10
ENDFUNCTION