How do I randomly select sets of data from a JSON object without repeating?

0 favourites
  • 10 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • I'm trying to implement a trivia game that should consist of a question and 3 different answers. This information would be extracted from a JSON. I currently know how to load JSON data into the project through AJAX and to select random sets of Q&A's. The part I'm having trouble with is to remove sets of Q&A that were already answered.

    In the sample project that I'm uploading there's a blue button to simulates when a new set is loaded into the view, a Text object that displays the JSON data and updates everytime a new set is loaded (and supposedly removed) and a "console" (another Text object) that displays the index that was selected randomly by the program.

    Sometimes it works as expected, but sometimes not. And I haven't been able to detect when is that it fails.

    I would appretiate if someone could take a look at the project. Thanks in advance.




  • Normally I would first load the data into an array.

    Then you have two options.

    A. Use the advanced random plugin to pick from a set of non repeating numbers associated with the index of your array.

    B. Delete the question out of the array after using it so it can't be picked again.

  • Thanks oosyrag for your reply. I tried storing the AJAX fetched data into an Array as you suggested, however something's not right. I was reading that in order to cast a JSON into an Array it needs to have an specific structure, is this true?

    Initially, when I was passing the AJAX fetched data to the JSON object and updated the Text object to show it using:

    The result "printed" in the Text object looked like this:

    I tried storing the AJAX fetched data into an Array by doing this:

    But the result looks like this:

    Is there a way to load the original JSON without modifying it's structure? and first of all, was my procedure correct? (how I loaded the AJAX data into the Array object I mean).

    Thanks for your time.


  • If the original json wasn't a construct array, you won't be able to load it directly with the array load action. You can definitely parse it with some loops, though I'll need to take a look at the project to see exactly how to do it. Can check it out later today.

  • oosyrag Awesome! Thanks, I'll be researching in the meantime If I come up with something I'll let you know.

    Thanks again.


  • I think you find a bug. JSON Data is not updated after some removes. Actually it look like updated but JSON.Get function sometimes return null values.

  • mrcgkh Yeah.. I think so too. I feel like the code should actually work that way. I will try with Arrays though when I am able to parse the data into an Array, and see what happens.



  • Update: I added some waits. I don't observe a problem right now. Probably GET function trying to read values before JSON is updated.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • mrcgkh Yess! you're right. It appears to be working in that way. Ok, I'll try to implement it that way on my main project :) thank you very much!

    (by the way, is there a way sort of to mark this thread as "solved" or to assign a correct answer like in Stack Overflow?)



  • Finally. I narrowed the cause (or rather the solution, because I'm not entirely sure what the causereally is). The secret was in this line that mrcgkh used while debugging:

    For some reason "printing" all the values of the JSON object before removing the next random item prevents the item from repeating. I'm not sure if the object restructures itself or something but the only thing I did (after testing a while every change in mrcgkh's version) was to add this Text and just "print" all the items there. That solved the problem. I noticed however, that everytime an item is removed from the JSON object, it "gets the value" of 0 and it's position on the array is now last.

    Another thing I realized while debugging was that the problem ocurred always that de next random index was equal to the previous selected random index.

    I would really aprettiate it if someone that understands why this happens could explain, because I'll be using Construct a lot this year and I could really use a deep understanding in this type of things in the engine.

    Thanks a lot guys!

    Special thanks to mrcgkh for contributing :)

    For anyone interested in testing the "solution" of the original sample project, here is a copy with that simple modification mentioned above, the only difference is a Text object with id="fix" that prints the values of the JSON (feels more like a hack):


Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)