How do I display decimals in a text field (ie. timer)

Get help using Construct 2

Post » Sat Jan 31, 2015 5:21 am

Hi there, i found this thread which seemed promising but also a bit confusing.. viewtopic.php?f=147&t=118866&p=880519#p880519

all that said my game has a timer.. with a NUMBER variable called "Timer".. let's say you have 4 seconds to do something.. i would need it to start at 4.0 and always show a decimal.. 3.8 3.7... 3.1 3.0 2.9 etc

it seems like there must be some way to just get in there and be able to round a variable like my "Timer" variable to drop off anything after a certain decimal.

Thanks!
Caleb
B
56
S
9
G
1
Posts: 938
Reputation: 6,458

Post » Sat Jan 31, 2015 5:30 am

The thread definitely talks about what I'm looking for, but i just don't follow the expressions he's using as they don't (to me) seem to really correspond to how you would work in an event sheet.

all of the formatted string stuff was confusing.
B
56
S
9
G
1
Posts: 938
Reputation: 6,458

Post » Sat Jan 31, 2015 5:39 am

You could use tokenat and left expression but I don't know if it would be the best option since it will run every tick.

example(if your timer is a number, use str to convert to string):

tokenat(timer, 0, ",")&","&lef((tokenat(timer,1,","), 2)

The first tokenat should retrieve the integer numbers and the second should get two numbers after the comma
Example: timer = 4,37079798734234
Would return 4,37
I didn't tested it on c2 but should work.

edit: https://www.scirra.com/manual/126/system-expressions

Edit: How are you doing this timer?
I think it would be an option doing this way too.
Every 0.01 seconds > add 0.01 to timer (timer as a number)

This way you don't need to do anything and never would return more than 2 numbers after the comma.
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Sun Feb 01, 2015 12:31 am

@kossglobal how does that work exactly? making something into a string in C2? do i need to make a new variable to put the converted value or is it done in the expression itself?

using your expression i got a syntax issue.. i think "lef" is meant to be left but still something in there wasn't working right.

you use the "," throughout the formula which would be a period right for a timer right?

This looks really promising I've just never had to work with casting values from a number to string or the other way around so the whole concept inside of C2 and the right way to do it is a bit mysterious to me.
B
56
S
9
G
1
Posts: 938
Reputation: 6,458

Post » Sun Feb 01, 2015 1:24 am

part12studios wrote:@kossglobal how does that work exactly? making something into a string in C2? do i need to make a new variable to put the converted value or is it done in the expression itself?

using your expression i got a syntax issue.. i think "lef" is meant to be left but still something in there wasn't working right.

you use the "," throughout the formula which would be a period right for a timer right?

This looks really promising I've just never had to work with casting values from a number to string or the other way around so the whole concept inside of C2 and the right way to do it is a bit mysterious to me.



Yes, it is left. I typed wrong.

Well, you can use it. https://www.scirra.com/tutorials/678/in ... -functions
It's simple and work.
B
56
S
21
G
3
Posts: 602
Reputation: 6,612

Post » Sun Feb 01, 2015 5:59 am

oh that's nice. That's an awesome tutorial. this is exactly what i needed. a working example capx to emulate and then some! thanks!
B
56
S
9
G
1
Posts: 938
Reputation: 6,458

Post » Sun Feb 01, 2015 8:32 pm

I've been trying this out more and the example they give is good as a timer going up, but i'm struggling trying to figure out how to get a seoncds and decimal to appear as it should..

this is the closest I've gotten so far

zeropad(floor(Timer% 60), 1) & "." & zeropad(floor(Timer%60), 1)

however this results in showing the timer value like this.. 5.5 4.4 3.3 I can't figure out how to shift the area to the right of the period to display the value in the .0 area..

i'm trying to make it count down 4.0 3.9 3.8 3.7 etc.. I'm sure its just a few simple changes but as i dug deeper into this tuturial it was clear they were not going to discuss how to get in to milliseconds..

it seems like it might have something to do with changing the % value, but i've tried a bunch of variations and nothing seems to allow me to grab the milliseconds of the timer (or even the time value from the tutorial)

Thank you again for your help with this!
B
56
S
9
G
1
Posts: 938
Reputation: 6,458

Post » Sun Feb 01, 2015 8:42 pm

Hey I found this thread also which is even closer to what i want.. well pretty much spot on.. but it just shows a VERY different approach to the structure. always interesting to see how different people approach things viewtopic.php?f=147&t=72073&p=605439&hilit=showing+milliseconds#p605439

I'm dissecting that capx next to take that approach.. i was able to quickly modify it to make the action subtract from and changed the timer from 0 to 10 and it counted down nicely.
B
56
S
9
G
1
Posts: 938
Reputation: 6,458


Return to How do I....?

Who is online

Users browsing this forum: jkmalau and 15 guests