concatenate text

Discussions about the technical aspects of graphics development, including NewGRF tools and utilities.

Moderator: Graphics Moderators

Post Reply
sky81
Engineer
Engineer
Posts: 45
Joined: 29 Oct 2019 05:49

concatenate text

Post by sky81 »

Hello,
Can anyone help me to concatenate 2 or 3 text strings in nml?
I've try with switches and load variables:
switch (FEAT_ROADVEHS, SELF, switch_Geo, [
STORE_TEMP(string(STR_GEO) ,0),
STORE_TEMP(string(STR_GEO1) ,1),
])
{
return LOAD_TEMP(0)+LOAD_TEMP(1);
}
I'm trying to show multiple strings in the purchase menu, but those strings are store separately in the lang file.
Thanks
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 990
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: concatenate text

Post by frosch »

1. First you need to add {STRING} parameters to the strings, where another string should be inserted.

1a. Either you add it at then end of the first string:

Code: Select all

STR_GEO      :...your text....{STRING}
1b. Or you add a generic string to concatenate two strings:

Code: Select all

STR_CONCAT   :{STRING}{STRING}

2. Second you need to decide, whether you want to concatenate the strings at compile time, or at run time.

2a. Concatenate at compile time:
This will occupy another string ID for the concatenated text. So if you have many different combinations of concatenations of the same strings, you may run out of string IDs.

For option (1a):

Code: Select all

return string(STR_GEO, string(STR_GEO1))
For option (1b):

Code: Select all

return string(STR_CONCAT, string(STR_GEO), string(STR_GEO1))
2b. Concatenate at run time:
For this you need to put the strings to insert onto the text stack (temp storage 0x100..0x10F).

For option (1a):

Code: Select all

return [STORE_TEMP(string(STR_GEO1), 0x100), string(STR_GEO) - 0xD000];
For option (1b):

Code: Select all

return [STORE_TEMP(string(STR_GEO) | string(STR_GEO1) << 16, 0x100), string(STR_CONCAT) - 0xD000];
Last edited by frosch on 05 Apr 2025 18:29, edited 1 time in total.
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
sky81
Engineer
Engineer
Posts: 45
Joined: 29 Oct 2019 05:49

Re: concatenate text

Post by sky81 »

Thank you for your answer.
But the command return string(str1,str2) is not compiling. I've also try return[(string(str1),string(str2)] this one compile but give error when try to execute.
Any ideea?
sky81
Engineer
Engineer
Posts: 45
Joined: 29 Oct 2019 05:49

Re: concatenate text

Post by sky81 »

Manage to concatenate 2 strings, but not 3.
return STORE_TEMP(string(str1) | string(str2)《16,256)
and in lang file refer to {STRING}{STRING}
But cound not concatenate 3 strings, try even with LOAD_TEMP: store first 2 string in a temp var, then loaded and conatenate the 3rd. But only 1st and 3rd where shown.
frosch
OpenTTD Developer
OpenTTD Developer
Posts: 990
Joined: 20 Dec 2006 13:31
Location: Aschaffenburg

Re: concatenate text

Post by frosch »

I edited my original post to fix the "compile time" case.

You cannot load/store a string with LOAD_TEMP/STORE_TEMP. You can only load/store string IDs. And adding string IDs makes no sense.

Each text stack storage can only hold two parameters. So to concat 3 strings at runtime, you need two storages.

For option (1b):

Code: Select all

STR_CONCAT3      :{STRING}{STRING}{STRING}

Code: Select all

return [
    STORE_TEMP(string(STR_GEO1) | string(STR_GEO2) << 16, 0x100), // first two parameters
    STORE_TEMP(string(STR_GEO3), 0x101), // third parameter
    string(STR_CONCAT3) - 0xD000
];
⢇⡸⢸⠢⡇⡇⢎⡁⢎⡱⢸⡱⢸⣭⠀⢸⢜⢸⢸⣀⢸⣀⢸⣭⢸⡱⠀⢰⠭⡆⣫⠰⣉⢸⢸⠀⢰⠭⡆⡯⡆⢹⠁⠀⢐⠰⡁
Post Reply

Return to “NewGRF Technical Discussions”

Who is online

Users browsing this forum: No registered users and 3 guests