Undo and death tracking

In my game I have a bunch of ways for a character to die, but he should be able to undo and then solve the puzzle with no problem. However, I’m wondering if there’s a way to track how many times the character died and undos(undoes?)
Is it possible to do that, or does the way undo is structured make it impossible?

You can try the Undo Output Control extension, which allows you to attach rules to UNDO as if it were a normal action. UNDO isn’t a regular action, of course, so this is merely an imitation,–but it does allow you to say things like “before undoing an action” and “after undoing an action”. To keep track of the number of times the player has undone, you could write:

After undoing an action: Increase the undo-count by 1; rule succeeds.
This increases a global variable by 1 after the UNDO has occurred, which makes it quasi-independent of the state-change that the UNDO brings about (see below for a significant limitation, though). Now, this increases the count for all UNDOs, not just end-of-game UNDOs. You seem to want this to apply only to after-death UNDOs, and that’s more difficult because there is no appropriate time at which to tell the game when to increase the count: When the player is dead, the UNDO hasn’t happened yet, and any flag you set will be immediately undone (i.e., before the after undoing rule can kick in, that flag has already been reset). And when the player has undone, he is no longer dead and so we can’t query the game about whether the player has died…

One way you could get around this conundrum is by disallowing all UNDOs except the ones that take place after the game has ended. Both Undo Output Control and Conditional Undo allow the author to conditionally disallow UNDOs (they’re also compatible).

Whatever you do with an UNDO count, you want to be sure that the player can never UNDO twice in a row, since he will then destroy the UNDO count by rolling back to the previous state of the counter. Limiting UNDOs to once in a row can be done like so:

[code]After undoing an action:
Increase the undo-count by 1;
now the undo-flag is true;
rule succeeds;

Before undoing an action:
if the undo-flag is true:
say “Your powers over the space-time continuum only allow you to move time backward one turn.”;
rule fails;

Every turn when the undo-flag is true:
now the undo-flag is false;[/code]
To take an entirely different tack: If you’re compiling to Glulx, you can write an external file to the player’s hard drive with information about the undo count, etc. External files don’t depend on game state, so this would enable you to do anything you like. I believe external files are covered in Chapter 22 of the manual.