Stamina System for UE 4.12

To anyone who reads this, I am a beginner and I really didn’t know what to do to get started so I started looking up tutorials. I came across one that was talking about adding a stamina system to a character, which I linked below, but it doesn’t seem to work for my character/version of UE 4 (I have 4.12, newest one). I want my character to slow down after a while when he runs and gain stamina back as he runs, meanwhile, different things occur, like the stamina system not decreasing at all, or the stamina decreasing too much, going into the negatives (that’s something else I experience too, I don’t want the value to pass into the negative values). If anyone could maybe try to help me with this, or even come up with the whole stamina system (You don’t have to if you don’t want to, but it would really help if you did), it would be really appreciated.

Here’s that video I was talking about: https://www.com/watch?v=lfqYvPZdjsg

Hi kingpanncake,

Firstly, welcome to UE4

I’ve been needing an excuse to make a tutorial video. I’ll download 4.12 and make a tutorial on how to do this when I get home. I’m just starting a new project so good timing.

I’ll post it here when I am done.

Thanks

Thanks !

This is my C++ implementation of a working stamina system, mostly inspired by Skyrim.

I know this is C++ and you’re looking for blueprints, but in my mind, they’re both “programming” and how you end up constructing the system is less important than the system design and implementation. You should be able to piece together a lot of the working parts here and use that to inspire your own design.

BaseCreature.h



//This is the current speed we would run at if we wanted to run as fast as we could.
	//This can be affected by environmental effects such frost and tar.
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Transient, Category = "Creature|Movement")
		float CurRunSpeed = MaxRunSpeed;

	//This is the default walk speed of the creature. This shouldn't change very often.
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
		float MaxWalkSpeed = 100;

	//This is the current walking speed we'd have if we walked as fast as we could.
	//This can be affected by environmental effects such frost and tar.
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Transient, Category = "Creature|Movement")
		float CurWalkSpeed = MaxWalkSpeed;

	/*This is how fast we're moving when we give movement input*/
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
		float MoveSpeed = 100;

	/*This is the most endurance we can have*/
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
		float MaxEndurance = 10;

	/*This is our current endurance. Some actions like sprinting uses this up.*/
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
		float Endurance = 10;

	/*Indicates whether or not the creature is burning endurance to move faster*/
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
		bool bSprinting = false;

	/*Indicates that the creature is exhausted. Exhausted creatures cannot perform actions which consume endurance.*/
	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Creature|Movement")
		bool bExhausted = false;

        UFUNCTION(BlueprintCallable, Category = "Creature|Movement")
		void TrySprint();


BaseCreature.cpp:




void ABaseCreature::TrySprint()
{
	if (!bSprinting && !bExhausted && Endurance > 0)
	{
		bSprinting = true;
		MoveSpeed = MaxRunSpeed;
	}
}

void ABaseCreature::Tick(float DeltaSeconds)
{

	Super::Tick(DeltaSeconds);

	//If we're sprinting, we try to move at our run speed. If we get exhausted, we reduce our speed to our walk speed.
	if (bSprinting)
	{
		
		if (Endurance == 0 && !bExhausted)
		{
			bSprinting = false;
			bExhausted = true;
			MoveSpeed = CurWalkSpeed;
			OnExhausted.Broadcast();
			
		}
		else
		{
			Endurance -= DeltaSeconds;
			Endurance = FMath::Clamp<float>(Endurance, 0, MaxEndurance);
			MoveSpeed = CurRunSpeed;
		}
	}
	else
	{
		//recover endurance
		if (Endurance < MaxEndurance)
		{
			Endurance += DeltaSeconds;
			Endurance = FMath::Clamp<float>(Endurance, 0, MaxEndurance);
		}

		//recover from exhausted state if we've recovered half our endurance
		if (Endurance >= MaxEndurance / 2.f)
		{
			bExhausted = false;
		}
	}

	
	GetCharacterMovement()->MaxWalkSpeed = MoveSpeed;
//other code removed...
}


Were you able to get the blueprints down?

yeah sorry. had some issues last night with getting UE 4.12. Ive got it now so ill do something tonight. Ill post it tonight (sorry in AEST time so its morning here :slight_smile: )

Well that was fun, GG.

I love BP’s but the C++ is just cleaner and performant…

teak

Your channel link is sorta messed up I click on it and it says the channel isn’t available

Here is the link of his channel:

https://www.com/channel/UCybw2PN_HIzgY_Yf-8pIldQ

Seems that one of his html tags didn’t convert properly. (I think he only needs to remove the whitespace in the tag).

thanks @DarkGodsLair Ill fix that now.

The video is currently uploading. Its a very quick video with no audio (due to all the background noise of living near a busy road). sorry bout the delay but this should help you.

video will be available when done at

Will probably take a couple of hours due to my internet connection.

Thanks I got it to work! Just one thing, for the - and + floats it was losing and gaining to slowly, so I raised it from 0.01 to 0.05. Does the trick! Thank you!

Hi,
If you really want to make game, do this tutorial: Tutorials | Shooter Tutorial
It is long and hard, but you will learn a lot…

Tactical_Beard - How did you make that all one giant image? Was that photoshopped together?
And just one correction on those BPs. It’s STAMINA ;D gave me a chuckle tho

Oh god how embarrassing lol. I used gimp, the windows snipping tool and zero english skills.
I snipped a small section of the grid and then tiled it to 1920 x 3000.
Everything else is on separate layers. It’s all fixed now.

“system” :wink:

I like your setup even better than mine. Going to have to use some ideas from that. Thanks.