"Go back" command

I'm working on my first game and in trying to make sure that any reasonable command has a function I'd like to create a "go back" command that would move the player back into the last occupied room. So far the only solution I've found is to create a string of "if/then" statements that check the room the player is in and then move them into an adjacent room. Problem is this doesn't work for rooms with multiple entrances and it's not very elegant or efficient. Is there any way to create a script that tracks the players last occupied room so that I can reference it and send the player there when they type "go back"?


I would suggest making an attribute to track which room the player was last in.

There is a script on the "Scripts" tab of the game, "Script when entering a room".
You can use this to set an attribute to the current room when the player enters one; and use the old value of the attribute to set an attribute to the previous room.

So your "Script when entering a room" would be something like:

game.pov.lastroom = game.pov.currentroom
game.pov.currentroom = game.pov.parent

(Note that I use game.pov instead of player because that's a good habit to get into)

Then your "go back" command could be:

game.pov.parent = game.pov.lastroom

However, it would probably be better to check if the player can go back (so, for example, they can't go back if they were teleported to another room by magic, or if the path collapsed behind them).
Then you could look for an exit connecting those two rooms, and use the normal go command to determine if the player can go through it or set off any traps on it. It would look something like:

exitname = GetExitByLink (game.pov.parent, game.pov.lastroom)
if (TypeOf (exitname) = "object") {
  // If the player's last movement wasn't through an exit, then it will be "null",
  //    which is technically an object
  msg ("You don't know how to get back from here.")
}
else {
  do (go, "script", QuickParams ("exit", GetObject (exitname)))
}

This works! I'm very new to programing so I pretty much just copy pasted but I think I have a basic understanding of what's going on. The only issue I've been able to identify is if the player uses the "go back" command before having actually moved from the first room it causes a string of errors, likely because there's nothing for the script to reference when "going back". But it's pretty unlikely that a player would try this before even moving anywhere so it's not a huge issue. I'm gonna play around with it some more to see if I need to do anything else for my specific use case but this has been a huge help. Thanks so much!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums