How do I simplify my code ?

0 favourites
  • 6 posts
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • Hi,

    I try to manage high score to my game using local storage. It's a card game, the player choose the number of cards at the beginning of the game. I want to keep an high score for each number of card (high score with 12 cards, high score with 16 cards, etc.).

    I can do it using an event for each case but it's very tedious. My code is :

    It works, though. And my local storage data is good.

    But how can I optimize the code in construct ?

    Thanks

  • CheeseLife

    you could keep all that data in an array, and then it is a single structure to save and load from local storage.

    that would also let you expand the data stored - you could keep the top 10 best scores for each game type, save the date, user name. etc... and still only need one get and set.

    EDIT: also, you don't need to check if key exists and then get it. checking returns the value stored if it does exist.

    EDIT2: it also looks like you are checking local storage every single tick - 60 times a second. you really only need to read local storage at the start, and then save at then end of a game if the score is greater than then last high score. (although I can't tell for sure how your code is structured based on that image)

  • I agree with AllanR. You're using local storage like a variable. It should be used for saving things that you want to remember when the user quits playing so that you have that info the next time the user starts the game. You should load them on start of layout and save them only when you need to. The rest of the time, just use a global or local variables.

  • I don't totally understand what you are trying to do, but honestly I always use the CSV plugin to create little databases for my games. Especially if you want to carry over info from other layouts etc.. the CSV plugin is simple to use, you can look up using field names etc... you reference it very similar to an array but with the field names: CSV.At("score",player_id) and you can modify the table just as easy.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks for your answers.

    I was using this code when the player wins, not every tick. But I understand that is not the best way...

    I have never used the csv plugin, but with an array I could get something like that :

    1. I have my game type (12 cards, 16 cards...) and I define an Array.X to set the corresponding High Score.
    2. On start of layout, I get that score in LocalStorage
    3. When the player wins, I set the new highscore in the array.X

    I need to work on it and then I will post my result here

  • Ok, i found my way to do it :

    On start of layout, I check if the key exists, if not I create it

    I use a global variable to know the game type

    Then, I load the local storage in my array

    And when the player wins, I write the new score in the array, and then I set it to the Local Storage

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