• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

Show Timer above unit (Format String problem)

Status
Not open for further replies.
Level 3
Joined
Apr 1, 2019
Messages
48
Hey, I'm right now creating a map, which needs some timers above units.
So basically I create a floating text and update it each second, reading the value of the timer.

So I did do it with GUI at the moment, but if you offer a solution in jass it is okay too.

What I did:
I did devide the string into 3 parts.
First Part: Convert Int to String, Convert Real to Int, Timer Remaining Time / 60
Second Part is literally a ":" (colon)
Third Part: Convert Real To Formatted String (2,0), Convert Int to Real, Convert Real to Int, Math - Modulo (Timer Remaining Time MOD 60)
upload_2020-7-3_18-35-10.png


Problem:
The Timer looks like: 1:16.0 and 1:9.0
But i wanted: 1:16 and 1:09

Edit:
At the moment I can only think of writing a method which changes "9" to "09" and leaves "16" as "16" myself, via jass or gui.
But is there anything prescripted I could use?
 
Last edited:
Level 24
Joined
Jun 26, 2020
Messages
1,853
Problem:
The Timer looks like: 1:16.0 and 1:9.0
But i wanted: 1:16 and 1:09

The problem in the second is you used "Convert integer to real"
3.PNG


Edit:
At the moment I can only think of writing a method which changes "9" to "09" and leaves "16" as "16" myself, via jass or gui.
But is there anything prescripted I could use?

In this before you have to do something like this:

  • If Length of String_seconds is equal to 1 set String_seconds=0+String_seconds else do nothing
 
Level 3
Joined
Apr 1, 2019
Messages
48
The problem in the second is you used "Convert integer to real"View attachment 358881
Sry that did not help.
I need that integer to real to perform a formatted string. There is no format from int to string - is there?
(But thank you for replying!)

In this before you have to do something like this:

  • If Length of String_seconds is equal to 1 set String_seconds=0+String_seconds else do nothing
Thats exactly what i mean by that. But that means I either have to write it triple (one for check, one for +"0", one for normal), or to temporarily save it and gain an onetime use variable.


Soo... is there a method to get that length by format? There shoulb be... I thought there is...
If there is not, I rather add a function in Jass, taking string, returning string, which converts this for me than adding that variable.
But I would not like adding that jassscript, as it would either need my code to be transformed to jass in order to fit in, or to use some "custom script" in GUI.

This is why I asked, if I did miss something which would make my situation easier.
I feel like there is a solution prebuild but I missed it.
 
Level 3
Joined
Apr 1, 2019
Messages
48
Sorry, english is not my first language, I meant:

There is no conversion from int to formatted string.


Edit:
The whole point why I convert it to real and then to string is to do the format, because the description of formatted string said:
"fromatted(x,y) where "x" is the minimum length of the numbers left and right side of the point together, while y is the maximum size right of the point."

Thats why I thought that the format would turn a "7" with (2,0) into a "07"
but apperently it turns "17.000000" to "17.0" and "7" to "7.0" although the "0" indicates I dont want any numbers right of the dot.
 
Level 24
Joined
Jun 26, 2020
Messages
1,853
Sorry, english is not my first language, I meant:

There is no conversion from int to formatted string.


Edit:
The whole point why I convert it to real and then to string is to do the format, because the description of formatted string said:
"fromatted(x,y) where "x" is the minimum length of the numbers left and right side of the point together, while y is the maximum size right of the point."

Thats why I thought that the format would turn a "7" with (2,0) into a "07"
but apperently it turns "17.000000" to "17.0" and "7" to "7.0" although the "0" indicates I dont want any numbers right of the dot.
In that case use "Substring"
 
Level 3
Joined
Apr 1, 2019
Messages
48
Substring would be a possibility to in fact delete that "0" behind the dot - just like converting it directly from int would do.
The problem still is persistant: I want all the numbers to have exactly 2 characters, while if only 1 then the first is a zero.

I thought that "formatted string"-prebuild function would be able to do it. And as I failed, I thought someone may know an easy prebuild function.

Soo... If you have any nice prebuild function in jassscript or gui or need me to explain the problem, just go ahead.


The solution I dont wanna use yet is to check via "if".

I mean, the description of that function tricked me into thinking, it would add a "left 0" instead of one behind the dot, when I there is a "0" for the number of characters right of the dot.
 
Level 24
Joined
Jun 26, 2020
Messages
1,853
Just do it before creating the text, its the unique form:
  • Events
  • Conditions
  • Actions
    • Set seconds=(The remaining seconds)
    • Set String_seconds(String(seconds))
    • If Length of String_seconds is equal to 1 set String_seconds=0+String_seconds else do nothing
    • Floating Text - Change text of FloatingTextTimer to (String(Minutes)+:+String_seconds), etc
But if isn't solve the problem or you wanna do it in some another way, I give up.
 
Level 3
Joined
Apr 1, 2019
Messages
48
Thank you for your help.
Yes, I understood what you meant there
In this before you have to do something like this:

  • If Length of String_seconds is equal to 1 set String_seconds=0+String_seconds else do nothing
But
The solution I dont wanna use yet is to check via "if".
because
Soo... is there a method to get that length by format? There shoulb be... I thought there is...
If there is not, I rather add a function in Jass, taking string, returning string, which converts this for me than adding that variable.
But I would not like adding that jassscript, as it would either need my code to be transformed to jass in order to fit in, or to use some "custom script" in GUI.

This is why I asked, if I did miss something which would make my situation easier.
I feel like there is a solution prebuild but I missed it.

Because in my actual code, which is pretty long, I would need to temporarily save the substring/string and add a temporary variable or to do that same very long code clicking three times.


As I want to add several Timers, both of the solutions would take alot of additional work and screw up the code, which makes a jass-function the only alternative to what I asked:

Did I miss something? Can you maybe use the prebuild "Formatted String" function to achieve what I want?
Or is there another function which may be used to add leading zeroes, so I do not need an "if">?
 
Level 3
Joined
Apr 1, 2019
Messages
48
What is your first language?
Is my language part of my problem here? I thought my english would be acceptable and a little understandable after all these...
However, I do not want to reveal my nationality to everyone.
Keep it a secret.
 
Level 24
Joined
Jun 26, 2020
Messages
1,853
Is my language part of my problem here? I thought my english would be acceptable and a little understandable after all these...
However, I do not want to reveal my nationality to everyone.
Keep it a secret.


I asked you for that because I wanted to know if you speak Spanish, because I speak Spanish and that make the things easier, now I finally understand you:

1) Formatted string don't erase the decimals, just truncate them, and don't add zeros to the left just emptys spaces, you have to add them manually (Although I think you noticed).
2) If you wanna make a lot of timers, the unique solution I can think is using hashtables, because you can save integers, strings, etc and you can assign them to units.
 
Level 3
Joined
Apr 1, 2019
Messages
48
I already assigned other things to that units.
I think a jassscript would help me the most if that formatted string cannot be tricked into doing my work - or there is a nother function doing that.


Jassscript would be like

func convertTo2Digits takes string input returns string

and then check with "if" and return formatted.
would suit better to any code than GUI.


Still this would not be perfect, that's why I asked if I missed sth.
 
Level 3
Joined
Apr 1, 2019
Messages
48
Yeah I'm not sure if it spares me anything to assign a rect, a percentage (real), a timer and a unit type to a limited number of units.
The number is literally designed from start, so I think, it takes the same amunt of variables and design to assign the variables to the certain unit.
As I use the unit type as the personal intenger, which each unit can have.

Additionally I'm not into hash tables for jassscript.


However this is more about if there is any prebuild function to add a leading zero for too small integers or reals when converting them into string.


PS: If you think we may have a language in common and that could eeez up things, concern writing private.
 
Level 13
Joined
May 10, 2009
Messages
868
Here's a function that does what you want, in JASS
JASS:
function SecondsToMinutes takes integer seconds returns string
    local integer minutes
    local string s = ""
 
    set minutes = seconds / 60
    set seconds = ModuloInteger(seconds, 60)
    // Since I want it to look like this: 00:00, we need to take into consideration values
    // that are less than 10. In such case, add a "0" before converting the number into string
    if minutes < 10 then
        set s = s+"0"
    endif
    // Then, the minutes
    set s = s+I2S(minutes)+":"
    if seconds < 10 then
        set s = s+"0"
    endif
    // At last, the seconds
    set s = s+I2S(seconds)

    return s
endfunction
However, since you just care about adding an extra "0" to the seconds, you can remove this if block
JASS:
    if minutes < 10 then
        set s = s+"0"
    endif
->
JASS:
function SecondsToMinutes takes integer seconds returns string
    local integer minutes
    local string s = ""
 
    set minutes = seconds / 60
    set seconds = ModuloInteger(seconds, 60)
 
    // Minutes
    set s = I2S(minutes)+":"
    if seconds < 10 then
        set s = s+"0"
    endif
    // At last, the seconds
    set s = s+I2S(seconds)

    return s
endfunction
Copy and paste the code above into your map header in the trigger editor.

Keep in mind the function returns a string value, so you need to assign it to a string variable, then use it within the floating text action.
  • Custom script: set udg_tmpString = SecondsToMinutes(R2I(TimerGetRemaining(udg_Timer)))
  • Floating Text - Change text of TXTAG to tmpString using font size 10.00
Capture.JPG
 

Attachments

  • ConvertSecondsToMinutes.w3x
    17.9 KB · Views: 12
Level 3
Joined
Apr 1, 2019
Messages
48
He!, he doesn't want use "if"
Thank you for your intention to help me and for your post.

HerlySQR is right, that solution of yours is previously mentioned by myself and I asked if there is no simplier prebuild way.
By prebuild I mean, that there is already something that does format an integer or real so it would add a leading zero if the string would be too short.
Or another simply way?


Anyway, still thank you.
I just wait a few hours and then do a jassscript and post it, if there occurs to be no simplier way.
I thought I missed something.
 
Level 13
Joined
May 10, 2009
Messages
868
He!, he doesn't want use "if"
That's the simplest way of solving his problem. Also, I think he means that he doesn't want to add IFs within his GUI trigger, not the general use of it.

I asked if there is no simplier prebuild way.
By prebuild I mean, that there is already something that does format an integer or real so it would add a leading zero if the string would be too short.
Or another simply way?
No. there isn't one.

GUI is quite limited, and you'll find yourself struggling most of the time if you choose to go just with it.

You can modify the JASS function above to take a floating text and a timer, then manipulate everything there, which will make your GUI trigger look cleaner.
 
Level 3
Joined
Apr 1, 2019
Messages
48
Yeah, as previously said, I will be adding and posting my jass script, which would format a time which has less than 2 digits to a time which as 2 digits.
So I will add a jassscript which will take a String and returns a String with 2 digits, which has a leading zero if that string would have less than 2 characters/digits.

As this is the easiest solution I can think of, unless I missed something or screwed up.
And I thought I did.
But you encourage me, thank you for your interest.
 
Level 3
Joined
Apr 1, 2019
Messages
48
Well I now wrote this very small function which will do for my purpose.
vJASS:
function Int2FormattedString takes integer toBeFormatted returns string
    local string output
    set output = I2S(toBeFormatted)
    if (StringLength(output)<2) then
        set output = "0" + output
    endif
    return output
endfunction

Just wanted to share it with you in case someone is googling the question.
However, if you want to achieve another format, there is only little to be done.
 
Last edited:
Level 24
Joined
Jun 26, 2020
Messages
1,853
Well I now wrote this very small function which will do for my purpose.
vJASS:
function Int2FormattedString takes integer toBeFormatted returns string
    local string output
    set output = I2S(toBeFormatted)
    if (StringLength(output)<2) then
        set output = "0" + output
    endif
    return output
endfunction

Just wanted to share it with you in case someone is googling the question.
However, if you want to achieve another format, there is only little to be done.
He, in the end you decided to use the if, what happened to the idea that I told you, didn't it work?
 
Level 3
Joined
Apr 1, 2019
Messages
48
Oh, your solution should work too. It just makes the function longer again, as "seconds" are no string but integer.
  • Floating Text - Substring((0+Seconds),Length of Seconds, Length of Seconds + 1)
So this would work but would turn into a very long thing...
I now did use a local variable + if in one function which everything can use.

PS:
I just forgot about your solution...
 
Level 24
Joined
Jun 26, 2020
Messages
1,853
Oh, your solution should work too. It just makes the function longer again, as "seconds" are no string but integer.
  • Floating Text - Substring((0+Seconds),Length of Seconds, Length of Seconds + 1)
So this would work but would turn into a very long thing...
I now did use a local variable + if in one function which everything can use.

PS:
I just forgot about your solution...

Ah ok I see, well luck with your map.
 
Status
Not open for further replies.
Top