STRINGS and variable in script

Discuss the new AI features ("NoAI") introduced into OpenTTD 0.7, allowing you to implement custom AIs, and the new Game Scripts available in OpenTTD 1.2 and higher.

Moderator: OpenTTD Developers

Post Reply
hpfx
Engineer
Engineer
Posts: 43
Joined: 09 Nov 2013 00:19

STRINGS and variable in script

Post by hpfx »

Hello,
I am working a a gamescript, and I have issue to understand something about variables GSText.

First of all, I want to display information on GSTown (SetText method)
information will be dynamically generated : the number of lines and content of lines themselves.
to begin, let's say we have two kind of lines only to display, I maintained in english.txt file

Code: Select all

STR_TOWN_CARGO_FIGURES	:{YELLOW}{CARGO_LIST}{BLACK} n:{BLUE}{NUM}{BLACK} n-1:{BLUE}{NUM}{BLACK} n-2:{BLUE}{NUM}{BLACK}
STR_TOWN_BONUS_INFO	:{GREEN}BONUS {NUM}{BLACK}
STR_TOWN_EMPTY_LINE	:{LTBROWN}- - - - - - -{BLACK}
in my code, I can easily fill these strings, for example

Code: Select all

GSText(GSText["STR_TOWN_BONUS_INFO",100])
and
cargo=1<<cargoid;
q0=100; q1=100;q2=100;
GSText(GSText["STR_TOWN_CARGO_FIGURES"],cargo,q0,q1,q2);
now, let's say I would like to combine these two lines into one text.
I have to create a {STRING}{}{STRING}{}{STRING}... text, but then I must take into account the number of variable I am using in sub-texts,
let's say STR_TOWN_CARGO_FIGURES lines will be used first... it consumes 4 variables (cargo,q0,q1,q2), then I should use {STRING4}.

Code: Select all

STR_TOWN_ALL_LINES	:1.{STRING4}{}2.{STRING4}{}3.{STRING4}{}4.{STRING4}{}5.{STRING4}{}6.{STRING4}{}7.{STRING4}{}8.{STRING4}{}9.{STRING4}{}10.{STRING4}
Let's start with easy need :I have to use STR_TOWN_ALL_LINES to display two STR_TOWN_CARGO_FIGURES line, and filling with STR_TOWN_EMPTY_LINE the 8 remaining lines.
but you see STR_TOWN_EMPTY_LINE wont consume 4 variables... and what when I will need to add a STR_TOWN_BONUS_INFO line ?
here is an example of code

Code: Select all

	line[0] <- GSText(GSText["STR_TOWN_CARGO_FIGURES"],1<<0,100,100,100); // 0:passengers
	line[1] <- GSText(GSText["STR_TOWN_CARGO_FIGURES"],1<<2,200,200,200); // 2:mails
	i=2;
	while(i<=9)
	{
		line[i++] <- GSText(GSText["STR_TOWN_EMPTY_LINE"]);
	}
	local txt=GSText(GSText["STR_TOWN_ALL_LINES"],line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]);
	GSTown.SetText(townid,txt);
looks to work, but it doesn't
result is

Code: Select all

1. Passengers n:100  n-1:100 n-2:100
2. Mails n:200  n-1:200 n-2:200
3.- - - - - - - - -
4.- - - - - - - - -
5.
6.
7.
8.
9.
10.
as you can see only lines 1 to 4 looks to be correctly displayed.
this is probably because STR_TOWN_EMPTY_LINE does not consumes 4 variables,

That's already weird...
remember I will need to have non-homogeneous line (bonus with only 1 parameter)


BUT... it's not the only issue,
I don't need to go further...


when using more STR_TOWN_CARGO_FIGURES string lines we have other issues:
using 5 STR_TOWN_CARGO_FIGURES => the whole GSTown.SetText does not work, no error, simply no action.
using 4 STR_TOWN_CARGO_FIGURES => the STR_TOWN_EMPTY_LINE are pushed back to line 10,
see:

Code: Select all

	line[0] <- GSText(GSText["STR_TOWN_CARGO_FIGURES"],1<<0,100,100,100); // 0:passengers
	line[1] <- GSText(GSText["STR_TOWN_CARGO_FIGURES"],1<<2,200,200,200); // 2:mails
	line[2] <- GSText(GSText["STR_TOWN_CARGO_FIGURES"],1<<1,100,100,100); // 1:alcohol
	line[3] <- GSText(GSText["STR_TOWN_CARGO_FIGURES"],1<<3,100,100,100); // 3:chemical
	i=4
	while(i<=9)
	{
		line[i++] <- GSText(GSText["STR_TOWN_EMPTY_LINE"]);
	}
	local txt=GSText(GSText["STR_TOWN_ALL_LINES"],line[0],line[1],line[2],line[3],line[4],line[5],line[6],line[7],line[8],line[9]);
	GSTown.SetText(townid,txt);
result is

Code: Select all

1. Passengers n:100  n-1:100 n-2:100
2. Mails n:200  n-1:200 n-2:200
3. Alcohol n:100  n-1:100  n-2:100
4. Chemical n:100  n-1:100  n-2:100
5.
6.
7.
8.
9.
10.- - - - - - - - -
strange void line 5 to 9...

I don't know what is wrong, you help is welcomed.
Sorry for long message, I tested as much as I could before asking your help here...

I am wondering if there is a method maybe to "consolidate string and all its parameters" to make it fit into a simple {STRING} on higher level
something like that

Code: Select all

	line[0] <- GSText(GSText["STR_TOWN_FIGURES"]);
	line[0].SetParam(1,1<<0); // 0:passengers
	line[0].SetParam(2,100);
	line[0].SetParam(3,100);
	line[0].SetParam(4,100); 
	line[0].MakeThisStringSOLID_Please(); // I want to use line[0] as a {STRING}, not a {STRING4} ...
_dp_
Transport Coordinator
Transport Coordinator
Posts: 278
Joined: 18 Dec 2013 12:32

Re: STRINGS and variable in script

Post by _dp_ »

GS strings can only use up to 20 parameters total, so as each of your lines consume 5 (line+4params) you can only use up to 4 lines in town text.
Also, afaik there is no way to have different number of parameters in the substring but you can make a line with 4 string params and pass empty string to them all
Post Reply

Return to “OpenTTD AIs and Game Scripts”

Who is online

Users browsing this forum: No registered users and 36 guests