Gather text update issues

So, I have been working on this simple script which is supposed to update every time the UI element is clicked (which it does perfectly) and updates the text by 5, however when the text reaches 1000, it should be displayed as 1k, and this should be the limit that the text should reach. instead, though, it reaches 1000 and says 1000 instead of 1k, when you click again it says 1k, which isn’t when it should be displayed, then if clicked again it resets. Anyone got any ideas?

here’s the script:

var Gather = pc.createScript('gather');

// initialize code called once per entity
Gather.prototype.initialize = function() {
    this.entity.element.on('click', this.onClick, this);
    this.resourceCounter = this.app.root.findByName('resource counter');
};

// update code called every frame
Gather.prototype.update = function(dt) {

};

// function called when entity is clicked
Gather.prototype.onClick = function(event) {
    var count = parseInt(this.resourceCounter.element.text);
    count += 5;
    this.resourceCounter.element.text = count.toString();
};

I fixed it to where when it reaches 1k the event listener is turned off and no longer goes up, however, it still won’t display 1k instead of 1000 until it is clicked once more after reaching 1000. Does anybody know why this is happening?

@Jacob_McBride1 Is this the entire piece of code that handles the update? Seems like there is some other code somewhere else that is acting on this number. Not sure where the logic is that is adding the K once over 1000. Also there seems to be an if() evaluation somewhere that is using Greater Than or Less Than without also checking if equal. Is there a project that you can share?

1 Like

no this is the only script. Do you mean it refers to an outside function? I do not remember that as all the code is contained in this script. Here is the updated script where I added that max out point:

var Gather = pc.createScript('gather');

// initialize code called once per entity
Gather.prototype.initialize = function() {
    this.entity.element.on('click', this.onClick, this);
    this.resourceCounter = this.app.root.findByName('resource counter');
};

// update code called every frame
Gather.prototype.update = function(dt) {

};

// function called when entity is clicked
Gather.prototype.onClick = function(event) {
    var count = parseInt(this.resourceCounter.element.text);

    if (count >= 1000) {
        this.resourceCounter.element.text = "1k";
        this.entity.element.off('click', this.onClick, this);
    } else {
        count += 5;
        this.resourceCounter.element.text = count.toString();
    }
};

It does refer to the text entity called recourse counter however, is this what you mean?

1 Like

@Jacob_McBride1 What do you want it to display when the count goes to 1100?

1 Like

Okay so looks like I found another error :sweat_smile: So it seems that in the editor linked here there are multiple entities that can be clicked on to add to the text. Maybe I should add a class such as tree, rock, and berries (as it is for a survival craft game) and then when they reach that max amount it disables the event listener?

So basically the script is supposed to close the listener once the max has reached (1000) and display 1k instead and no longer update the text. but the other thing is that there are multiple entities with the script so when you reach the max (1k) on one entity if you click another the text resets.

1 Like

And still, even when it does reach 1000 it displays it as 1000, and when clicked again it displays as 1k. It is supposed to display 1000 as 1k and nothing else

@Jacob_McBride1 So you can capture exactly what is going on you could put a console.log(count) on line 17. Also, you could add one inside below line 19 to see if it is actually getting in there based on the console.log with the count.

So is this what you mean? here is the code:

var Gather = pc.createScript('gather');

// initialize code called once per entity
Gather.prototype.initialize = function() {
    this.entity.element.on('click', this.onClick, this);
    this.maxCount = 1000;
    this.isMaxed = false;
};

// function called when entity is clicked
Gather.prototype.onClick = function(event) {
    if (this.isMaxed) {
        return;
    }
    
    var count = parseInt(this.entity.element.text);
    if (count >= this.maxCount) {
        this.entity.element.text = "1k";
        this.isMaxed = true;
        this.entity.element.off('click', this.onClick, this);
    } else {
        count += 5;
        this.entity.element.text = count.toString();
    }
};

The problem in your second script is when you set the text to ‘1k’, the next time you click your count is not 1000 anymore, because you use your text to determine the value.

I suggest to create a variable this.count in the initialize or a global variable count and use this to update the value and set the text. Then you can skip the parseInt line.

Your last script can also work, give this still the same problem?

2 Likes

Perfect! Thanks for helping! It works now.