Getting a sequence of buttons to effect an actor

So I have a UI widget with 7 buttons

  • You can either click the button on screen or press buttons 1-7
  • they have an associated audio file as these are buttons for a musical instrument

i want to have an actor that would respond to a specific sequence

  • such as 1,3,5,2,4,5
  • this would have effects on the actor that the player is 10 m from it - such as destroy and actor that was acting as a barrier - or wake up an inactive portal

any guidance on how I may go about achieving this?

1 Like

it sounds like what you’re trying to do is a mechanic similar to what a Bard character would do if I’m not mistaken?

ANSWER PART 1 - I think the best way to handle this would be to use string variable that will record a sequence of button presses.
This way, the player can press a button, the number of that button is appended to the string. This process repeats until the player has reached the maximum combo length (in your example this was at 6). Once the string’s length is equal to 6, you can pass the string through a switch statement that will identify the string as a key for what action that you want to happen.
Instead of a max combo length you could make a key for the player to enter that would indicate to the game that the combo is over. For example: if the last three characters in the string are equal to “2,1,3”, cast the spell base on the previously entered numbers. You could also just make “7” the cast key which is simpler.

ANSWER PART 2 - Now to handle the casting to an actor that is 10 meters from it. I think the best way to handle this would be to create a sphere collision that has a radii of 10m with the player character at the center. Whenever the string in answer part 1 is casted, you can run this code to get all actors inside the collision sphere at that time, then apply the damage to all of them.


What this code does is it gets every single actor that is inside the player character’s sphere hitbox, the uses a for each loop to iterate through each of the pawns inside. It is an overlap detection algorithm. In this specific case is applies damage to the other pawns but you can change that to be whatever set of code you want.
Since you said destroying a barrier or waking a portal, I’d say you should do is add your switch statement logic that I mentioned earlier in part 1 to figure out what spell was used. Then use a cast node that is relative to the given spell.

In this example, when a spell of “1,2,3” is read, the program will try to cast to each actor in the sphere, but it will only succeed when the actors are of type BP_BaseEnemyAi (which is one of my pawn names). If a different spell is passed, it will run the other corresponding node. This way you will only run the code that is relevant to your spell, and apply it to all the actors of specified type in range.
One thing to remember is that casts act like if statements, so if the input object is of that type, the cast will succeed. If it is not, it will run the cast failed node. So you can also use this to try a cast to a different pawn with the same spell.

Another thing that might help with casting is using inheritance with child blueprints. This way when you cast to the parent blueprint it will work for all different child blueprints. It will also make your code more efficient. (If you aren’t familiar with inheritance I’d google it since this post is getting long and it shouldn’t be hard to figure out since it exists in all Object Oriented Programming languages)

Hopefully this helped, if there’s anything else that you’re still not sure enough feel more than free to ask and I will try to clarify as best I can :+1: Happy programming!

1 Like

Thank you for taking the time to answer.

Just working through this - one issue I’ve come across though - with collision sphere - I have collectables that get picked up on overlap with the player - issue with having a 10 m collision sphere is that they get picked up by the sphere - how can I overcome this

thanks!