How do I code a random action that does not repeat if the player remains in the location

I’m trying to add a dynamic element to my game (non-functional, but aspirational code shown below). I want the tiny monkeys to show up just once, or not at all, until the player moves to another “monkey accessible” location. If I remove the “and the tiny monkeys are not in the location” it will work but they can arrive multiple times while the player does stuff in the location.

Every turn when the location is Tiny Monkey-accessible and the fruit grove is visited and the tiny monkeys are not in the location and a random chance of one in two succeeds:
	say "With [one of]a clammor of chattering[or]catlike stealth[or]deafening screams[or]clicking and cackling[then at random], the monkeys arrive. They begin to [one of] groom each other [or]eye you suspiciously [or]munch on some fruit [then at random] .";
	move the Tiny Monkeys to the location.

1 Like

That looks right to me, as-is. When I try a small test case, it works correctly.

What’s not working for you?

Do you mean that you want it to be a 50:50 chance that the monkeys show up at all, rather than a 50:50 chance every turn that the player stays in the location after moving there?

If so, you could make it a last report going rule rather than an every turn rule:

Last report going when the location is Tiny Monkey-accessible and the fruit grove is visited and the tiny monkeys are not in the location and a random chance of one in two succeeds:

The query does raise a question that seems at though it ought to have an easy solution within the syntax of Inform itself, but I can’t immediately see it:

How do you count how many turns the player has been in the current location without setting up a new global variable or property to hold the value of ‘the location on the previous turn’ and another to either flag when it changes or count how many turns it has been unchanged e.g.

Yourself has a room called the previous location. Yourself can be static or mobile.
When play begins:
	now the previous location of the player is the location.
First every turn:
	if the previous location of the player is the location: [not moved this turn]
		now the player is static;
	else:
		now the player is mobile;
	now the previous location of the player is the location.
Every turn when the player has been static for at least 2 turns: say "Get a move on!"

Which seems like a lot of code to achieve something which feels as though it ought to be expressible in one simple phrase…

‘if the location was not the location’ won’t work to find out if the location has changed, because with this phrase the condition ‘location is not the location’ is considered exactly as written at the start of the current turn, and of course it’s never true that ‘X is not X’.

Plausible-sounding phrases like ‘if the player has been in the location for at least two turns’ have the same problem, because as Inform interprets this ‘the player is in the location’ is always true at the start of every turn…

I assume, without digging into the Standard Rules or Templates, that conditions in the past tense are actually asked in the present tense at the time they refer to (e.g. ‘if the location was adjacent to the Ballroom’ will have been asked as ‘if the location is adjacent to the Ballroom’ at the start of the current turn) and the result stored as a truth state, the parameters of the question itself being discarded and not available for later reference.

Thanks again for the reply. The issue is that if I’m in a location, and the tiny monkeys turn up, and then I do something like get something, the monkeys can turn up again.

I want the monkeys to show up (or not) but if they do, and I spend more than a turn in a location, they can’t show up a second time. It’s fine if they follow me from location to location (many places are tiny monkey accessible) but I don’t want them to show up a second time after conducting some action but remaining in the location.

Thanks for looking this over. I feel okay for having asked a “good” question, humbled for knowing so little, and bad for leaving empty cans and worms everywhere.

I’m trying this:

The Kitchen is a room.

The Fruit Grove is south of the Kitchen.

The Pantry is south of the Fruit Grove.

A room can be Tiny Monkey-accessible. 

The Kitchen is Tiny Monkey-accessible.
The Fruit Grove is Tiny Monkey-accessible.
The Pantry is Tiny Monkey-accessible.

The Tiny Monkeys are a plural-named animal.

Every turn when the location is Tiny Monkey-accessible and the Fruit Grove is visited and the tiny monkeys are not in the location and a random chance of one in two succeeds:
	say "With [one of]a clamor of chattering[or]catlike stealth[or]deafening screams[or]clicking and cackling[then at random], the monkeys arrive. They begin to [one of]groom each other[or]eye you suspiciously[or]munch on some fruit[then at random].";
	move the Tiny Monkeys to the location.

This works as expected. The monkeys only arrive once per location.

Thanks for doing that. I appreciate you taking the time to answer.
I’m looking over it now and comparing it to my code. I say “my code” but, actually, @zarf I think you might have helped me with these troublesome monkeys before!

So… did I get it right!?

I wonder what other stuff is messing it up for me…

This part of your rule should ensure that the monkeys cannot arrive more than once, after they have been moved to the location. Either the monkeys are not being moved to the location, or something elsewhere in your code is moving them away again…?

Try adding this rule and see what it says?

First every turn: say "The monkeys are [if the tiny monkeys are not in the location]not [end if] here.".

It should never report the monkeys being absent after they have arrived in a location…

…and they should not arrive when reported to be already present.

In a previous conversation, you’d posted a room description that mentioned the monkeys. That could make it seems like the monkeys are arriving even when the “tiny monkeys” object isn’t actually present.

If this

doesn’t solve your issue, you could try posting a sample transcript.

Oh – if you can take the monkeys, that will mess up the logic. They would no longer count as “in the location”.

(My version doesn’t have that problem because animals are non-takeable by default.)

You could fix this by using “enclosed by”.

I just want to say I’ve enjoyed seeing these bits of code about monkey-accessibility as you’ve brought them up. :monkey_face:

Ultimately the tiny monkeys can be saved from the island but I was going to change their “name” at the very end so that they could be taken. Until then they’re not takeable.