Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Clarifying the "global" keyword

Author
Message
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 22nd Feb 2012 21:03
Can I ask someone to clarify for me what the "global" keyword does?

I've been using it in my code (at the very top, outside of any functions, to declare certain variables that I use in many places), but I just found that when I try to change the value of variables inside functions, it doesn't work.

Just to check, if I do the following:

global variablename as integer

function example()
variablename = 0
endfunction

Will the GLOBAL variablename be changed to 0? Or do I have to do:

global variablename as integer

function example()
global variablename = 0
endfunction

As that seems to run contrary to other languages I've used (not that this is a problem - I just need to know if this is the case before I start making quite large-scale changes).
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 22nd Feb 2012 22:07
The first way is correct. Once a var has been declared as global it always will be.

KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 22nd Feb 2012 22:38
Ah OK. It's odd. I was having some problems where it appeared that changing the value of a global variable inside a function wasn't changing it. I moved some code around whilst looking for the problem and now it seems to be fixed, so I'm unsure what the issue was.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 23rd Feb 2012 00:05
Perhaps you changed it one one function, but reset it somewhere else? Simple errors like that are annoying and hard to track down sometimes.

My signature is NOT a moderator plaything! Stop changing it!
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 24th Feb 2012 16:50
Did you perhaps declare the global variable outside of the main.agc?

My hovercraft is full of eels
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 25th Feb 2012 11:24
OK, I seem to have this problem again.

So at the top of my code, I have some values, for example:

global currentgametime as float
currentgametime# = 0.0

This is meant to the the total time in seconds that the player has been in-game. So then during the main loop (when in the active gamestate) I do this:

currenttotalgametime# = currenttotalgametime# + frametime#

So far, so good. I have this printing to the screen so I know it's incrementing OK.

Then, when the player dies, and they click on "restart", I have a function called StartNewGame(). This function resets all of the active variables and sorts out initialising the game world for the next playthrough. This function is located below my main loop, some way towards the bottom of my program.

In this StartNewGame() function I have the following statement:

currenttotalgametime# = 0.0

However, the currenttotalgametime value is NOT being reset to zero when I call this function. I've tried setting it to something like 999.0 to be sure. Assignments I make to global variables in this StartNewGame() function are NOT being made successfully.

Does anyone know any quirks of the compiler, or possible problems with the compiler that I may be falling foul of here? The only reason I ask is that with the previous problem I mentioned above, I didn't change anything - I simply made some comments and moved some functions around in the file to get a better look at the problem, and the issue "fixed itself", which is worrying.
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 25th Feb 2012 11:25
Oh and also, everything is in main.agc - the program isn't really that large.
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 25th Feb 2012 11:43
I've just found that if I change the statement in the StartNewGame function to...

global currenttotalgametime# = 0.0

Then it works. Surely that's not right? But it compiles and everything seems to work OK? Why should I be putting global there? Surely the variable has been defined as a global at the top of the program?
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 25th Feb 2012 11:52 Edited at: 25th Feb 2012 11:54
I think I see the issue, when you declared the variable "global currentgametime as float" the variable is called "currentgametime". If you then start using "currentgametime#" that is a new variable.

Try either:

global currentgametime as float
currentgametime = 0.0

or

global currentgametime# = 0.0

My hovercraft is full of eels
KareDev
12
Years of Service
User Offline
Joined: 6th Feb 2012
Location:
Posted: 25th Feb 2012 12:03
Ah OK, thanks very much for that.

I was under the impression that no matter how you declared them, if you wanted to work in floating point terms (as opposed to in integer terms) you had to always include the # on the end of the variable name. Fixed now!
lilpissywilly
AGK Developer
14
Years of Service
User Offline
Joined: 10th Sep 2010
Location: Office Chair
Posted: 25th Feb 2012 12:06
No problem, that's quite dodgy

My hovercraft is full of eels

Login to post a reply

Server time is: 2024-11-23 04:42:11
Your offset time is: 2024-11-23 04:42:11