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.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
ramon.dexter
Posts: 1254
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

string formatting in zscript

Post by ramon.dexter »

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 all

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.
Jarewill
Posts: 1334
Joined: Sun Jul 21, 2019 8:54 am

Re: string formatting in zscript

Post by Jarewill »

With ZScript you can combine strings by putting ".." between them.
For example you could try this:

Code: Select all

"W:"..FormatNumber(...) 
User avatar
ramon.dexter
Posts: 1254
Joined: Tue Oct 20, 2015 12:50 pm
Graphics Processor: nVidia with Vulkan support
Location: Kozolupy, Bohemia

Re: string formatting in zscript

Post by ramon.dexter »

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 all

FormatNumber(item.Mass, 3, 5, 0, "W:")
Silly me! :D

Return to “Scripting”