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.

Newcomers AppGameKit Corner / [QUESTION] Behavior of Global Variables

Author
Message
Maik
8
Years of Service
User Offline
Joined: 29th Jul 2015
Location:
Posted: 29th Jul 2015 11:59


In my code, I'm expecting to generate a random number from 1 to 5 but not equal to the previously generated number. However, while I'm checking the output, I noticed that the value of previousGeneratedNumber returns to 0 every loop.

What is the behavior of global variable in AppGameKit?

Does the variable return to it's default value every time it is used within a function?
BatVink
Moderator
20
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 29th Jul 2015 14:00
Your understanding of global variables is correct.

The first time you go in, returnValue (RV) and PreviousGeneratedNumber (PGN) are zero, so it generates a random number.

The second time, RV is zero and PGN <> 0. Therefore it doesn't generate a new number and RV remains at zero. PGN then becomes zero.

These 2 steps repeat forever.

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Maik
8
Years of Service
User Offline
Joined: 29th Jul 2015
Location:
Posted: 29th Jul 2015 20:14
Thank you very much BatVink.

How could I miss that the variable rV doesn't have any value at the start of the Randomize() function.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Aug 2015 00:44
For some very wierd reason, this forum is displaying hyphens instead of the equal symbol when viewed in Firefox.

The best way to make sure that your while loop executes at least once is to set returnValue to previousGeneratedNumber just before the loop.

Cheers,
Ancient Lady
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 2nd Aug 2015 00:52
Okay, closing FF35 and opening FF39 fixed the issue. But it was still very wierd.

Every time I tried to type an equals sign, it showed a hyphen.

I even copied the code snippet from the original post and pasted it into a text editor and all the hypens magically became equals signs.

Cheers,
Ancient Lady

Login to post a reply

Server time is: 2024-03-28 21:37:46
Your offset time is: 2024-03-28 21:37:46