Every Turn When...., but not first time

how would I go about creating something like this?

I want it so that while a player is in a room with an enemy, unless they deal with it, it will try to attack.

Every Turn while a enemy is in the location of the player:
     say "The enemy tries to attack you";
     if a random chance of 1 in 3 succeeds:
          say "It hits you, causing much pain!";
     otherwise:
           say "Thankfully it misses!";

something like that works great.
However it leads to a situation that is not ideal.

The moment you enter a room that has a enemy in, it launches an attack attempt.
Doesn’t even print room name or description of contents.
just goes straight to “The enemy tries to attack you”…

I’d like the player to be able to assess a room first, see it contains an enemy, and decide to deal with it or not.
I’d like enemies only to attack if a player is dawdling in a room that contains one, not as soon as they enter a room before they even have a chance to know there even is one. Bit unfair!

What would be the best way to achieve that?

The best way to handle exact timing like this is to use scenes.

For instance, you can do something like this:

EnemyNearby is a recurring scene. EnemyNearby begins when an enemy is in the location of the player. EnemyNearby ends when the number of enemies in the location of the player < 1.

Every turn when EnemyNearby is happening:
    if the time since EnemyNearby began is greater than 1 minute:
      etc.

The examples in this section of the documentation can help:
http://inform7.com/book/RB_4_2.html

(my code isn’t perfect, and can be simplified a lot, but scenes are designed for this sort of thing. The ‘recurring’ lets it happen over and over again, but you have to make it stop before it can start again, which is why I gave it an ending condition.)

If you don’t fancy using scenes, this is an even simpler approach for this particular problem:

Every Turn while a enemy has been in the location of the player for one turn:
     say "The enemy tries to attack you";
     if a random chance of 1 in 3 succeeds:
          say "It hits you, causing much pain!";
     otherwise:
           say "Thankfully it misses!";

although as written, if the player tries to avoid trouble by moving directly from one room with an enemy to another, the enemy in the second room could attack immediately because it will still be true that an enemy has been in the location of the player for a turn…

(Brian’s scene-based solution has the same potential drawback)

1 Like

Thanks! That was literally exactly what I was looking for.

Good to note about the drawback…I could do a check to see if it’s a different enemy first.

1 Like

PS you don’t need to write ‘the location of the player’ in full. ‘location’ on its own implies the location of the player.

1 Like

The enemy are guarding and patrolling ? that is, are static or move around the map ? and there’s other guards (that is, static enemies) in the adjacent locations ? this are, IMVHO, the point to be assessed in weighting the drawback…

Bates’s solution is excellent, and can even give a valid solution to the puzzle, if a stealth rushing over unnoticed makes sense in the story. (e.g. the guard is preoccupied only about one direction and don’t give much attention about the other directions)

Best regards from Italy,
dott. Piergiorgio.

By the way, I didn’t know you could write this, and I’m using it in my game right now. Thanks for posting this!

1 Like