string formatting in zscript

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

string formatting in zscript

Postby ramon.dexter » Mon Sep 13, 2021 10:54 am

Hello all, I need a simple advice here.

How do I format a string, composed of a pre-defined string and a calculated integer? Let's say I need to display a value on statusbar, but I want the number to be preceeded by a number.
I have this code here used to display item weights in opened inventory bar:
Code: Select allExpand view
if (item.Mass > 0) {
   DrawString(mYelFont, FormatNumber(item.Mass, 3, 5), (103 + 35*i, 38), DI_TEXT_ALIGN_RIGHT, Font.CR_GREEN);
}

With "FormatNumber(item.Mass, 3, 5)" resulting in simple number like '27'.
All I need to know is how do defined the string to display 'W: 27'?
I just don't know how to define the string with pre-defined "W:" and the FormatNumber(item.Mass, 3, 5) following.
I'm little slow in this part and zscript offers toomuch possibilities on how to achieve this, so I'm asking for an advice on how to make this.
User avatar
ramon.dexter
rudebwoy
 
Joined: 20 Oct 2015
Location: Kozolupy, Bohemia
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support

Re: string formatting in zscript

Postby Jarewill » Mon Sep 13, 2021 11:01 am

With ZScript you can combine strings by putting ".." between them.
For example you could try this:
Code: Select allExpand view
"W:"..FormatNumber(...) 
Jarewill
 
 
 
Joined: 21 Jul 2019

Re: string formatting in zscript

Postby ramon.dexter » Mon Sep 13, 2021 11:08 am

Many thanks, jarewill. Exactly what I was looking for.

But I need to note one pretty important thing. Took me a while to notice that it's not working as intended because the font I use for the small number lacks letters...so, if you use this, use a font containing both letters and numbers. Otherwise it will just not work.

edit:

Ummm....I really, really, really have to start reading documentation. Or even the hints displayed by slade... :D
The function FormatNumber() has the string prefix built in... :shock: :D

Code: Select allExpand view
FormatNumber(item.Mass, 3, 5, 0, "W:")


Silly me! :D
User avatar
ramon.dexter
rudebwoy
 
Joined: 20 Oct 2015
Location: Kozolupy, Bohemia
Operating System: Windows 10/8.1/8/201x 64-bit
OS Test Version: No (Using Stable Public Version)
Graphics Processor: nVidia with Vulkan support


Return to Scripting

Who is online

Users browsing this forum: No registered users and 0 guests