How do I create a list with the last 5 results?

0 favourites
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • I need to display the last 5 results of a game, for which I was thinking of using the local storage for this, which includes the date, level, amount of right and wrong points, and the time in minutes referring to the duration of the game. I tried to solve this by accessing some tutorials and I saw that I will need to use an array for this, but I don't know how to include this information in it so that it is displayed on the screen and how to manage it. I don't need to sort the results for the highest amount. Just show them in play order. Could anyone help me understand how to do this?

  • Assuming you have all that data stored in other variables ready to go you would create a new row of an array (push the data to the back of the array) and set all the data across each column. You would also need an additional check if you have 5 rows (array X size) then you can delete the top row so you only ever have 5 entries.

    To display it in game you could have the text objects with variables that relate to co-ords of the array and then display that info, so text where variables are 0,0 for x and y display array.at(0,0), so of course in one event for all texts it's something like set text to array.at(text.var1,text.var2)

    If any of that confused you I would check out the array tutorials some more, adding data to an array is fairly simple stuff when you have tried it a few times.

  • Thanks.

    I have some questions:

    - I need to have 5 rows and columns corresponding to the data I want to show, right? In this case, it would be date/rights/errors/level

    - Do I need to store the array data in variables or simply pull these data from the array and display them in order whenever I show the score?

    - Should I always populate the array at the end of the game and then use local storage to store this data on the user's machine? how would the data be updated in local storage?

    - How to store array data in the local storage? Storing the data in variables first?

    The biggest question I have is how to include 4 different pieces of information (date/rights/errors/level) in the array.

    EDITED***

  • Just think of the array like an excel spreadsheet, you store data as cells with rows and columns. No you don't store it in variables but you need to get the data from somewhere I guess you already have events for that. So if the row is X=1, then you set the data at 1,0 1,1 1,2 etc

    For saving it depends how you are already saving the game. If you are using system save/load then array would save/load with that. You can export a whole array as a JSON string though and store this in local storage if you need it.

  • Thanks again.

    Yes, I intend to use this solution to store data. In this case, I will store the entire array in the local storage, right?

    You said I can use the array coordinates to store the information, but how can I do that? I believe that if I have 4 columns I have to use "Y" coordinate but from 0 and keep this position in the X coordinate for all the information to be at the top of the list (like this: (0,0/0,1/0,2/...). Is that right? If you

    Understanding this is the most complicated for me until now. If you could post a pseudo code here I think it would illustrate this very well.

  • Yes you would set the data at 0,0 and other data at 0,1 0,2 0,3 0,4. Then the next row is 1,0 1,1 1,2 1,3 1,4 like a table. You can learn more by doing than explanation, practice with the array object.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I really can't understand things without examples. I appreciate your help so far. Thanks.

    Let's see if I can get some help from someone else.

    I tried this code:

    On function 'populateArray'
    -> Array_Score: Set value at (0, 0) to Score_Date
    -> Array_Score: Set value at (0, 1) to Score_Rights
    -> Array_Score: Set value at (0, 2) to Score_Wrongs
    -> Array_Score: Set value at (0, 3) to Score_Level
    -> Array_Score: Push front Array_Score.At(0,LoopIndex) on X axis
    -> Array_Score: Delete index 5 from X axis
    ----+ System: Repeat Array_Score.Width times
    -----> Score: Append LoopIndex+1&" - "&Array_Score.At(LoopIndex)&newline
    

    I don't understand why it doesn't display all columns of the first row of the array.

    I already tried this code too:

    On function 'populateArray'
    -> System: Set arrayData to str(Score_Date&" "&Score_Rights&" "&Score_Wrongs&" "&Score_Level)
    -> Array_Score: Push front arrayData on X axis
    -> Array_Score: Delete index 5 from X axis
    ----+ System: Repeat Array_Score.Width times
    -----> Score: Append LoopIndex+1&" - "&Array_Score.At(LoopIndex)&newline
    

    I only got the 1st line with this code but it doesn't seem very natural for me. I believe the right thing was to handle this directly from the array

    Could someone explain to me what is wrong or missing?

  • You can use debug view to see the array, I wouldn't start out by using the text objects to check if it's correct. I don't know what you are attempting with the loop I thought you were simply adding a row of values so set value should work and you should see those values in debug view.

    Something that might be useful, to add a new row you can push the first value to the back of the array which creates the new row, then the other values you set at (array.width-1,1) (array.width-1,2) etc because the width (number of X rows) of the array is always 1 more than the highest row since they start from 0, i.e. 3 rows 0,1,2 have a width of 3

    When you have managed that then you know how to add a new row of scores at the end of the level.

  • There I made a file : dropbox.com/s/3i3937n3ekzgf6i/add%20row.c3p

  • Thanks a lot for the example. It is now clear what you explained earlier.

    I can then store the whole array in local storage and each time the game comes in I must populate it with the array information and then insert the new data according to the new scores, right?

  • As mentioned if you are using system save/load then that is good enough. If you want to use local storage then you can save the array as JSON string and load from local storage when launching the game.

  • Thank you very much!

  • I wonder if it's not possible to use a function instead of a button to add data. I did a test and it always only writes to the first 4 fields (first line)

  • I wonder if it's not possible to use a function instead of a button to add data. I did a test and it always only writes to the first 4 fields (first line)

    You can put all the logic into a function and call it when you press the button if you want, there's no difference. The second bit I don't understand or the problem you are having.

  • I had already tested calling the function through the button and it worked, but I need a game action to call this function and what happens is that instead of skipping the lines, the program always writes the new score on the first line, overlapping the previous information.

    From what I understand, from watching the code, maybe this happens because the width of the array is being used as a reference to add new lines. What do you think?

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