Ongoing C++ Gameplay Example Series: Making a Survival Game

The latest source on GitHub is updated for 4.10, so yep it’s still alive but currently no new sections planned.

thank you for share!!!

So I am trying to learn C++ and I am using your Survival game but I have hit two issues that are confusing me.

  1. I have been able to implement semi-auto fire by adding a check in the CanFire(); function,


bool ASWeapon::CanFire() const
{
	bool bPawnCanFire = MyPawn && MyPawn->CanFire();
	bool bStateOK = CurrentState == EWeaponState::Idle || CurrentState == EWeaponState::Firing;
	bool bSFire;
	if (SemiAuto)
	{
		bSFire = SemiFire;
	}
	else
	{
		bSFire = false;
	}
	return bPawnCanFire && bStateOK && !bPendingReload && !bSFire;
}


and the SetWeaponState Function



void ASWeapon::SetWeaponState(EWeaponState NewState)
{
	const EWeaponState PrevState = CurrentState;

	if (PrevState == EWeaponState::Firing && NewState == EWeaponState::Firing && !CanFire())
	{
		OnBurstFinished();
	}
	
	if (PrevState == EWeaponState::Firing && NewState != EWeaponState::Firing)
	{
		OnBurstFinished();
	}

	CurrentState = NewState;

	if (PrevState != EWeaponState::Firing && NewState == EWeaponState::Firing && CanFire())
	{
		OnBurstStarted();
	}
}


and it works if I set the the SemiFire variable true and false in the right places. The problem is the fire animation either continuously plays, or, if I add an && CanFire() to the IsFiring bool in the SCharacter.cpp, wont play at all.

Any thoughts on how to better implement semi-fire or allow the fire animation to only play once?

  1. I am trying to take the reserve ammo off of the gun itself and put it into a repository. For testing purposes I created HeldAmmo and MaxHeldAmo variable on the SCharacter class but I am having a hell of a time passing that information to the SWeapon.cpp. The bolded is how I am trying to do this


ASWeapon::ASWeapon(const class FObjectInitializer& PCIP)
: Super(PCIP)
{
	Mesh = PCIP.CreateDefaultSubobject<USkeletalMeshComponent>(this, TEXT("WeaponMesh3P"));
	Mesh->MeshComponentUpdateFlag = EMeshComponentUpdateFlag::OnlyTickPoseWhenRendered;
	Mesh->bReceivesDecals = true;
	Mesh->CastShadow = true;
	Mesh->SetCollisionObjectType(ECC_WorldDynamic);
	Mesh->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	Mesh->SetCollisionResponseToAllChannels(ECR_Ignore);
	Mesh->SetCollisionResponseToChannel(ECC_Visibility, ECR_Block);
	RootComponent = Mesh;
	bIsEquipped = false;
	CurrentState = EWeaponState::Idle;
	AmmoType = EAmmoType::Rifle;
	PrimaryActorTick.bCanEverTick = true;
	PrimaryActorTick.TickGroup = TG_PrePhysics;

	SetReplicates(true);
	bNetUseOwnerRelevancy = true;

	MuzzleAttachPoint = TEXT("MuzzleFlashSocket");
	StorageSlot = EInventorySlot::Primary;
	
	
	ShotsPerMinute = 700;
	
	**if (MyPawn)
	{
		StartAmmo = MyPawn->HeldAmmo;
		MaxAmmo = MyPawn->MaxAmmo;
	}**

	
	
	
	MaxAmmoPerClip = 30;
	NoAnimReloadDuration = 1.5f;
	NoEquipAnimDuration = 0.5f;
	SemiAuto = false;
	SemiFire = false;
	
}


The is that MyPawn doesn’t seem to get called and if I remove the if(MyPawn) {} it crashes the engine. Is there a better way to do this? The reason why I want to remove the ammo from the Gun is to not allow dropping and pickup up a weapon to reset your held ammo.

Hi, I was looking at building something using your code as a base.
But I don’t really need everything you’ve got, are the Sections updated to a newer engine?
(for example if I took Section 4 branch) or will I need to migrate them myself?

Hi ,
In 4.10.2 the InitNewPlayer in GameMode is protected how we can fix it i gave warning for that ?

The individual sections are no longer updated to the latest engine version. The branch still is. I recommend grabbing latest and looking for the components you wish to learn and/or use. The development of a project wasn’t a linear typing exercise, I made improvements to features that made it into Section 1 in later submits etc.

The function is protected, but you can still override it as before in your own gamemode class. If you want to call it manually from outside of Gamemode, I advise you not to, and look for a different solution or make your own function to handle adding players.

hi I would like to learn something about ue4 so I started to work on your project mmm but still I couldnt handle custom character problem because of animations, also I made a new map and I created everything but when I try to launch its give me error ( windows platform ) and I cant play it such a game can you help me please ?

hi i need your help i trying to download your survival proyect but when i download it says that the proyect dont work that is damage the proyect

Hi Roberto,

What is the exact error you are getting? Are you downloading the ZIP file or cloning it as a git project?

Hi ,
I want to know how many player can player this Survival sample game in network ?

And also please explain how i can play your game with my friend through internet is this possible ?
Thanks

Hi mate,

The game can be played with as many players are most of other UE4 titles. It’ll require some optimization to work with 32+ players, but in theory, that is possible.

You can run the game in multiplayer as any other. I’ve added a few options in the main menu to make it easier to connect with your friends.

(Make sure port 7777 is forwarded on the router from the person that hosts the server, that’ll be required for others to connect to the host)

Thanks for your quick reply, you are very nice i learned many things from this project… My problem was I didn’t download branch I just downloaded Section-6, I’ll start download that to learn more …

It’s a Level in the /Maps/ folder. When you are launching a packaged version of the game, it will open this map automatically when you launch the EXE.

I get the following error when I start the editor:

“Error /Game/Maps/CoopLandscape_Map : Failed import for AudioComponent /Game/Base/BP_TimeOfDayManager.Default__BP_TimeOfDayManager_C:AmbientAudioComp”

Any idea how to fix this?

Noticed the same when opening the project, I’ll look for a fix the next time I do an upgrade of the project.

Hello, I’m not sure if this project is still active but it would be a great asset for me. Unfortunately, when trying to launch the project I receive the message:

I am running Windows 10 with Visual Studio 2015, and am trying to open the uproject file in Unreal Editor 4.10. I cloned the git repository, but am trying with the zip now (30 minute download.)

Hi ,
I tried to connect with a friend by internet IP, but I couldn’t.
It’s work good when I’m connecting to another pc on lan.

please help how i can connect with Internet to another pc

Hi Vahid,

im not 100% sure how UE4 handles it, but i think you need the current public IP of your friend if he is hosting the server (most routers show it in the webinterface).
And you also have to enable portforwarding for the ports ue4 is using. Then connecting directly via IP should work. But im happy if someone can correct me.

Regards

Thanks,
I get public IP, by searching in google this “What’s My IP” and i have the correct IP, can you explain more on portforwarding

This was posted by a bit earlier:
“…(Make sure port 7777 is forwarded on the router from the person that hosts the server, that’ll be required for others to connect to the host)…”

I don’t know what kind of router you have, so you have to check the webinterface and check the settings. there should be a section for portforwarding. A quick google search with the model should give you a documentation.

Basicly you have to make an entry for port 7777 (TCP i guess) and forward it to the local ip of your pc where the server is running.

Then if your friend sends ue4 data to your public ip your router will see that traffic on port 7777 should be sent to your pc and it should work.

Regards