How to retrieve timer value into a text object ? - [SOLVED]

I am trying to display the value of the timer into a text object. What would be the code that needs to be entered into the Text: ______ to get the timer value ? The code that could be similar to GlobalVariableString(GlobalVariableName) which is entered to get the value of the global variable to be it displayed by the text object ?

Something like ToString(TimerElapsedTime(“yourtimername”))

1 Like

Thanks. I have copied the code and it works. :mrgreen:

But how do i do the same going through the options without typing anything when clicking the txt button after it ? Is it possible. Just curious to know.

Or is there sections in documentations that shows all such codes ?

I think it is a flaw of the editor.
You can try other action and copy the code from there.
For some reason the action that changes text has fewer functions in the editor.

I searched within the documentation and found this as useful. wiki.compilgames.net/doku.ph 
 s/built_in

But now i got the problem in using trunc with the new code. How do i make it display one decimal place like 3.1, 2.4.etc. ? Where should i enter the trunc (value) in this code with all the brackets being there ?
screenshot.PNG

ToString(trunc(TimerElapsedTime(“yourtimername”)))

Thanks but that didn’t make any difference. There was no error even with that white space before. :ugeek:

It worked earlier but it truncated all the decimal places and didn’t even show one place. I think the trunc (value) needs to be entered as trunc (1) for one place like ‘3.1’ get displayed, trunc (2) for two places like ‘3.15’ to get display displayed.etc. which was what i was asked with the question earlier. But the problem was all those brackets making me confuse where to enter the trunc (value) into there.
screenshot_2.PNG
screenshot_1.PNG

Maybe you can use round instead.

ToString(round(TimerElapsedTime(“yourtimername”)*10)/10)

Eventhough it works fine for most of the parts, it doesn’t seem to retain it while displaying a whole number. For instance, when reaching 3.0 it displays 3, instead of displaying 4.0 it just displays 4. So it is not the equivalent of trunc which always retains the decimal places which is it’s direct action. The workaround with the round function is not a perfect replacement i think.

The problem that it causes is that the timer appears inconsistent, glitchy and not at all smooth when it’s running.

A workaround could be check if the string length is = 1, if so add a “.0” string to the text, this way:
DisplayTimer.gdg (7.21 KB)

This works now . Thanks a lot. Both of you. :sunglasses:

So the trunc function is impossible to use with a value in gdevelop ? I think it does work to zero decimal places when it’s typed in as code. So there is no way to place in a value there to be done with a simple single line of code instead of a workaround ? I think if trunc works to zero places fine, there might be a code to make it run with a value similar to trunc (2).

Wait a second
 while taking a rest I noticed that my workaround will work only if the timer doesn’t reach 10 seconds, because after that time, even integer numbers without floating point will have a length > 1 :neutral_face:
Instead checking if the string length is = 1, you can check if the string contains a dot “.”, with the function:

StrFind(YourTextObject.String(), ".")

If this function returns -1, the dot was not found so you have to add “.0”.

trunc just discards the decimal part of the number, and return the integer, but something like text formatting functions would be useful indeed :slight_smile:

I am still getting confused when i try to implement more codes. I am attempting to reverse the timer to display a countdown but i am confused with the brackets and it’s doesn’t seem to be working.

ToString(round(GlobalVariable(AbilityCooldown)-(TimerElapsedTime("AbilityCooldownTimer")*10)/10)

How to make something like this work ?

In my case, the timer needed to be only a single digit number so it didn’t matter much. But it’s still a great improvement and i will try to implement it using the method.

So it just removes the decimal and gets the integer in godot. If that’s the case, what i said wouldn’t exist.

Thanks Again. :slight_smile:

Here is a more complex example:
Cooldown.gdg (16.3 KB)

Do it in little steps, in a some-way inverse order (actually the way the machine do it):

  • First, you need the difference between the cooldown variable and the timer (accurately, don’t round yet):
GlobalVariable(CooldownAbility) - TimerElapsedTime("AbilityCooldownTimer")
  • Then you multiply the value *10, so you save the first decimal number, the others will be deleted by the round function:
( GlobalVariable(CooldownAbility) - TimerElapsedTime("AbilityCooldownTimer") )*10
  • Now round the value, so you keep the integer part only:
round(  ( GlobalVariable(CooldownAbility) - TimerElapsedTime("AbilityCooldownTimer") )*10  )
  • Divide /10 to get the decimal saved again as a decimal:
round(  ( GlobalVariable(CooldownAbility) - TimerElapsedTime("AbilityCooldownTimer") )*10  ) /10
  • Finally, convert the number in a string:
ToString(    round(  ( GlobalVariable(CooldownAbility) - TimerElapsedTime("AbilityCooldownTimer") )*10  ) /10    )

If the code gets very complex, you can divide it in multiple variables:

Variable diff = GlobalVariable(CooldownAbility) - TimerElapsedTime("AbilityCooldownTimer") Variable rounded = round( Variable(diff)*10 ) /10 Text object string = ToString( Variable(rounded) )
:slight_smile:

1 Like

That was extremely helpful. Perfectly covered everything. Thanks a lot. :mrgreen:

Everything is completely running perfect now. :slight_smile: