How To Code A 'Return to Previous Room' Feature?

So after doing my Text 101 game I plan on making another where instead of just moving between game states as text sections with one or two options, you actually move between rooms in a house. Each room will connect to each of its neighbours, and you will need to find keys to progress and such.

But i’m not sure how to approach a few aspects. I want it to remember which room the player was in previously and then have a ‘back’ option to allow players to return to it.

Whats the best way to have the game ‘remember’ your previous state?

Cheers!

Hey,

Based on what you have described you could just keep a reference to the state that you last loaded.

So, each time you are about to change state based on a selected option, before you do, have something along the lines of;

private State previousState; 

as a member variable in your AdventureGame.cs script.

Then set it before loading the next state;

// handle player input 

previousState = currentState;

// set new state

Note, being that you add multiple states to a state for the options, you could just associate the one you want for them to go back that way too.

Hope this helps :slight_smile:

Hey, thanks for the info!

I shall give it a try :smiley:

No worries.

The approach you take may depend on some factors. For example, if a State is using an array of others states for your options, perhaps numbered etc, then you’d need to have the one that was going to be the back state always in the same place in the array, so you could guarantee which element it was in, I would recommend the first. However, if you do this, then the expectation is that every state will always allow you to go back,you.might not want that - for example, if you kill the player.

Have a think about the rules of your story and then choose an approach that fits would be my suggestion. :slight_smile:

Okay, I have tried to implement this, but don’t know enough about what i am doing, can you elaborate for me please?

Hi Jon,

Happy to, but which part/area specifically?

Thanks!
So, its giving me an error for ‘currentState;’ because it doesnt exist in the current content, do i need to add a;
private State currentState;
as a member variable as well?

and how do i make the previousState an option from within unitys inspector?

Hi Jon,

Ah I see. That was a very brief snippet above to convey the concept rather than a piece of working code, sorry for any confusion.

So, my understanding of the Text101 game is that AdventureGame.cs manages the game and you have unique scriptable objects which derive from a class called State.cs.

So,if I wanted to model moving around my home I would create a State for “Living Room”, “Kitchen”, “Hallway” etc.

I believe at the moment that when you create a state you can add other states to it via the Inspector which become the options for moving to other states based on player choice.

My suggestion was that you could use the existing model but always have one additional state which would be the one to take you back. So, if the array size for the options in one state is 3, set it to 4 and add the state which is the previous place they have come from.

I think the player options are numbered aren’t they? So, you might want to make sure that the “Back” option was always the first one in the list, if you did this you could then add some code to display “Back” as the option for the first item in the array.

The other way I was describing would require a member variable in your AdventureGame.cs script for the previous state as mentioned. The current state you probably already have access to though, whilst it may have a different variable name, it’s effectively the one that is currently loaded in.

If you would like to share your project files with me I can take a quick look and give you some more specific pointers, I’m talking at the moment from only glancing at the updated course content briefly and obviously am not fully aware of your project.

If you zip up your project files and share them I will happily take a look in about an hour.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Ah cool, yes okay so i have been making a state for every single room in the house and i have them all linked to each other so you can move around freely.
Instead of having a back option i have done as you suggested and simply made it so every room can access every other room that’s adjacent to it. It means that the player has to keep track of where they are and where they have been but it works for now, if i have every room unique enough to not confuse the player.

Thanks for the help, and i might send them over in a bit, but im just working through it at the moment.

I wanted to ask something else, if i wanted to update or change the text in a state whilst the player is in the ‘room’ whats the best way of going about it? If the player were to come back to the ‘living room’ for example, how could you get the text to say something different? would i need a new state that would replace that? like an updated living room state?

Thanks for you help so far!

1 Like

Hi,

Your more than welcome.

Regarding returning to the same room again, it depends.

If something has changed, then you would really want another state, so for example if there was a table the first time round, but the player breaks it, the second time it should state there is a broken table etc. That is really a state change. As you can imagine, with you back functionality, that is going to get very complicated, very quickly.

If it was about items in a room, perhaps things the player could take, then you could detain an array of items and add to it if they drop somethibg, remove from it if they take something and update a player inventory accordingly. These changes would need to be reflected to the store stare, e.g. the scriptable object.

You could use the abive approach for damage enable items as well I suppose, but this would need some careful thought, but may also work for characters you can encounter.

If it is more simplistic, which would probably be the best place to start, you could just have three story text properties, the first is displayed when the player first goes there, the second is displayed when they go there a second time, the third is displayed the third and every subsequent time. You would need to store the number of visits to that state within the state and use it as part of the check. This approach is more simplistic but does create a lot of duplication of data, as for the most part your description of the room will be the same.

You could retain the visit count and check it, but keep the text for the description of the room very simple, in such a way that it describes the room but doesn’t cause any silliness for future visits. You could then use your number of visits variable to include atmosphere. For example, the first time they visit the room perhaps you add, “Your lack of familiarity with the room has made you nervous and you want to leave as soon as possible”. The next time they visit, you don’t add that bit, because it wouldnt make sense, they have already been there. The esnippets to add or not add could be held in an array/list against the state object.

Hope this helps. :slight_smile:

Privacy & Terms