Jump to content

I need to Store some data


OGKaqtus
 Share

Recommended Posts

I am trying to store from this code:

Potato.upgradeButtonsData = [
        {icon: '1stUpgrade', PIcon: 'potato4Upgrade', background: 'upgradeBg', themeText: "", name: '', level: 1, cost: 100, pas: 100, purchaseHandler: function(button, player){
            Potato.player.clickDmg += 0.2;
        }},

But the problem is that when I try it either says "Nan" or will just do nothing.

I am using localStorage because i could not get the store.js thing to work.

And if you have to store all the items(by all the items i just mean (level, cost and pas)) like an Array then I dont know how to. I am pretty new to Phaser

This is the code I am using to store the items in the list:

if(localStorage.getItem('numL') === null){
        console.log("Creating localStorage 'numL");
        localStorage.setItem('numL', Potato.upgradeButtonsData.level);
    }
    
     if(Potato.upgradeButtonsData.level >= 1){
            Potato.upgradeButtonsData.level = localStorage.getItem('numL');
        }
    
    Potato.upgradeButtonsData.level = parseInt(localStorage.getItem('numL'));

And in the update method:

localStorage.setItem('numL', Potato.upgradeButtonsData.level);

Any help would be awesome! :)

 

 

EDIT: I have more than 1 item in the lists in my actual code, this is just an example

Edited by OGKaqtus
needed text
Link to comment
Share on other sites

I'm not sure I understand your question, but you're missing a "parseInt" on that last call to "localStorage.getItem('numL')". getItem returns a string, so you need to call parseInt (like you've got on that other line). I'm not sure why adding to a string would make NaN, unless you're also dividing or multiplying that string somewhere else? It should (maybe annoyingly) just concat the number to the string, i.e. "42" + 3 === "423".

Link to comment
Share on other sites

I have to store a list of items in in localStorage. The problem is when i try to store the whole list because there are multiple item in the list, it just says Nan

Potato.upgradeButtonsData = [
        {icon: '1stUpgrade', PIcon: 'potato4Upgrade', background: 'upgradeBg', themeText: "Buy some mighty hands... and increase your\nPAS.", name: 'Click Power ', level: 1, cost: 100, pas: 0.2, purchaseHandler: function(button, player){
            Potato.player.clickDmg += 0.2;
        }},
            
        {icon: '2ndUpgrade', PIcon: 'potato4Upgrade', background: 'upgradeBg', themeText: "Tatoes are small helpers which increase\nyour PAS.", name: 'Tatoes ', level: 1, cost: 500, pas: 0.5, purchaseHandler: function(button, player){
            Potato.player.pas += 0.5;    
        }},
        {icon: 'chipFactory', PIcon: 'potato4Upgrade', background: 'upgradeBg', themeText: "Chip Factory generates alot of chips,\nwhich will increase your PAS.", name: 'Chip Factory ', level: 1, cost: 1000, pas: 0.7, purchaseHandler: function(button, player){
            Potato.player.pas += 0.7  
        }},
        {icon: 'potatoFarm', PIcon: 'potato4Upgrade', background: 'upgradeBg', themeText: 'The Potato Farmers has been farming potatoes\nfor decades, they will increase your PAS', name: 'Potato Farm', level: 1, cost: 5000, pas: 1, purchaseHandler: function(button, player){
            Potato.player.pas += 1;
        }},
        {icon: 'fingerChips', PIcon: 'potato4Upgrade', background: 'upgradeBg', themeText: 'Delicous Potato Fingers made with secret\ningredients, increases your PAS', name: 'Finger Chips', level: 1, cost: 10000, pas: 3, purchaseHandler: function(button, player){
            Potato.player.pas += 3;
        }}
        ];

I am trying to store the level, cost and pas values in localStorage for each item.

So if we say the last 'fingerChips' is level 1, and the '1stUpgrade' is level 3, then it would store that the '1stUpgrade' is level 3, and the 'fingerChips' is level 1, but i dont have to store the text because that will always be the same.

When i then load the page again it would remember that the '1stUpgrade' is level 3, and the 'fingerChips' is level 1

Link to comment
Share on other sites

Ah, okay, yeah. localStorage can't really store arrays directly since it coerces everything into strings when it stores them: https://jsbin.com/sulufew/edit?js,console Notice how it stored "1, 2, 3, 4" *as a string*. And when we say l[1] it says the value is ","

But if you rely on the JSON parser to do some of the work for you, you can store an array: https://jsbin.com/botuhu/edit?js,console

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...