[SOLVED] Problems with numbers

I have an array full of currencies for my game (this.currency = {gold:0};) and whenever I try to change the value it doesn’t always add 1+ the value but instead, it adds a new digit to the number. It only occurs when I use value += amount; otherwise, it will just work fine.

What could be the cause of it and how can I fix it?


Full Script
var Main = pc.createScript('main');

// initialize code called once per entity
Main.prototype.initialize = function() {
    /*--- prev session vals for temp---*/
    var data = {
        gold:sessionStorage.getItem('gold'),
        multi:{
            gold:sessionStorage.getItem('gmultip')
        },
        ship:sessionStorage.getItem('ship')
    };
        
    /*--- Vars ---*/
    this.currency = {
        gold: 0, 
        shards:0
    };
    
    this.multiplier = {
        gold:1, 
        shards:1
    };
    
    this.automaticcrew = {
        pirate:1,
        captain:0,
        officer:0,
        getVals:function(){
            return this.pirate+this.captain+this.officer;
        }
    };
    
    this.ship = 0;
    
    if(data.gold === undefined|| data.gold === null || data.ship === undefined|| data.ship === null){
        // do nothing
    }else{
        this.ship = data.ship;
        this.currency.gold = data.gold;
    }
    
    
    this.app.mouse.on(pc.EVENT_MOUSEDOWN,function(){
        var amount = 1*this.multiplier.gold;
        this.currency.gold++;
        var ent = this.app.root.findByName('UpArrow').clone();
        ent.enabled = true;
        this.app.root.findByName('2D Screen').addChild(ent);
        ent.setLocalPosition(pc.math.random(-screen.width/2,screen.width/2),pc.math.random(-screen.height/2,screen.height/2),0);
        ent.findByName('Amount').element.text = amount;
        var speed = 0.5;
        setInterval(function(){
            speed += 0.1/30;
            ent.translateLocal(0,speed,0);
            ent.element.opacity -= 0.01/3;
            ent.findByName('Amount').element.opacity = ent.element.opacity;
            if(ent.getLocalPosition().y > screen.height/2){
                this.app.root.removeChild(ent);
            }
        }.bind(this),0);
    },this);
};

// update code called every frame
Main.prototype.update = function(dt) {
    var shipnode = this.app.root.findByName('Ships').children[this.ship];
    this.app.root.findByName('Ships').children.forEach(function(node){
        if(node.name != shipnode.name){
            node.enabled = false;
        }
    });
    shipnode.enabled = true;
    
    this.app.root.findByName('Counter').element.text = this.currency.gold;
    
    
    /*--- save values for temp---*/
    sessionStorage.setItem('gold',this.currency.gold);
    sessionStorage.setItem('ship',this.ship);
};

Hi @Fus_ion,

From your explanation I would think that this happens because your value is of type string, not number. So instead of a numeric addition, the number is converted to string and appended at the end.

Though from your code I am not sure how or where this may happen. Could you check with the browser debugger or a console.log the type of your value during gameplay?

console.log( typeof amount);
2 Likes

when I did that I got two different types. I got a number when I did + and when I did += and I got a string.

What value do you add when you do +=?

1*this.multiplier.gold

Right, you are getting that value from localStorage, it will come back as a string.

Try parsing it first as a number:

    var data = {
        gold: parseFloat(sessionStorage.getItem('gold')),
        multi:{
            gold: parseFloat(sessionStorage.getItem('gmultip'))
        },
        ship: parseFloat(sessionStorage.getItem('ship'))
    };
2 Likes

Thanks. it works now.

1 Like