I am trying to spawn in a blueprint via c++. And I have a problem with the class.
When I compile it says that AA_MainGame::AA_MainGame(const class FObjectInitializer& PCIP) already has a body. I read somewhere that this means that I’ve probably included the file to much. I’ve read this post on stackoverflow: c++ - Error C2084 'Function already has a body' - Stack Overflow and the article about header guards, and I do understand the concept but I do not know how to apply it to my own code.
.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "GameFramework/Actor.h"
#include "A_MainGame.generated.h"
/**
*
*/
UCLASS()
class ROCKETGAME_API AA_MainGame : public AActor
{
GENERATED_BODY()
virtual void BeginPlay() override;
TSubclassOf<AA_MainGame> BlueprintVar; // YourClass is the base class that your blueprint uses
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "RocketGame.h"
#include "A_MainGame.h"
#include "Engine.h"
AA_MainGame::AA_MainGame(const class FObjectInitializer& PCIP)
: Super(PCIP)
{
// Spawning the blueprint into the level
static ConstructorHelpers::FObjectFinder<UBlueprint> BP_MainGame(TEXT("Blueprint'/Game/Blueprints/BP_MainGame.BP_MainGame'"));
if (BP_MainGame.Object) {
BlueprintVar = (UClass*)BP_MainGame.Object->GeneratedClass;
}
}
void AA_MainGame::BeginPlay()
{
Super::BeginPlay();
UWorld* const World = GetWorld(); // get a reference to the world
if (World) {
// if world exists
AA_MainGame* YC = World->SpawnActor<AA_MainGame>(BlueprintVar, FVector(0, 0, 0), FRotator(0, 0, 0));
}
}
The Problem here is that the ‘GENERATED_BODY()’ Macro aleready declares and defines ‘AA_MainGame(const class FObjectInitializer& PCIP)’ for you, so when you define it in your *.cpp you have two Definitions.
But the Macro does not declare & define the Constructor if you declare it yourself, so you have to add ‘AA_MainGame(const class FObjectInitializer& PCIP);’ in your header and it should work.
Thanks for the quick respond. I get what you are trying to say, but when I try to apply it, I can not find out where to put it.
For me, it sounds the most logical to add it after: class ROCKETGAME_API AA_MainGame : public AActor. But that does not work, any other spot neither. Where do I place it?
Maybe the purpose is just to learn how to developp in C++, even if the same can be achieved quicker with a blueprint.
You should restrain from judging what people do or want to do, because you may not have a full understanding of their goals.