Download

Player States

Hey guys its HelloWorldTerabyte (again :slight_smile: ,

So I was making this sprinting system, just the other day. Everything is working fine, stamina and all that. But everything is controlled in a simple easy to break booleans. So for example I wrote if not stationary and the left shift is pressed(and some long stuff). Set this variable to true and stuff. Then on the Tick i changed the speed and controls and stuff. So what I want to now is do UE4 still have “States” like UE3 days. Or is there a another way I can make this safe and not break this easy.

There are player / game states, not really looked into using them yet. How is it breaking?

Thanks for replying Karner,

Well, The system doesnt break often. But i think it might be too unsafe. and if u hold down shift and control at the same time its walk slow but he looses stamina.Is there a better way to do this?

I use protected, replicated bools for states.

I had the same problem as you in the beginning: Even though I was standing still, stamina would still be drained as long as the sprint key was held down.
The key is to check more than one state. I made an RPC that checks for movement every tick and updates a replicated bIsMoving state.
So, in stead of just checking if char is bIsSprinting (which really just tells me if the key is held down or not), I check bIsSprinting && bIsMoving (you can also add a check for crouch here. I haven’t implemented that yet…).

This is handy, if you want to check character movement:



if (GetCharacterMovement()->Velocity.SizeSquared() > 5) {
	// Player is moving

} else {
	// Player is not moving

}


Remember to replicate the state vars, and to change states via server side RPCs…

thanks oasf, I already did that, the problem is that there is three functions one for walking one for jogging(Normal speed/ normal state) and sprinting.

I have a server RPC that modifies walk and crouched speed, as well as jumpzvel, based on states. I start out with normal speed, and modify this through if statements iterating through the states/combinations of states. At the end of the function I apply this to character movement and I can then use the same move function all the time.
Every time a state is changed, through a server RPC, I call the change speed server RPC to update accordingly…

The speed should be replicated automatically, with the normal replication of the component, I believe. Haven’t tested this properly, though.

I am pretty new to netcode, but it seems like this is a good way of doing it…

You could use an enum for this instead of bools if that helps. This way the player can only have one state at a time.

Thanks guys for the help :slight_smile: