How do I use the same input key for different events happening in the level?

Hello everyone,

I have searched far and wide for an answer but haven´t really found what i´m looking for. So my problem is…

My level contains a lot of statues and I want the player to be able to go up to a certain object and press E. And when the player has pressed E a text pops up with info about this object. The question is, how can I use the same key multiple times without all the texts appear at once?

Can I use DoOnce in some way and tell it to be able to press E? Or if I disable the E input anywhere in the level and only allow the player to press E when entering certain areas (with triggerboxes)?
I also read that creating an Blueprint Interface could allow me to create a function for this? I don´t understand how it works yet though. Another thing I considered was using arrays? You understand my confusion? :wink: I guess there are many ways to solve this?

Thanks

You’re quite right in saying that there are many ways of doing this.

Here’s one:

I’ve placed some statues around the level - the statue is an actor with a cube that can simulate physics, there is also a Text Render.

The third person character has Sphere collision (made visible in this case) and the following script:

Image from Gyazo


If you need to be more precise and only interact with a statue that the players is looking at directly, you will need to linetrace ahead of the player and check whether there is a suitable object close by.

I’ve put this script in the character as it makes more sense than placing it in the statue or Level Blueprint. It’s the character that will do the interacting bit.

In the script you use Isvalid, but how
did you do to be able to get to choose
“Current Statue”?

One of the *OnComponentBeginOverlap’s pins returns Other Actor - this is the actor the character’s collision sphere comes in contact with. By casting to InteractiveStatue object, I automatically reject anything else that is not a statue. If I overlap something else, the cast will fail and it’s absolutely fine to leave it at that.

Current Statue is a variable of type InteractiveStatue I added to the character. As soon as the overlap happens, I store the reference of the statue I just interacted with. I need to store it because I want to hit E key later on and refer to this very statue I came in contact with. As soon as the overlap ends, this reference is cleared, so I cannot interact with the statues I’m not in range of. I used isValid during when hitting E - I need to check if there’s a statue nearby, otherwise I may apply physics force to a non-existing object - this will produce accessed none error.

You are creating the script under your
Thirdpersoncharacter and I tried to
make a reference to my object (static
mesh box) in there but that didn´t
work…? What am I doing wrong?

How did you try to create the references? You can only drag them from world to the graph in the Level Blueprint.

My statues are fully fledged actors. I created a statue actor and added a cube component. This is a preferred method as this gives you the option to add other elements to the statues, like additional functionality, text render, variables and more ways to interact with it. When you place one of them in the level, it becomes an instance in the world and can live their own virtual life.

[hit character limit…]

You could use just a component for this, sure, but that will be very limiting in the long run. If you do insist on this, though, you can modify the script I posted to use Other Component instead (of OnComponentBeginOverlap’s Other Actor pin).

Cool, thank you for helping! There is a lot in it that I didn´t understand though (i´m not that experienced). In the script you use Isvalid, but how did you do to be able to get to choose “Current Statue”? You are creating the script under your Thirdpersoncharacter and I tried to make a reference to my object (static mesh box) in there but that didn´t work…? What am I doing wrong?

In the script, what is it that makes me use E multiple times together with different objects (without, in my case, texts about the statues showing everywhere)? Is it the IsValid that makes it possible? Does IsValid in this case mean that the “CurrentStatue” is the only one that the player can interact with by overlapping the Sphere?

The E key is just one function, it’s not related to how many objects you have that will do something with that input.

In the example that he’s given he has a collision sphere around the character, when the character gets close to a box it sets a reference to that box as the “Current Statue” variable, that variable keeps track of the last box to enter the sphere around the player.

OnComponentBeginOverlap is an engine default event that gets called when something enters the given collision (in this case the sphere around the player) it has an output for “Other Actor” which is the actor that entered the sphere it’s what you want to store as “Current Statue” so that the game will know what is the thing you will interact with. In this case it will always be the last object to enter the sphere, there are other ways to do this to have better precision more on that at the end of this comment.

OnComponentEndOverlap clears the “Current Statue” variable by setting it to nothing whenever something exits the sphere collision.

On the E key press he first uses isValid to check if the “Current Statue” variable has a reference to something. If there’s no reference (not valid) it does nothing, but if there is a reference (valid) it does whatever it will do with the object stored in “Current Statue”, in this case adds an impulse.


For more precision you can go with a different way, for example a line trace when you press E. Basically when you key press make a line trace starting from the camera and going forward a certain distance (max distance for interaction with an object). When it hits the object you can call a function depending on what object was hit for example if it was a door you can call “open” if it was a statue you can call “inspect” etc you have a lot of freedom and if you use interfaces like you mentioned that makes it even easier to manage. This video covers some of the ways to do exactly that CWTH - Is it ok to do it wrong? - YouTube

I´m sorry it took so long to reply! I have had some personal issues. I also try to figure out it all and what I got to learn to get there to understand how you created the script. I hadn´t used variables before, so I need to learn that first and foremost. And then there are other things. I´m working hard on it.

It helped a lot with you two explaining so a big THANKS for that. I need to dissect each step and understand how I myself can do it and learn new stuff on the way.
I have more questions but will first try to solve and figure them out on my own (if i´m able) :wink: