[SOLVED] If statement in Update function does not execute

Hi,

I don’t know what I’m doing wrong here but if I set bool value to either true or false my if statement (this.increment) does not execute accordingly.

This is my code:

var Coins = pc.createScript('coins');

Coins.attributes.add('coins', {type: 'entity'});

Coins.prototype.initialize = function() 
{
    this.coins.element.text = globals.coins;
    this.buffering = false;
    this.increment = false;
    this.coinBuffer = 0;
    this.value = 0;
   
    this.app.on('coin', function (buffering, increment, value)
    {
        this.buffering = buffering;
        this.increment = increment;
        this.coinBuffer = (increment === true) ? globals.coins + value : globals.coins - value;
    }, this);
};

Coins.prototype.update = function(dt)
{
    if(this.buffering)
    {   
        console.log('buffering');
        this.value = dt * 1000;
        if(this.increment === true)
        {
            console.log('true');
            globals.coins += this.value;
            if(globals.coins > this.coinBuffer)
            {
                globals.coins = this.coinBuffer;
                this.buffering = false;
            }
        }
        else if(this.increment === false)
        {
            console.log('false');
            globals.coins -= this.value;
            if(globals.coins < this.coinBuffer)
            {
                globals.coins = this.coinBuffer;
                this.buffering = false;
            }
        }
        
        this.coins.element.text = Math.round(globals.coins);
    }
};

So, if I do:

this.app.fire('coin', true, true, 900);

I don’t get the console.log(‘true’) or console.log(‘false’) output, but I do get the console.log(‘buffering’) ouput. What is happening here?

Hi @ivan.vnieu,

I don’t see anything wrong in your code, it should print both console logs.

Are you able to share a sample project url to give it a try?

Hi @Leonidas, here is a link to the project:
https://playcanvas.com/project/675746/overview/alpha-crafty

So the coin script is attached to the Root entity and the this.app.fire(‘coin’, true, true, 900); gets called from the button script attached to the ‘Buy’ entity (UI/Popups/Time-up/Buy) and its using the arguments set in the editor.

If you get to the stage where the time is up and you get the popup, when you click on the button you are supposed to see the logs.

That’s a nice UI, great job!

So, your issue basically is due to a types mismatch. Here:

image

When you type true, false, these values are of type string, not boolean.

So the checks you do in your coin.js script aren’t valid. You can either:

  • Convert those values in your button.js to boolean if they are always either true or false in value:
this.app.fire(this.event, 
   this.argument_1 === 'true' ? true : false, 
   this.argument_2 === 'true' ? true : false, 
   this.argument_3);
  • Or change your checks in your coin.js script to check for string values:
        if(this.increment === 'true')
        {
            globals.coins += this.value;
            if(globals.coins > this.coinBuffer)
            {
                globals.coins = this.coinBuffer;
                this.buffering = false;
            }
        }
        else if(this.increment === 'false')
        {
            console.log('false');
            globals.coins -= this.value;
            if(globals.coins < this.coinBuffer)
            {
                globals.coins = this.coinBuffer;
                this.buffering = false;
            }
        }
1 Like

Thank you so much! I can’t believe that I have missed such a simple error in my code. It all works good now.

1 Like