ScoreSystem bug!

Ok so I was working on a way to fix the score system bug in Matrexball,
But I still encountered the bug anyways here’s thr bug

(Look at the score)

Also here’s the code:


Heres the link:https://playcanvas.com/editor/code/624824?tabs=21805247,24799274
Aand heres the score folder:https://playcanvas.com/editor/code/624824?tabs=21805247,25164720

Hi @Marquise_Edwards,

It seems that you aren’t resetting the state of your number entities when you are changing levels.

Actually you took a very difficult route in implementing scoring, having a different entity for each number. And also doing the scoring updating per frame in your update method, instead of using events.

I’d propose that you try having a single entity with an element component, and update score only when it is needed: when the player scores update the text property of the element component. You could that easily using events, example code:

this.app.on('player:scored', function(){
   this.score++;
   this.entity.element.text = this.score;
}, this);

So do I make a new script and just add that script to the ball or score?

My code isn’t complete, it’s just an example of a different way to setup your scoring system.

If you go that way you will need to change your numbers entities from many to a single and then attach a script that takes care of updating the score.

You will need to learn how to communicate from the point in your code where the scoring happens to that script, when that happens.

You can learn how to use Playcanvas events here:

https://developer.playcanvas.com/en/user-manual/scripting/communication/

Cool I’m done to learn a easier and I’m also down to learn more about javascript and this engine so thnx

1 Like

So I came up with something like this

That’s a good start, though you need to have the method properly named: initialize instead of intalize.

And also make sure to set a starting value to score, in your initialize method, you could add this after line 6:

this.score = 0;

Then you need to attach that script to your scoring entity and start calling the event in your scoring scripts, something like this:

this.app.fire("player:scored");

That will increase the score and render it each it is called.

1 Like

So when you said "That’s a good start, though you need to have the method properly named: initialize instead of intalize.

And also make sure to set a starting value to score, in your initialize method, you could add this after line 6"

So, Playcanvas scripts have a number of methods that are called automatically by the engine like initialize, postInitialize, update etc.

From what I see you attempt to call initialize, but you have a syntax error which you need to fix. Instead of calling it intalize on line 6, you need to properly name it initialize. You can learn more about the structure of a script here:

https://developer.playcanvas.com/en/user-manual/scripting/anatomy/

Regarding setting an initial value: when in Javascript you use the following structure to increment the value of a variable:

this.score++;

That requires the variable or property to have already a numeric value, to increment. If it is undefined, it will remain undefined. Here is an attempt to fix your code:

Scorep.prototype.initialize = function(){
   this.score = 0;
   this.app.on('player:scored', function(){
      this.score++;
      this.entity.element.text = this.score;
   }, this);
};
1 Like

So I fixed the error!!!

var Scorep = pc.createScript('scorep');

Scorep.attributes.add('Score', {type:'entity'});

var score = "game";
console.log('Score started');

Scorep.prototype.initialize = function(){
   this.score = 0;
   this.app.on('player:scored', function(){
      this.score++;
      this.entity.element.text = this.score;
   }, this);
};

But Now I’m wandering am I supposed to add a entity for every number just like in the original score script?
Heres the original code btw:https://playcanvas.com/editor/code/624824?tabs=25529770,24799274

No you need to add a single one, with an element, text, component and start calling this event from your other scripts.

Do check my explanation about that above.

Ok :sweat_smile: now I’m confused

Study the following tutorial to understand how a single element entity can render any kind of text:

https://developer.playcanvas.com/en/user-manual/user-interface/text-elements/

1 Like

Yep I know this already

So, where are you stuck?

So Yes I know how to code it but what I don’t understand is how this code here:https://playcanvas.com/editor/code/624824?tabs=25529770,24799274
Can be switched to this:https://playcanvas.com/editor/code/624824?tabs=25529770,24799274
Bsically I’m am I supposed to do something like this

var ScoreSystem = pc.createScript('scoreSystem');

//Numbers
ScoreSystem.attributes.add("zero", { type: 'entity', title: '0' });
ScoreSystem.attributes.add("one", { type: 'entity', title: '1' });
ScoreSystem.attributes.add("two", { type: 'entity', title: '2' });
ScoreSystem.attributes.add("three", { type: 'entity', title: '3' });
ScoreSystem.attributes.add("four", { type: 'entity', title: '4' });
ScoreSystem.attributes.add("five", { type: 'entity', title: '5' });
ScoreSystem.attributes.add("six", { type: 'entity', title: '6' });
ScoreSystem.attributes.add("seven", { type: 'entity', title: '7' });
ScoreSystem.attributes.add("eight", { type: 'entity', title: '8' });
ScoreSystem.attributes.add("nine", { type: 'entity', title: '9' });
ScoreSystem.attributes.add("ten", { type: 'entity', title: '10' });

//Points
ScoreSystem.attributes.add("p1", { type: 'entity', title: 'Point 1' });
ScoreSystem.attributes.add("p2", { type: 'entity', title: 'Point 2' });
ScoreSystem.attributes.add("p3", { type: 'entity', title: 'Point 3' });
ScoreSystem.attributes.add("p4", { type: 'entity', title: 'Point 4' });
ScoreSystem.attributes.add("p5", { type: 'entity', title: 'Point 5' });
ScoreSystem.attributes.add("p6", { type: 'entity', title: 'Point 6' });
ScoreSystem.attributes.add("p7", { type: 'entity', title: 'Point 7' });
ScoreSystem.attributes.add("p8", { type: 'entity', title: 'Point 8' });
ScoreSystem.attributes.add("p9", { type: 'entity', title: 'Point 9' });
ScoreSystem.attributes.add("p10", { type: 'entity', title: 'Point 10' });

The idea is this: you don’t need to use multiple entities for all the numbers, just a single one which can render any score text.

So start simple, drop that script for the moment and start understanding the code we wrote above, line by line.

Something that can help you is to isolate your issues, start a blank project and try implementing just this function. When you got that there and you have understanding then try moving that code to your active project.

Alright now I understand I’ll try that

1 Like

So I did what you said and this is what I came up with:
https://playcanvas.com/editor/scene/838866

Good, now from your understanding given what we said on this thread, what is the next step?

Also, out of curiosity, why did you put the Score element as a child to your Camera, instead of the 2D Screen?

image