Round floating point to integer


#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


#21

That’s not quite right.

This is the code you have before (more or less):

var EHealth = pc.createScript('eHealth');
EHealth.attributes.add('textHealth', {
    type: 'entity'
});
// initialize code called once per entity
EHealth.prototype.initialize = function() {
    this.textHealth = 100;
};

At the start of the initialize, the variable this.textHealth is referencing an object of entity type that has been assigned in the editor.

When you called:

    this.textHealth = 100;

In the initialize function, you changed the variable this.textHealth from point to an object of entity type to now a number type with the value of 100. You lose the reference to the entity that referenced before.

Remember that Javascript is a weakly typed language so you can change a variable to reference ANY object type at any time.


#22

ya but if you look here
it seems to work just fine for me


#23

That’s because you are no longer assigning a number type to this.textHealth. You are assigning now it this.textHealth.element.text which also has issues that you can’t see.

I’m telling you the mistake you did before on this post Round floating point to integer so you can try to understand why it went wrong.


#24

yes i see it i feel stupid for knowing why i did that but i guess i wasnt really thinking. but i looked through some things that involved text and i seen what i did wrong and thanks to your help i got it correct :slight_smile:


split this topic #25

A post was split to a new topic: lookAt API question