Help with Additional Variables to affect story progression

Besides the player choice between the next scenes (ex. 1. Move to room A. 2. Move to room B.) I’d like to have an additional variable that would affect the story, based on previous choices. For example, if the player made a choice that killed their friend, it would change a the variable FriendAlive = True to FriendAlive = False. Then when the method is called to select the next room, it would check to see if FriendAlive = True and send them to a room based on that.

My main questions are:

Where would I store the variables? Would it be within the states.cs script, or adventuregame.cs script?

How would I adjust the method to be appropriate for each new state? Is there a way to attach the method to the state, so that it uses whichever method I’ve made to advance?

Any help would be greatly appreciated!

Hi! Welcome to the community!

My suggestion would be to create a new branch in the plot since that’s basically what you described there’s no need to over-complicate the code, just arrange your branches and plot so that previous actions remain relevant.

I’ll answer your questions if you want to push your idea:

  1. I would create a new script to hold that information. If you want to use an already created script then go for “AventureGame” script.

  2. Add another condition in your “if” statements that check for the variable.

  3. What do you mean by “attach the method to the state”? Are you planning to create a method for each state? If that’s the case, yes, you can do that, add a string variable to the state, then, in the “AdventureGame” script, use the command “Invoke” to call the method using the appropriate string. I don’t suggest this approach because you’ll end with 20 methods that are pretty much the same.

I’ll give you pseudo-code, hope this helps you:

  • Check which button was pressed.
    • Check for “friendAlive”.
      • If false load X state.
      • If true load Y state.

If you are planning on having multiple variables and scenarios, you could use a Switch statement instead.

1 Like

That is super helpful and will definitely get me started on the right path. I was going to have multiple scenarios, and most likely 4 total variables. I was going to do a simple “skill system” where they player gets to choose what the character is good at at the beginning (strength, stealth, etc.) and also the variable if they save their companion.

I’ve got the paths written out and the variables necessary for each one, but from looking at it, the requirements to move down different paths may vary. Sometimes I may have 3 paths instead of 2, or some options may require 2 true variables (strength skill + Friend Alive). My guess is I will need multiple different methods for each of these, but some requirements will overlap and I can reuse those methods (maybe?). That’s why I think I need a way to attach a method to the scenes, to make sure each scene is using the correct method.

I have around 80 scenes for the basic story and am planning on adding a few more, so I expect it to be a little complicated XD.

Thanks again for the guidance, I’m really enjoying this!

EDIT: I was re-reading your post and was thinking about it the branching paths options. If at the beginning of the story I had 3 choice path, where the player chose a skill, would I then have to triple the amount of scenes I have? It would remove the need for the variables I think, but I don’t know if it would be worth having to make so many scenes. Of course I could re-use some of the story writing, but I’d still need separate scenes with the proper paths attached. At the very least it would take it from around 80 scenes to 240, so I don’t know if that would be more efficient than working out the variable option with different methods. Anyways, thanks!

Yes, the issue with the first recommendation of simply implementing more branches requires a lot of work but prevents issues with the code.

I suggested that because having multiple branches by code can become quite cumbersome for beginners; if you truly want to go super deep into this then I suggest you take a look at this;

These types could help you organize your code better and make it far more flexible. You’ll learn about them later in the course.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms