I’m learning Unreal these days, and I managed to make a door that opens and closes when you press E. It also gives a prompt saying “open/close”. But I wanted to take things a step further and have it say “open” when the door is closed and “close” when the door is open. I’ve been trying for the better part of four hours to get this to work with no luck… what should I change to make this happen?
A tips is to always share your BP to show how much you have so far. That makes it much easier for others to help
What’s up, bro.
What you are describing is not exactly an “Unreal Engine thing”. This is more about HOW you program.
One simple way to do this is to have a bool variable on your Door Blueprint that stores if the Door is currently opened or not. Then, whenever a player interacts with, you first check on this bool value to determine what your logic will do next.
But since you want to learn something new (and if you are not afraid of a bit of overkill) , you may try challenge yourself a bit more with this:
If you want your code to take different routes depending on which state your object is in (In your case, If it is opened or if it is closed) you can also create a Finite State Machine.
In short: a FSM is a design pattern, not a tool in Unreal Engine, that allows you to give your object different “modes”.
This is simple enough to replicate in Unreal Engine with the help of Enumerators or if you are looking for something more complex, State Trees.
But what this really comes down to is to HOW you program your actor.
Try to start like this:
- Create a new Enumerator named E_DoorState.
- Add 3 entries to it: DS_None, DS_Opened, DS_Closed.
3, In your Door blueprint, add a new variable named DoorState. As its type, you want to select the E_DoorState enum type you just created. - In your Door blueprint add a new event to the EventGraph named Interact.
- Start the Interact event logic by adding your new variable ( get DoorState) and using a Switch node on its value. Now you will see 3 exists. One for each entry of your enum.
- Now you need to create a form in which the player can use an input to interact with the door. And this is really up to you. There are many forms to do this and is not really complex.
- When you have the Interaction logic ready, make the Player call this Interact event of your Door blueprint and voila. You can decide what to do depending on which state your door is currently in.
This is a basic demonstration of Finite State Machines, but you will come to learn that this design pattern rules in Unreal Engine (at least in my experience).
Anyway, sorry for the overkill answer and I wish you a great journey ahead.
Take care, bro!