Round floating point to integer


#1

Hey!

I have a simple timer setup

in initialize

    this.timeperlevel = 61;

in update

    this.timeperlevel -= dt;

I am showing the number in a ui text element, but the problem is that it shows a floating number, I need to convert it to integers like with Math.Round(), which is not included in the PlayCanvas API.

As the main project is private i have made a simple project containing just the timer: https://playcanvas.com/project/612739/overview/timer-test


#2

You can round/floor/ceil the float number before assignning it to the text element. As it’s a timer count down, I would suggest using ceil.


#3

How would i do that? i don’t see any of those function in Math Api


#4

like with Math.Round(), which is not included in the PlayCanvas API.

It’s not in the PlayCanvas API, but it’s still available in JavaScript, so there’s no issue using it in your projects.


#5

Oh! I had no idea i could acces common Javascript functions! Thank you!, but could you provide a code snippet? Every configuration i have tried for this problem returns NaN.


#6

Untested but you get the idea

this.timeperlevel -= dt;
if (this,timeperlevel < 0) {
    this.timeperlevel = 0;
}
someTextElement.text = Math.ceil(this.timeperlevel).toString();

#7

Still return NaN, but i get the idea, i’ll tinker some more with it.

Thank you all for your help! You are all fantastic people! Have a really nice day/weekend!


#8

The mistake was somewhere else in the code, i didn’t realize that when i only tested that particular screen it did not define this.timeperlevel = 60, it only did so when it was redirected from another scene. The code you gave works perfectly!


#9

So @alexrojae i have my code saying this:

EHealth.attributes.add('textHealth', {
    type: 'entity'
});

.....
 this.textHealth = 100;
......
if (this.app.mouse.wasPressed(pc.MOUSEBUTTON_LEFT)) {
    scale.x = pc.math.lerp(scale.x, 0, 0.7);  // SCALE DOWN
    this.textHealth -= 20; 
}   

would this work? well it doesnt but how would i make it work? i tried looking at your example above but pops up private


#10

Why are you setting an entity attribute to a number type?
Where are you setting the value to a text element?


#11

i did this so when i connect the textHealth attribute to the text entity i can tell it that thats the entity of text i want to change

Im doing this so it knows where to start(even though its set to the number) and where to end


#12

That’s fine expect for the fact that you are overwriting that variable with a number type. You are effectively doing this:

this.textHealth = this.app.findByName("SomeTextElementEntity");
this.textHealth = 100;

#13

so instead of
this.textHealth = this.app.findByName("SomeTextElementEntity");
i just leave this.textHealth = 100
and keep the attribute?


#14

Let’s try this again. Show me the line of code that is setting the value to the text element so you can see the number on screen.


#15

https://playcanvas.com/editor/code/612040?tabs=19182205 here id the full code,

EHealth.prototype.initialize = function() {
    this.healthEntity = this.app.root.findByName('eHealth');
    this.AI = this.app.root.findByName('AI');
    this.textEntity = this.app.root.findByName('win');
    this.textHealth = 100;
};

im setting the value here
Ive tryed this this.text.element.text = “”; to kinda help but never succeds (i think because it needs a string not a number


#16

I don’t see where you are setting the textElement text in your code.


#17

i thought by doing this:
this.textHealth = 100; was setting the value?


#18

Not to a textElement that isn’t.


#19

i have tryed doing this.textHealth.element.text but alsways calls element undefined, and ive tryed using pc.ELEMENTTYPE_TEXT but that diesnt work neither if neither one of those are right i have no idea what to do next


#20

ok so i did what ive seen used before i did this:

EHealth.attributes.add('textHealth', {
    type: 'entity'
});
...
    this.textHealth = this.app.root.findByName('textHealth');
...
 this.entity.element.textHealth -= 2;
...

at first i just did this.entity.element.text but called text undefined and i think its because i have something else named text
EDIT: Nothing else is called on as text
EDIT #2:
ok so i got the text to change but now what i want it to do is subtract 2 numbers every time ill keep trying thanks for helping out btw