[SOLVED] Returning To The Previous Scene

Hello folks, I was wondering if you could help me out.

I’m on the verge of finishing quite a complicated text based adventure but have a slight issue. There are states in the game which I was hoping to be able to view from multiple locations in the story, like so.

Statement 1: You’re in bath, click R to view alien > you view alien , pressing R returns to statement 1.
Statement 2: You’re further into the story, click R to view alien > you view alien, pressing R returns you to statement 2.

Because of a mistake on my part the result is more like this:

Statement 1: You’re in bath, click R to view alien > you view alien , pressing R returns to statement 1.
Statement 2: You’re further into the story, click R to view alien > you view alien, pressing R returns you to statement 1 instead, erasing a significant amount of your progress on the spot.

I know how I can fix this - by simply duplicating the alien statement, giving them slightly different names and so keeping the statements on their own branches - but that’s messy, and could potentially end up with a lot more code then I need in some areas because of other stuff I have planned.

You could also give options to go back to either story section - but that causes various problems of it’s own in writing and game flow.

So I was wondering if there were a way to simply return to the previous scene? A function by which, pressing the same button, you could return to the previous statement no matter what it was? For now I’m using the above duplication workaround, but as I say, when it comes to initializing a future part of the game it could result in hundreds of extra lines (it’s a fairly big game).

Anyway, any help would be gratefully received.

One potential solution I can imagine would be to make use of two new variables, ex:
private States prevState; private States tempState;

Then any time you’re pushing a button to update myState, you would have to make sure to set prevState to the old value of myState, ex:
if (Input.GetKeyDown(KeyCode.A)) { prevState = myState; myState = States.alien; }

Finally, anytime you do the “Press R to Return” bit, it would look like this:
if (Input.GetKeyDown(KeyCode.R)) { tempState = myState; myState = prevState; prevState = tempState; }

The main downside I see to that solution, is that you’re adding a fair bit of extra code to manage the value of “prevState”, to make sure it’s always accurate, but it does make the return bit seem less confusing. It would also make the logic diagram in the Design Document a little less obvious to follow, since you’ve got “one state” which is actually flowing to multiple different Return paths depending on the state that you reached it from, so it’s sort of breaking the way those diagrams are supposed to work. I.E. That one state is essentially really multiple states, because you’re using that code trick to update its pathing based on how it was reached.

Anyway, hopefully that helps. I haven’t tried implementing any of the code I typed above, but it all seems to make sense as a potential solution. I used tempState, as I couldn’t think of a more elegant way of swapping the values of myState and prevState.

2 Likes

Hi Derrick.

Thank you very much for that solution - initial tests seem to indicate it works, which is great! And with the clean up it doesn’t add any code either, with it being held all in the same line. Thank you very much for that.

I am confused about one thing though. That is the role of tempState in this. Initially we’re setting the previous state as the same as myState - saving the state to be used in the next scene before we move onto it. Shouldn’t it then just be a question of using myState = prevState without any other need for lines. Testing seems to indicate that latter option works too. So what is the role of tempState in this particular set up?

Thank you again for the help so far.

The point of the tempState bit is that I wanted to exactly swap around the values of myState and prevState. Whichever of those two you’re updating first, you’d be unable to update the other one unless you’ve kept its original value around somewhere, such as in that temp variable: tempState.
Ex: if myState is alien, and prevState is hallway, we want to end up with myState being hallway and prevState being alien. I.E. We’re allowing for two-way Returns that way.

It’s my understanding that you won’t ever have two rooms both of which have a “Press R to Return” back into the other room? If so, that’s how you’re able to get away with only using myState = prevState, since it isn’t really important for you to have prevState getting updated once you’ve done that one-way Return.

And glad I could be of some help. :slight_smile:

1 Like

Privacy & Terms