May someone give me advice on how to optimize this system?

The Explanation
I’m working on an advanced first person movement, where player can execute many actions defined by an enumeration. Those enumerations determines what the player is exactly doing in that moment, like walking, running or just idle, and that is then used for movement logic.
Each movement should be executed one at a time, and should be always accessible without the need to pass by another first. I made the following system, consisting of a macro, pin-connected to the input axis:

The system is just a bunch of branches checked every time to see if their condition is true, else the next one is checked till the very end. For example, this part here checks if player is walking by comparing the current speed with the MaxWalkSpeed variable, and then checking that the player is not executing any other movement:

The Question
The system works just fine, but I perceive it as a poorly made one, since it could be made in a better a cleaner way. I would like to get rid of all the branches, as well as the boolean conditionals, and determine in a more dynamic system which (enum) movement status is the player currently executing. I tried using MultiGate and ForEachLoop for this, but to no avail, as I don’t know how to use them in a correct way.

I’m not asking for an instant resolve, as it would be too much to ask for, but just for some tips or advice if possible. I’m still a very inexperienced programmer and I enjoy complex challenges like this, and sometimes I manage to resolve it on my own, but other times I just need the knowledge of more experienced people.

1 Like

I notice you have bools for crouching, crawling, running and flying, and then you’re converting them into a enumerated type.

Would it be better to just use the enumerated type for those states, and have one more, ‘unknown’ otherwise?

Rather than lots of bools, just have an enumerated variable for the player state.

lol I think in the opposite direction
What do you want enums for?
with bools you can create more options like crouch and walk

Oh, I forgot to state that “flying” is just a debug movement I used in other projects to press a key and make the player a “ghost”, so I could fly all over the level and go through solid stuff while playing, but now is more like a residual data with no real use.

I also have bools for idle and walking, but the problem is that without all the conditional checking mess, some bugs may appear while playing. For example, if you’re running and at the same time press crouching, whithout the bool the player will crouch but still keep the running velocity.

To be honest I don’t remember why or where did I get this system from, but some time ago I only use booleans, and then some gameplay bugs appeared when, for example, crouching and running at the same time and vice versa. That’s why I tried to make a solid system that checks which exact movement is the player performing.

I usually use pure functions to ask specific conditions. Not always a change of state needs to know all the other things. Tthere may be a better solution but this one suits me well

There’s also the venerable bit masks:

You could just use blendspace machine state, it much more cleaner and easier to understand if you need to comeback to it later, here mine where i modify mine a bit to make it a bit similar to yours:

1 Like

This is just what I was about to say… it’s what it’s for…

@Grimoriie What you’re looking for, to get rid of all those booleans and Branch nodes… is a Blueprintable Finite State Machine system.

Since you seem to be new to Unreal, if you want to, send me an email an I give you a copy of mine. My FSM have been used on several shipped PS4 games:

That can help you build state machines on blueprints while minimizing the chaos in your graphs.


Which games?