Survival Sample Game in C++ for Unreal Engine 4

C++ Third-person survival game sample covering common gameplay and networking concepts for games in Unreal Engine 4.

Introduction

This series focuses on the C++ game development aspect of Unreal Engine 4. The goal is to introduce a variety of concepts written in C++. The project will help you to get you more comfortable using C++ for your own projects by using practical examples instead of relying on just theory. You may even use this project as a kickstart for your own project instead of having to start from scratch.

It’s important to note that this series is not a step-by-step tutorial. Instead each two weeks a new section is published, with all source and assets and associated documentation to explain concepts and the “why” behind some of the code. You can leave questions & feedback on the official forum thread.

Last updated for (See GitHub ReadMe)

PROJECT SOURCE DOWNLOAD (GitHub)

PROJECT DOCUMENTATION (Wiki)

section6_coop02

Game Premise

“The game is a third person survival game focusing on familiar mechanics from games in this genre.

You’ll have to find a weapon to defend yourself. Food and ammunition are spread throughout the level and so you are constantly on the search for resources. Enemies may be anywhere, making too much noise while scavenging to survive will attract attention. The environment will have interactive objects to help your defense. The game will support coop play with a buddy. Survive as many days/nights as possible.

You will end up with a basic third person game, fully networked and a small environment with interactive objects that you may use as a base for your own survival game.”

Get Started!

You can download the latest project source at GitHub and get started with the project documentation right away!

At this time the project is at Section 4 of 6. Here is a brief overview of what has been added with each section:

Section 1 – Character Setup

section6_advancedanimbp03

Sets up the third-person character movement with animation, object interaction, simple hunger system, all with networking support. (view docs)

Section 2 – Weapons, Death & Inventory

section6_equipment03

Adds weapon support for the character, a flashlight, UT-style inventory with on-character visual representation of the carried items and deals with damage, death and respawns for players. (view docs)

Section 3 – Zombie AI

section6_zombieattacking01

AI “Zombie” enemy to our game using PawnSensing and Behavior Tree. (view docs)

Section 4 – Time of Day

section6_timeofday_combined

A dynamic time of day, advanced player spawning and a basic game loop. (view docs)

Section 5 – Networking

section6_coopoverview

Introduces the ability to carry around objects like barriers and introduces you to Unreal Engine 4 game networking. (view docs)

Section 6 – Polish & Review

section_openworldscenery01

The final section in the series focuses on bug fixing and a bit of polish to the existing features. This section is compatible with the 4.8 release. (view docs)

References

100 Comments

  1. The video series on 3rd person power pickup is outdated, they will need to modify some things in the code to get it to work. I don’t think it would be a good starter series for people trying to learn UE4 C++. Unless you want them to test their skills in debugging code. 😉

  2. To your understanding what is fully networked? As with your inventory tutorial you stated it was fully networked/multiplayer as well while it’s not. (as checking the replication check box doesn’t make it multiplayer/fully networked)

    And do you plan on making a blueprint only series for this?

    As always great work on sharing this I’m sure it’s useful for a lot of people.

    • The inventory tutorial itself doesn’t state it’s fully networked. The separate UsableActor tutorials (considered a “part 1”) are fully network supported however.

      And as for this project, yes it will by default support all features in multiplayer.

      Edit: There won’t be a BP version for this (not planned atleast) The main reason this is all C++ is to balance out the great difference between BP and C++ resources that are currently available for UE4.

      – Tom

  3. I just tried the second part, and the flashlike is inside his arm. It looks like you missed the attatchment point or something. Just a small thing. Otherwise, it is awesome.
    Oh, one more thing, could you make it so we can open the Visual Studio project? It seems all the profect files are missing from the project folder (Intermediate/ProjectFiles) There is not even a folder. Thanks.

    • Hi Lance,

      The flashlight is a content bug, I need to fix the translation during import.

      You’ll need to generate the Visual Studio files by right-clicking the .uproject, I can’t includes those folders because they are supposed to be machine specific. Perhaps I should clarify this in the project documentation.

      – Tom

  4. Thanx for the gr8 content i noticed that there is a bug on pirate boomb explode and game crash in the latest update github.
    Also if possible can you add more comments on the damge handling system and how it works in details.

    In sweapon.h there is a macro PURE_VIRTUAL if you can explain what it do.

    I know it still work in progress but i though to let you know.

    Keep dropping the good stuff .
    big thanx from the hart
    Abdullah

    • Hi Abdullah,

      Thanks the bomb crash has been reported, I will fix that today! Thanks for pointing this out to me.

      I’ll add some comments to PURE_VIRTUAL to explain how/when to use it. For now: It’s basically to omit the implementation requirement since our SWeapon.cpp has nothing to implement, it’s only when we reach the SWeaponInstant.cpp that we care about this function and need to implement behavior.

      – Tom

  5. Hello, I have this error

    1>D:/Doc/Unreal Projects/AzuraDreams_NG 4.6/Source/AzuraDreams_NG/BaseCharacter.h(61): error : In BaseCharacter: Unrecognized type ‘FTakeHitInfo’
    1>Error : Failed to generate code for AzuraDreams_NGEditor – error code: OtherCompilationError (2)

    I need Help!! I work with Unreal 4.6.1

    • You either don’t have the FTakeHitInfo copied over from the survival project or you don’t have the #include to wherever you added your FTakeHitInfo struct.

      – Tom

  6. Hello, Tom! I’ve learned a lot going through the Wiki for the past few weeks. So before I ask my question, I’d like to thank you for taking the time out to post these tutorials as they’ve been a lot of help.

    I noticed the TeamNumber variables have not been used yet, and was curious if there was a currently a procedure to make them workable in game. I’m following along, but have not seen this functionality mentioned.

    • Hi D,

      The teamnumbers in SPlayerState are used by the GameMode, have a look at CanDealDamage() in SGameMode to see how 🙂

      – Tom

      • Oh, it seems I was mistaken! I thought that teamnumbers was used to separate players into teams, whereas it actually separates the AI zombies from the players. Makes sense now 🙂

  7. Hello Tom,

    My name is Joaquín and I´m following your C++ SurvivalGame Samples. I will like to question, Is this C++ API’s Project available for UE4 4.8?.

    Thank you very much!.

    Best regards

  8. Hi Tom,

    So in some of the levels in this project there’s those Barrier that you can push and jump on but I noticed that they shake when you jump on them and sometimes they just bounce off.

    I made a short clip to show what I mean:
    https://www.youtube.com/watch?v=BJnKnkoEsas

    And just to note if I start a new project and put something on the ground and simulate physics the same thing happens.
    Here’s a test in another project just to show it:
    https://www.youtube.com/watch?v=DKTPc42OUUU

    How do I fix this and make the behavior more real?

    I know it’s kind of unrelated but I couldn’t find an answer for it anywhere.

  9. I’ve played around a little with the weapon trace & weapon trails. It seems that the weapon trail is not working correctly. It doesn’t go into the direction you’ve shot, but in a fixed direction (I did not manipulate the c++ code, the Vector parameter is still set in code). If you comment the “only create trails fx by other players” part out, you’ll notice it quite quickly.

    Cheers
    Theo

  10. Hi. I have downloaded the full project from github. Now when I try to open it up with UE 4.8.1 , build fails with around 20 errors.
    Some of them are :
    error C2555: ‘USoundNodeLocalPlayer::GetInputPinName’: overriding virtual function return type differs and is not covariant from ‘USoundNode::GetInputPinName’

    error C2039: ‘SpawnSoundAttached’ : is not a member of ‘UGameplayStatics’

    What could be the reasons? Am I doing something wrong?

  11. Hey Tom, I tried to open the survival game unreal project file in UE4, but then I got a message that said “The following modules are missing or built with a different engine version: UE4Editor-SurvivalGame.dll” and it asked if I would like to rebuild them, so I started the build and when it was finished I got an error saying “Error recompiling, try recompiling from source manually.”

    I am really new to UE4 in terms of opening another users UE4 project, so I just was wondering if I am doing it correctly or if the problem stems from a difference in versions?

    I am running UE4 version 4.9.2

      • Thanks Tom. Would you mind to point me in the right direction as to how you would go about implementing this in the context of this example project? Just some general advice would help point me toward the solution and I can work it out on my own from there.

  12. Hello, very nice thank you for taking the time to make these,

    I know your busy so ill be quick, roughly how difficult would it be to make a mobile (android mainly) version of this?

    Thanks

    Luke

    • There will be some rendering limitations, and of course you’d need to fit in your own methods of controlling the character etc. I’ve not tried this, but I welcome you to give it a shot.

      Just keep in mind that the project was definitely not designed for mobile, and certain features may very well be way too heavy for mobile at this point.

  13. Hello Tom.I am facing a problem while running the section 1 uproject.It says SurvivalGame could not be compiled.Try rebuilding from source manually .I have ue 4.9.

  14. hi Tom,
    your tutorial are awesome, but i’ve got a problem with the function TakeDamage() and DealDamage().
    i can’t TakeDamage from other player and InflictDamage to other Player (it’s a Multiplayer game), in my characterClass i override the function TakeDamage and in my WeaponClass i’ve created the weapon trace and DealDamage to inflict damage, i don’t know what is/are the error/s.
    Thanks for your time.

    • The damage application is already implemented in the project. You should have all the functions necessary to deal/take damage – without any specific errors I can’t do much to help.

      – Tom

  15. Hi Tom! I admire your work in this tutorial. I’ve downloaded the github version of this game and build it smoothly. But I realized this game was very choked up and laggy. Can you give me the advice to fix it?

    • Hi Diego,

      There is no automatic way to convert C++ to Blueprints. You can however have a C++ project framework and continue building features etc. using Blueprint on top of that.

      – Tom

  16. This has absolutely made a lot of things clear for me on how to do things. For the life of me, I cannot find where the text is set for the “stats” in the top left: ComputerName – Score 0 – Kills 0 – Deaths 0. I want to add another varaible in their for debugging purposes but its not in the UI blueprints and I looked at all the references related to those variables. 🙁

    • That means you are opening the engine with a different version than it was built with. The project was last updated for 4.12

      – Tom

      • I am Getting this:
        3> SurvivalGame.generated.cpp
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Editor\SoundNodeLocalPlayer.cpp(13): error C2039: ‘GetAudioComponent’: is not a member of ‘FActiveSound’
        3> G:\UnrealEngine\Engine\Source\Runtime\Engine\Classes\Sound/SoundNode.h(11): note: see declaration of ‘FActiveSound’
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Editor\SoundNodeLocalPlayer.cpp(13): error C2227: left of ‘->GetOwner’ must point to class/struct/union/generic type
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Player\SCharacter.cpp(127): warning C4458: declaration of ‘InputComponent’ hides class member
        3> G:\UnrealEngine\Engine\Source\Runtime\Engine\Classes\GameFramework/Actor.h(316): note: see declaration of ‘AActor::InputComponent’
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Player\SCharacter.cpp(888): error C2027: use of undefined type ‘UAnimInstance’
        3> G:\UnrealEngine\Engine\Source\Runtime\Engine\Classes\Animation/AnimBlueprintGeneratedClass.h(15): note: see declaration of ‘UAnimInstance’
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Player\SCharacter.cpp(888): error C2227: left of ‘->Montage_Stop’ must point to class/struct/union/generic type
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Player\SPlayerController.cpp(94): warning C4458: declaration of ‘MyHUD’ hides class member
        3> G:\UnrealEngine\Engine\Source\Runtime\Engine\Classes\GameFramework/PlayerController.h(188): note: see declaration of ‘APlayerController::MyHUD’
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\Player\SPlayerController.cpp(107): warning C4458: declaration of ‘MyHUD’ hides class member
        3> G:\UnrealEngine\Engine\Source\Runtime\Engine\Classes\GameFramework/PlayerController.h(188): note: see declaration of ‘APlayerController::MyHUD’
        3>E:\Game Development\Master Branch\Source\SurvivalGame\Private\World\SGameMode.cpp(99): warning C4456: declaration of ‘MyGameState’ hides previous local declaration
        3> E:\Game Development\Master Branch\Source\SurvivalGame\Private\World\SGameMode.cpp(86): note: see declaration of ‘MyGameState’
        3>ERROR : UBT error : Failed to produce item: E:\Game Development\Master Branch\Binaries\Win64\UE4Editor-SurvivalGame-3591.dll
        3> Total build time: 171.06 seconds
        3>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.MakeFile.Targets(37,5): error MSB3075: The command “G:\UnrealEngine\Engine\Build\BatchFiles\Build.bat SurvivalGameEditor Win64 Development “E:\Game Development\Master Branch\SurvivalGame.uproject” -waitmutex” exited with code 5. Please verify that you have sufficient rights to run this command.
        ========== Build: 2 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

        Please Help

        • SoundNodeLocalPlayer.h and SoundNodeLocalPlayer.cpp copied from another epic project “ShooterGame”

          In SCharacter.cpp add a line #include “Animation/AnimInstance.h”
          Done

  17. Hi,

    I have a question?

    What happens if FocusedUsableActor in SCharacter.cpp is destroyed?

    Then this code FocusedUsableActor->OnEndFocus(); will result in editor crash. Maybe it’s not likely in your case but what would be the best way to ensure that FocusedUsableActor is not destroyed when it’s used?

    FWeakObjPtr or AActor::OnDestroyed?

    • Fair point, Hadn’t considered that case. I’d go for a WeakObjPtr and check it’s state before calling functions on it. Good find.

      – Tom

  18. Shooting a bomb to kill a zombie doesn’t register as a kill for the player. Is this expected? When debugging I see the EventInstigator parameter in the ASBaseCharacter::TakeDamage() function call is null.

    • After slightly more digging I found the last parameter to ApplyRadialDamage was nullptr. I believe just keeping track of who the last APawn/AController to interact with the bomb was and using that instead of nullptr should fix the problem.

    • After looking at GameMode.cpp in the Engine project, i see that the other InitNewPlayer function is being called instead of the one that ASGameMode class is overriding. I swapped the ASGameMode InitNewPlayer() to use the other prototype and everything works now (Couldn’t deal damage to AI because SetTeamNumber() wasn’t getting called). Any explanation as to why switching from 4.12 to 4.13 could cause this to happen?

      • Thanks for pointing this out Clay, I just updated the public repo with the same fix. Looks like a bad merge in the engine code, the functions are so similar that one of them should in my opinion be removed.

  19. Hi Tom! How i can use the RPG Inventory system in the project? Please write the minimal instructions! Thank you!

    • Hi Zoltan,

      The RPG inventory system isn’t mine and afaik it’s a paid package (which I don’t own). So you’ll have to resolve that yourself I’m afraid.

      – Tom

  20. Hi Sir ! Have you even thought to make Youtube tutorials for your game ? Because it would be very nice. We can understand the concepts of Unreal Engine C++ more easily and efficiently.
    Looking for a positive response.

  21. pls sloved this error

    The project could not be compiled. Would you like to open it in Visual Studio?

    Running C:/Program Files (x86)/Epic Games/4.14/Engine/Binaries/DotNET/UnrealBuildTool.exe SurvivalGame Development Win64 -project=”C:/Users/Kavan/Documents/Unreal Projects/SurvivalGame 4.14/SurvivalGame.uproject” -editorrecompile -progress -noubtmakefiles -NoHotReloadFromIDE
    @progress push 5%
    Parsing headers for SurvivalGameEditor
    Running UnrealHeaderTool “C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\SurvivalGame.uproject” “C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Intermediate\Build\Win64\SurvivalGameEditor\Development\SurvivalGameEditor.uhtmanifest” -LogCmds=”loginit warning, logexit warning, logdatabase error” -Unattended -WarningsAsErrors -installed
    Reflection code generated for SurvivalGameEditor in 4.9968581 seconds
    @progress pop
    Performing 6 actions (4 in parallel)
    [2/6] Resource ModuleVersionResource.rc.inl
    [3/6] Resource SurvivalGame.rc
    PCH.SurvivalGame.h.cpp
    Module.SurvivalGame.cpp
    SurvivalGame.generated.cpp
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\Player\SBaseCharacter.cpp(101): error C2039: ‘GetMatchState’: is not a member of ‘AGameModeBase’
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime\Engine\Classes\GameFramework/GameStateBase.h(10): note: see declaration of ‘AGameModeBase’
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\Player\SPlayerController.cpp(31): error C2248: ‘UWorld::GameState’: cannot access private member declared in class ‘UWorld’
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime\Engine\Classes\Engine/World.h(780): note: see declaration of ‘UWorld::GameState’
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime/Engine/Classes/Animation/AnimInstance.h(24): note: see declaration of ‘UWorld’
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\Player\SPlayerState.cpp(41): error C2248: ‘UWorld::GameState’: cannot access private member declared in class ‘UWorld’
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime\Engine\Classes\Engine/World.h(780): note: see declaration of ‘UWorld::GameState’
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime/Engine/Classes/Animation/AnimInstance.h(24): note: see declaration of ‘UWorld’
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(9): error C2011: ‘FLogCategoryLogGameMode’: ‘struct’ type redefinition
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime\Engine\Classes\GameFramework/GameModeBase.h(20): note: see declaration of ‘FLogCategoryLogGameMode’
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(69): error C2027: use of undefined type ‘FLogCategoryLogGameMode’
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime\Engine\Classes\GameFramework/GameModeBase.h(20): note: see declaration of ‘FLogCategoryLogGameMode’
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(69): error C2065: ‘CompileTimeVerbosity’: undeclared identifier
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(69): error C2228: left of ‘.IsSuppressed’ must have class/struct/union
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(69): error C2228: left of ‘.GetCategoryName’ must have class/struct/union
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(69): error C2665: ‘FMsg::Logf_Internal’: none of the 2 overloads could convert all the argument types
    C:\Program Files (x86)\Epic Games\4.14\Engine\Source\Runtime\Core\Public\Logging\LogMacros.h(37): note: could be ‘void FMsg::Logf_Internal(const ANSICHAR *,int32,const FName &,ELogVerbosity::Type,const TCHAR *,T1)’
    with
    [
    T1=FVector *
    ]
    C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Source\SurvivalGame\Private\World\SCoopGameMode.cpp(69): note: while trying to match the argument list ‘(const char [111], int, ELogVerbosity::Type, const wchar_t [37], const TCHAR *, FVector *)’
    ERROR: UBT ERROR: Failed to produce item: C:\Users\Kavan\Documents\Unreal Projects\SurvivalGame 4.14\Binaries\Win64\UE4Editor-SurvivalGame.dll
    Total build time: 47.08 seconds

  22. Fantastic tutorial! When I play the game I see a very noticeable lag when the game time advances. I set the sunlight to not cast shadows, I set the trees to not cast dynamic shadows and rebuilt the lighting but it’s still there. If I remove the day/night manager from the game it goes away. My laptop is intel i7, 8GB RAM, windows 10, Radeon 5600 with 2GB RAM.

    • I’m afraid that’s due to the Skylight actor being re-captured dynamically as the time of day changes. Not very ideal, I know where are other ways of handling blending of this by texture, esp. now!

      I haven’t seen hitches myself after making some changes to how frequently it re-captures, but it’s still a perf hog and if you’re moving forward with this project, it’s better to blend between a few cubemaps in the Skylight rather than trying to dynamically recapture.

      One source on blending cubemaps I remember is from Zak: https://www.youtube.com/watch?v=cHzt73Tn0L4

  23. It might not be the best place to ask this but i really can’t find any answers. Can someone please tell me/teach me how to create a second player (local splitscreen) in C++ and give inputs to it? I can’t find anything that is in c++ and nothing that works in blueprint. I’m trying for a week and I can’t find a solution.

    • Hi Alex,

      ShooterGame example available via the Learn-tab of the Epic Games launcher might have some splitscreen code for you to look into. I never tried it, but I remember seeing some mentions of it in their code.

  24. Hi Tom, I have a question…will you upgrade this to 4.15 or that’s not in your plans?
    I seen you have done this before but I wanted to ask to be sure. If you could be so kind to reply.

      • Oooh hahahaha, I’m just starting to mess around with it, there’s a lot of cool things I wanna try out in this new upgrade. Thanks for the reply!!!

Leave a Reply