Ending the story after a number of turns

Hi everyone, I’m very new to this IF stuff and struggling to get to grips with Inform 7.

I have a scene which involves a goblin running toward the player and the game will end (with the goblin killing the player) in a few turns unless the player carries out a specific action. I have tried many ways to achieve this but failed at each attempt.

The closest I’ve got is by doing something like this…

[code][Meet The Goblin scene]
During Meet The Goblin, goblin distance is a number which varies.

When Meet The Goblin begins:
let goblin distance be 30;
say "Suddenly you hear footsteps alternating clumsily between running and stumbling through the forest; twigs cracking beneath them and accompanied by a familiar congested breathing.

'The goblin!!' you anxiously mutter under your breath.";
say "Goblin distance is [goblin distance]".

Every turn during Meet The Goblin:
decrease goblin distance by 10;
say “The goblin is getting closer! [goblin distance]”;
if goblin distance is 0, end the story saying “You’re dead”.[/code]

But for some reason the “goblin distance” value seems to get reset to zero after the scene begins (you can see I’ve put in some debug code to check the distance value). So when the scene begins the line…

	say "Goblin distance is [goblin distance]".

shows “Goblin distance is 30”… as it should.

But then the line…

	say "The goblin is getting closer! [goblin distance]";

shows the goblin distance as -10… as if it has reset it to zero?!? I’m baffled by this, can anyone help explain to me what I am doing wrong?

Your assistance is greatly appreciated.

I find there’s always usually an easier way to do some things. What about this?

every turn while in the location: if the player has been in the location for exactly 10 turns: end the game in death.

Of course, you’d add descriptive text into this.

Then, you add the warnings with stuff like:

every turn while in the location: say "[one of]The goblin's getting closer.[or]etc.[or]etc[or]He has reached you![stopping]"

And just make sure your timing with the first part fits the descriptions of the 2nd part.

There are other ways to do it, but the problem with the way you tried is that you said ‘let’, which creates a temporary variable. You want a global variable, which you set using ‘now’. Replace ‘let’ with ‘now’ and see what happens.

Also, this line: ‘During scene, suchandsuch is a number which varies.’ - that’s actually creating a variable called ‘During scene, suchandsuch’. It compiles because ‘suchandsuch’ is an unambiguous reference to that variable. You can tuck a variable away on the scene object easily enough, but that’s not the way to do so.

Wow! Brilliant replies. Thank you so much!

I shall try both methods before opting which one to go for.