Need help to "First Person Shooter C++ Tutorial"

Hello out there. Noob need help to a thing in the First Person Shooter C++ Tutorial.
tut.gif
This is were i am lost its in the “Creating a GameMode” nr. 8 in the First Person Shooter C++ Tutorial.
I guess i dont have to take it to literally and what i tryed dosnt work.
Here is my code so far so you can see if anything is wrong. I still need to put in the above code.
h.gif
cpp.gif
I am using version 4.7.3

Hope you can help.

Hi!

I did that tutorial two weeks ago as well. Not sure what your problem is, though. Just add the constructor to the header and implementation file as it says, like so in the header file:


#pragma once

#include "GameFramework/GameMode.h"
#include "FpsGameMode.generated.h"

/**
 * 
 */
UCLASS()
class TUTORIALFPS_API AFpsGameMode : public AGameMode
{
	GENERATED_BODY()
	
public:
	AFpsGameMode(const FObjectInitializer& ObjectInitializer);

	virtual void StartPlay() override;
	
};


and then simply implement it:


AFpsGameMode::AFpsGameMode(const class FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
	static ConstructorHelpers::FClassFinder<APawn> PlayerPawnObject(TEXT("Pawn'/Game/Blueprints/BP_FpsCharacter.BP_FpsCharacter_C'"));
	if (PlayerPawnObject.Class != nullptr)
	{
		DefaultPawnClass = PlayerPawnObject.Class;
	}

	HUDClass = AFpsHud::StaticClass();
}

The only thing I noticed is, you don’t have the visibility defined in the header (the “public:”). I think the constructor needs to be public. In older versions of the UE, this was automatically done by the GENERATED_UCLASS_BODY() macro (which was replaced by the GENERATED_BODY() macro you are using now), but this was changed in 4.6 IIRC.

Are you getting any errors? If yes, can you post them?
It’s hard to help without knowing what’s wrong. :wink:

Hey zaha, thanks for posting your code. I think what you posted is from the Making a Character part of the tutorial? Prior to that in Creating a GameMode as referenced by the OP it says:

*"Note: 4.6 needs below line to be included (before or after StartPlay(). This is the definition of constructor whose signature is also different from older versions.

AFPSGameMode::AFPSGameMode(const class FObjectInitializer& ObjectInitializer);"*

Can you confirm this can be ignored and just to enter it when directed to do so later on in Making a Character?

I think that’s where the confusion lies, but I could be mistaken.

Ok. When i add the code so it looks like this
cpp02.gif
I get this error messege when trying to compile it in the editor.

Hey Moose_Head!,

the posted code is from my finished FPS tutorial project, so everything is there. :slight_smile:

Ah I see, now I get what the question was all about: Since 4.6 some constructors are optional (but not all yet). That’s what this note is all about.
AGameMode is one of those classes where the constructor can safely be omitted now. So to answer the question: Yes, you can ignore it for now and add it later. On the other hand, an empty constructor won’t do any harm as well, so adding it now is also a possibility (since you need it later anyway). It doesn’t really matter.

In this case, if the compiler does not complain about it, everything is OK and you can do it this way. :wink:
(that may not always be true, though. When working with dynamically allocated stuff errors may manifest only during run-time and cannot be caught by the compiler. But as I said, this does not apply here.)

@Rainbow unicorn on mushrooms: Ok, so you added the constructor to the header file. Now you must add it to the implementation file (.cpp) as well! Otherwise the compiler error you posted will occur. See step 7. in the tutorial (which must be slightly adapted for the latest version):


AFPSGameMode::AFPSGameMode(const class FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
 
}

And it Works. Thanks a lot mate :smiley:

Thanks, I only have a basic understanding of C++ and I couldn’t see the wood for the trees until I saw your code.

Really? If you try to add that line it will throw an error to say it already has a body, I guess because it already inherits one?

Even though it’s mentioned where you’re editing the FPSGameMode.h, it’s not intended for there, and you can’t add it to the .cpp (well I couldn’t) until you’ve put a declaration in .h and it will instruct you to put them both in later on in the tutorial.

I must really be bad if I’m getting confused so early on. But, that warning, if it is a warning, seems ill placed, easily confused as a direction to act upon and really just surplus to requirements.

@Moose_Head!: Can you post your code and the error occurring? That makes it easier to help you.

The base class constructor shouldn’t interfere with this.

Well as I said, you don’t need it, so feel free to leave it out at this step of the tutorial. And don’t worry, C++ is a rather difficult language. Don’t let yourself be discouraged, it takes time to learn it, so keep at it!
Sorry, what do you mean by warning? I can’t follow you.

Sorry if I’ve been rather vague, by warning I mean the one Rainbow unicorn on mushrooms posted in the OP, regarding the need to include:


AFPSGameMode::AFPSGameMode(const class FObjectInitializer& ObjectInitializer)

You can’t include that line on its own, yet the way it’s written “Note: 4.6 needs below line to be included (before or after StartPlay(). This is the definition of constructor whose signature is also different from older versions.” I think gives the impression that you need to do something regarding it’s inclusion at that stage, when that’s not the case. I’m not sure how you’ve managed to add it without the declaration which isn’t mentioned until later, but if I were to add it, the error it would give would be: *“C2084: function ‘AFPSGameMode::AFPSGameMode(const FObjectInitializer &)’ already has a body”
*

I just think it’s odd because it’s mentions this right after it tells you to edit the FPSGameMode.h. Just to be clear, I’m not talking about when the tutorial gets you to add it in the Making a Character section, because that goes along with the declaration in the header and is fine, I’m talking about where it mentions that it will need to be included in Creating a GameMode. If you can add it at that stage without the ‘already has a body’ error, then something is amiss.

Anyway, I haven’t had any further errors, having reached to where you add the first-person model, so hopefully it’ll continue that way so just to be clear I don’t have any current issues to resolve, though I appreciate your time.

The only question I have is a curiosity, can you really add a constructor implementation that won’t clash with the base implemetation without also adding a declaration for it? Though it may not be worth answering, and I’m not even certain I’ve got the terminology right.

Cheers.

I see and you are right, beginners might be confused by this note. It assumes some experience with C++.

To answer your question, you always need to provide a definition (implementation) for your declaration. The only case where this is not necessary are pure virtual functions. I’m not a C++ expert, but constructors are specific to the classes they construct. I’m not aware of any clashes or conflicts between base class constructors and derived class constructors. Each constructor has its own name, since the class names must be different from one another, so you can think of them as simple static functions. Even when you call a base class constructor from a derived class, the derived class still has and needs its own constructors which are executed after the base class constructors did their work.

Hope that sheds some light on the topic.

Best regards

I forgot to say, thanks for the tips and the links, I’ll check them out.

Cheers.