Concatenating text

Hi,
I’m looking for a way to build a text string in a series of steps as I progress through a rule book, but I don’t seem to be able to find the correct syntax to combine two texts together.

The idea is that I have an action with an action variable that is some text, and that as the carry out rules are processed they add a little bit of description (as well as doing other things, in this case increasing the “score” of the action) that I can then play back at the report stage.

But when I try the (python inspired)

now shot report is shot report + " my extra text";

it doesn’t work. Is there another phrasing I need to concatenate two texts?

Thanks!

1 Like

Now the shot report is “[shot report] [my extra text]”.

This won’t work in your case because it’s an action variable, but often the better approach is to print out the pieces of text as you go, and wrap the whole thing in a “to say” phrase (like “to say my elaborate text”). Then you can get the whole thing as “[my elaborate text]” without building up a bunch of intermediate strings in memory that have to be allocated and deallocated.

4 Likes

One good trick to know: if you leave a blank space at the end of quoted text (such as after sentence-ending punctuation) in most cases Inform won’t automatically line break.

After taking the fabulous idol:
     increase the score by 10;
     say "You got the fabulous idol! Your score is now ";
     say "[score]! ";
     say "You're well on your way to solving this game!"

Screen Shot 2023-07-16 at 2.41.17 PM

You also have inform tags [no line break] and [run paragraph on] within text which I think can be manipulated to concatenate between individual rules, although sometimes it can be fussy:

5:8 Line Breaks and Paragraph Breaks

4 Likes

Thanks - I’m writing up a description of a game of (English) billiards, where the order the balls collide in makes a difference to the final score in various complicated ways. I’m not modelling the whole game, and the player is meant to be rubbish at it. The actual shot is is comprised of a series of random events “does it hit”, “is it a cannon”, “is the red boll potted” and the like. I have a series of to say statements that creates the text describing each event, but was looking for a way to merge them together while tallying up the score. I’ll have another think about whether I could do this with some temporary variables in the carry out rules and a big to say statement in the report stage!

2 Likes

in the specific implementation, you can look up a table with the random number, store the random number in another table (array) then you can use the extracted number table in building the tally reporting. You also have the tally record realistically chronologically ordered…

Best regards from Italy,
dott. Piergiorgio.

1 Like

That’s an interesting idea, I had started thinking about this in terms of a table, but of the complete final string. Breaking it down into components I can look up, and then constructing an array as I determine the shot outcome could work. That would allow me to separate the number crunching “carry out” part of the shot from the report - and even given me something to interrogate to determine onlooker’s reactions…

Is there a reason to build the array in a table rather than a list though?

So I’ve managed to get it to work, with a combination of your various suggestions. For those interested, here’s the code I’ve come up with. The key turned out to be the table suggested by @Piergiorgio_d_errico, and saying the bits of text when I wanted them printed, as suggested by @Draconis .

Still needs a little bit of refinement in terms of what is printed as the shot report (although I do intend it to be a little mysterious as to how the scoring is actually working), and to adapt the carry out rules for the skill levels of the various characters, but the proof of concept is working.

Thanks once more for all your input - it definitely helped get my thinking straight.

The games room is southwest of the dining room.

The billiard table is in the games room. Some pockets are an open container. The pockets are a part of the billiard table. Understand "pocket", "top left/right/-- pocket", "centre left/right/-- pocket", "bottom left/right/-- pocket", "center left/right/-- pocket" as the pockets.

A ball is a kind of thing.
Before listing contents: group balls together as "billiard balls".

A plain white ball, a spotted ball and a red ball are balls on the billiard table.
The description of the plain white ball is "A plain white billiard ball."
The description of the spotted ball is "A white billiard ball with a single red spot."
The description of the red ball is "A red billiard ball."

Section - the billiards game

A person has a ball called the cue ball. The cue ball of the player is the spotted ball.

The billiard cue is in the games room. The description of the cue is "A long stick of wood, that you are far more familiar with in the context of pool. And after at least three or four pints."

Billiards game is a recurring scene. Billiards game begins when the player is in the games room.

Before going from the games room when Billiards game is happening:
	say "It would be rude to leave right now." instead.

When billiards game begins:
	now the player has the cue.
	
Section - taking a shot

Taking a shot is an action applying to nothing. The taking a shot action has a number called the points.

Understand "take a shot" as taking a shot.

Check taking a shot (this is the can't take a shot if not playing billiards rule):
	if billiards game is not happening, rule fails.

Check taking a shot (this is the you need the cue to play rule):
	if the player does not have the cue, rule fails.
	
Check taking a shot (this is the must be in the games room to play rule):
	if the player is not in the games room, rule fails.
	
Carry out taking a shot:
	blank out the whole of Table of Shot Outcomes;
	choose row 1 from the Table of Shot Outcomes;
	now red potted entry is false;
	now plain white potted entry is false;
	now spotted potted entry is false;	
	[determine which balls were hit]
	Let L1 be the list of balls;
	remove the cue ball of the person asked from L1;
	now the striker entry is the person asked;
	now the first ball entry is a random ball that is not the cue ball of the person asked;
	remove the first ball entry from L1;
	if a random chance of 1 in 3 succeeds:
		sort L1 in random order;
		now second ball entry is entry 1 of L1; 
	[did the red get potted]
	if first ball entry is the red ball:
		if a random chance of 1 in 3 succeeds:
			now red potted entry is true;
	else if there is a second ball entry and the second ball entry is the red ball:
		if a random chance of 1 in 3 succeeds:
			now red potted entry is true;	
	[did the plain white get potted]
	if the cue ball of the person asked is the plain white ball:
		if a random chance of 1 in 3 succeeds:
			now plain white potted entry is true;
	else if the first ball entry is the plain white ball:
		if a random chance of 1 in 3 succeeds:
			now plain white potted entry is true;
	else if there is a second ball entry and the second ball entry is the plain white ball:
		if a random chance of 1 in 3 succeeds:
			now plain white potted entry is true;
	[did the spotted ball get potted]
	if the cue ball of the person asked is the spotted ball:
		if a random chance of 1 in 3 succeeds:
			now spotted potted entry is true;
	else if the first ball entry is the spotted ball:
		if a random chance of 1 in 3 succeeds:
			now spotted potted entry is true;
	else if there is a second ball entry and the second ball entry is the spotted ball:
		if a random chance of 1 in 3 succeeds:
			now spotted potted entry is true;
	[Score the shot]
	if there is no first ball entry:
		now points is -2;
	else:
		if there is a second ball entry, increase points by 2;
		if red potted entry is true, increase points by 3;
		if the cue ball of the person asked is the plain white ball:
			if the spotted potted entry is true, increase points by 2;
			if the plain white potted entry is true:
				if the first ball entry is the red ball:
					increase points by 3;
				else:
					increase points by 2;
		else:
			if plain white potted entry is true, increase points by 2;
			if spotted potted entry is true:
				if the first ball entry is the red ball:
					increase points by 3;
				else:
					increase points by 2;
	
Report taking a shot:
	Repeat through the Table of Shot Outcomes:
		if the striker entry is the player:
			say "You lean nervously over the green baize, gripping your cue in your sweaty palms. Thankfully you make contact with [the cue ball of the player], which ";
		else:
			say "[striker entry] confidently hits [the cue ball of the striker entry],which ";
		if there is no first ball entry:
			say "[heads towards] absolutely nothing. 'That's a foul!' announces Simon cheerfully. ";
		else:
			say "[heads towards] [the first ball entry] and [collides with] it. ";
			if there is a second ball entry:
				say "[The cue ball of the striker entry] deflects at an angle before it [heads towards] and [collides with] [the second ball entry]. 'A canon!' exclaims Simon. 'Well played!' ";
			say "After colliding, the balls continue to roll and ";
			if the red potted entry is true:
				say red ball sunk;
				if spotted potted entry is true or plain white potted entry is true:
					say ", while at the same time ";
			if the plain white potted entry is true:
				say plain white ball sunk;
				if spotted potted entry is true:
					say ". Also, ";
			if the spotted potted entry is true:
				say spotted ball sunk;
			if the red potted entry is false and the plain white potted entry is false and the spotted potted entry is false:
				say "eventually come to a rest. 'Hmm no hazards this time,' frowns Simon";
			say ". 'That's a total score of [points][if points is less than 0], which goes to the other player[end if],' Simon says, marking it up on the blackboard with some chalk. [if points is greater than 0]'It's still your shot.'[else]'That completes your turn.'[end if]";

	
To say collides with:
	say "[one of]richochets off[or]collides with[or]hits[or]knocks against[or]kisses[purely at random][run paragraph on]"
	
To say pocket:
	say "[one of]top[or]centre[or]bottom[purely at random] pocket[run paragraph on]"
	
To say falls in:
	say "[one of]falls[or]nestles[or]plops[or]kisses the pocket mouth before falling[or]goes[purely at random] in[run paragraph on]"

To say heads towards:
	say "[one of]bounces off a cushion[or]heads directly[or]rolls inexorably[purely at random] towards[run paragraph on]"
	
To say cannon into (first ball - a ball):
	say "[collides with] the [first ball], before it [heads towards] and [collides with] the other ball[run paragraph on]"
	
To say (potted - a ball) sunk:
	say "the [potted] [heads towards] the [pocket] where it [falls in][run paragraph on]"
	
Table of shot outcomes
striker	First ball	Second ball	Red potted	Plain white potted	spotted potted
a person	a ball	a ball	true	true	true
--	--	--	--	--	--
with 1 blank row