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 / Random array int gets reset midcode...

Years of Service
User Offline
Joined: 3rd Nov 2005
Posted: 26th Sep 2018 00:31
Sorry i dont know how to properly describe my problem in the title. But basicly I got an array with questions and answers. Its going to be a trivia game/app. I got an integer set to randomly select a question from the big array. The code works as intended I would say 90% of the time but 10% of the time it will select a question and get the answers from another array entree. So here is how I define the array:

So ofcourse the function is called whenever a new question has to be loaded (start of the game or after a correct answer. My real question array is bigger than this but now you got an idea.

My problem: Like i said it works as intended for 90% of the time but sometimes it loads question[0].question (Question 1) but loads the answers of question[1].answer1 . So somehow iRandomQuestion integer went from 0 to 1 for example.

Anyone know what might be wrong? I tried to put a lock on the function so it can only be run once at a time, but still get this behavior.

I use this code for the buttons:

newQuestion = 1 will trigger the newQuestion function in the main loop
Valued Member
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 26th Sep 2018 09:50
Most likely reason is that your button script is returning returnValue = 0
Not sure why that would give the incorrect answers though, you must have some other code that doesn't update the answer text if the question is not above 0?
life's one big game
spec= 4ghz, 16gb ram, AMD R9 2700 gpu
Years of Service
User Offline
Joined: 3rd Nov 2005
Posted: 26th Sep 2018 14:22
Fixed it, was a bug somewhere else in the code. Sorry Can close topic.
DBPro Master
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 26th Sep 2018 15:59
I know you already solved this but,

Right off the bat, I see this line:

iRandomQuestion = random(1,(questions.length-1))

It'll never return 0, therefore you'll never see the first question. As for your problem, your code snippet shows no reason as to why the question and its answers wouldn't match up so the problem is definitely somewhere else in your code that you haven't shown.

And just a tidbit on your button function, might consider breaking it down into two functions and making the selection more modular.

I would find this much easier to read and reuse:

Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 26th Sep 2018 16:04 Edited at: 26th Sep 2018 21:40
or just use GetSpriteHit().... it makes the button check a very easy one line function

...and use GetPointerPressed() so you dont need a lockout variable

Login to post a reply

Server time is: 2023-12-05 08:10:45
Your offset time is: 2023-12-05 08:10:45