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 / Possible newbie array issue...

Author
Message
AlexMes
13
Years of Service
User Offline
Joined: 3rd Nov 2005
Location:
Posted: 2nd Oct 2018 10:40
Let me start to clarify my situation and what I am trying to do. I got an array with questions. I got a 2nd array which starts out empty but as people answer questions it gets populated with Id's of the questions already asked. I try to make a method so people dont get a question two times during a session. I just put down snippets of the code below because I dont want to paste 300 lines of code here. However eveything works as intended except for the if iArrayIndex = -1 handler. It gets bypassed even if questionsCheck.find (iCurrentQuestionId) returns a value > -1. Debugging wise I can confirm that the array questionsCheck does get populated with the correct id's. Maybe theres an easier or better way to check for a value in an array?

Snippets of my code:



Also is there a quick way to output an array in agk? Possible on an exit message? Like in PHP you can do print_r($array);exit; I have yet to find an easy AppGameKit method to quickly debug an array.
Phaelax
DBPro Master
16
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 2nd Oct 2018 13:17 Edited at: 2nd Oct 2018 13:17
Far as I see, the questions array is being created locally inside a function and hasn't been declared global. You're also calling that particular function in a loop, which makes no sense. Every time you try to find a new question, you're recreating the array over and over. Also, I don't see where you ever define iCurrentQuestionId

Have one array containing your questions. Have another array contain a pool of available questions that can be asked. This pool is just a list of indices to all the questions in the questions array. As you grab a question from the pool, remove it. Think of a deck of cards, you'll pulling a card from the deck. This would be more efficient that looping through the array trying to find one that isn't a duplicate. Essentially, it's the opposite of what you did. In your case, you added questions to the pool as you asked them, then searched each time to make sure a question wasn't already added to the pool. In my example, we throw all the questions into the pool from the start then randomly extract them, shrinking the pool each time.

Try this example:
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
AlexMes
13
Years of Service
User Offline
Joined: 3rd Nov 2005
Location:
Posted: 2nd Oct 2018 13:17
Its very weird because this simple script does work (no double questions)

Bengismo
1
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 2nd Oct 2018 17:09
A really good way of printing out all the data you have in a type structure is to use Print() and .toJSON()

In your case:
Print(questionsCheck.tojson())
This would print out all the items in the array and this works for arrays of types and will print out floats, ints and strings easily....good for debugging

Also
If your going to use the find operator then the array has to be sorted or the find may not work correctly. It operates bubble sort so if the items in the array are not sorted then the find will fail eventually.

Instead of using find you can write your own that checks EVERY value in the array for the value you are looking for.

And I would totally agree with Phaelax that a list of indices makes the most sense so you can just load up indices of the questions and ensure no duplicates are loaded into the array.
AlexMes
13
Years of Service
User Offline
Joined: 3rd Nov 2005
Location:
Posted: 2nd Oct 2018 17:52
Thanks for the pointers.... I will try them
AlexMes
13
Years of Service
User Offline
Joined: 3rd Nov 2005
Location:
Posted: 3rd Oct 2018 21:22
Thanks for the code examples Phaelax! Works like a gem.
sayto2018
User Offline
Joined: 15th Oct 2018
Location:
Posted: 15th Oct 2018 13:02
Thank you the thread was useful for me!
seifhd
User Banned
Posted: 9th Nov 2018 11:23
Thank you

Login to post a reply

Server time is: 2019-09-19 19:48:05
Your offset time is: 2019-09-19 19:48:05