C++ error when compiling an Actor class (trying to create a function for spawning an actor)

Hi everyone,

I’m trying to create a C++ function who can spawn an actor callable in BluePrint.
I want with this function doing the same things than “spawnActor” in BluePrint.

The problem is, when i compile, i got an error, that make a half day i trying to resolve my problem.
Here my code.

My actor class .h:

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “MyActorTest.generated.h”

UCLASS()
class MYPROJECT2_API AMyActorTest : public AActor
{
GENERATED_BODY()

public:
// Sets default values for this actor’s properties
AMyActorTest();

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;

UFUNCTION(BlueprintCallable, Category = "Spawning Actor")
AMyActorTest * SpawnObjectActor(AActor * ActorReference, UClass * ClassReference, FVector Location, FRotator Rotation);

};

And here my .cpp code:

// Fill out your copyright notice in the Description page of Project Settings.

#include “MyProject2.h”
#include “Engine/Level.h”
#include “MyActorTest.h”

// Sets default values
AMyActorTest::AMyActorTest()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActorTest::BeginPlay()
{
Super::BeginPlay();

}

// Called every frame
void AMyActorTest::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);

}

AMyActorTest * SpawnObjectActor(AActor * ActorReference, UClass * ClassReference, FVector Location, FRotator Rotation) {

UWorld* const World = ActorReference->GetWorld();
//AMyActorTest * MySpawnedActor = TSubclassOf<AActor> MyObject;
AActor * MySpawnedActor = ActorReference;
AMyActorTest * ActorTest = new AMyActorTest();

if (World != NULL)
{
    World-&gt;SpawnActor&lt;AActor&gt;(Location, Rotation);
}

return ActorTest;

}

Here a screenshot of my compiler error:

https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116711

Thanks in advance for who will help me, i don’t know how to resolve my *** problem.

You didn’t implement your method correctly, it should be:


AMyActorTest* AMyActorTest::SpawnObjectActor(AActor* ActorReference, UClass* ClassReference, FVector Location, FRotator Rotation)

Thanks for your reply Zeblote, it’s what i did, but when i do this, i got this error:

up please ^^

can you post your code after the suggested modification was applied? :slight_smile:

Yes of course, here my code:
My .h file




// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "MyActorTest.generated.h"


UCLASS()
class MYPROJECT2_API AMyActorTest : public AActor
{
    GENERATED_BODY()

public:    
    // Sets default values for this actor's properties
    AMyActorTest();

protected:
    // Called when the game starts or when spawned
    virtual void BeginPlay() override;

public:    
    // Called every frame
    virtual void Tick(float DeltaTime) override;

    UFUNCTION(BlueprintCallable, Category = "Spawning Actor")
    AMyActorTest* AMyActorTest::SpawnObjectActor(AActor * ActorReference, UClass * ClassReference, FVector Location, FRotator Rotation);

};



https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116743

And my cpp file:




// Fill out your copyright notice in the Description page of Project Settings.

#include "MyProject2.h"
#include "Engine/Level.h"
#include "MyActorTest.h"


// Sets default values
AMyActorTest::AMyActorTest()
{
     // Set this actor to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
    PrimaryActorTick.bCanEverTick = true;

}

// Called when the game starts or when spawned
void AMyActorTest::BeginPlay()
{
    Super::BeginPlay();

}

// Called every frame
void AMyActorTest::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);

}

AMyActorTest* AMyActorTest::SpawnObjectActor(AMyActorTest * ActorReference, UClass * ClassReference, FVector Location, FRotator Rotation) {

UWorld* const World = ActorReference->GetWorld();
//AMyActorTest * MySpawnedActor = TSubclassOf<AActor> MyObject;
AMyActorTest * MySpawnedActor = ActorReference;
AMyActorTest * ActorTest = new AMyActorTest();

    if (World != NULL)
    {
        World->SpawnActor<AMyActorTest>(Location, Rotation);
    }

    return MySpawnedActor;
}



https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116744

Thanks for your help, i hope i will resolve my problem ^^

I found editing the YourGame.Editor.cs can resolve the problem, but i’m not sure this resolve really the problem

In my .h file if i delete my “UFUNCTION” my function will work perfectly.
But i need tis property for calling my function in my BluePrint

Well, like this without my UFUNCTION declaration my method is working fine.

https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116753

But with the declaration of UFUNCTION, i got this error:

https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116754

Any idea ?

Hi,

In your header file, your function decleration should appear as follows:


AMyActorTest* SpawnObjectActor(AActor * ActorReference, UClass * ClassReference, FVector Location, FRotator Rotation);

and in your cpp file your function definitions should be as follows


 AMyActorTest* AMyActorTest::SpawnObjectActor(AActor * ActorReference, UClass * ClassReference, FVector Location, FRotator Rotation) {     ... Your code .... } 

Note the difference between the AMyActorTest:: appearing in the .cpp file and not in the .h file. This is known as scope referencing… you don’t need to include it in the header file because the decleration is already contained in the AMyActorTest class. Wherease in the .cpp file, it has no way of knowing where that method is coming from because it has no context of the class/scope. Thus adding AMyActorTest:: to the function lets it know that the function is declared in the AMyActorTest class. Once doing this, it should be safe for you to put the UFUNCTION back in, as that looks correct to me. Thanks, Nick

Ah thanks you mate you right :slight_smile:
Thanks for your help NickCullent, that was a noob problem ^^

Well, my function doesn’t work when i call it on my BluePrint:
Actor is not spawning.

Here my code .h:



    UFUNCTION(BlueprintCallable, Category = "Mesh Character")
    UClass* SpawnObjectActor(UClass* ClassReference, FVector Location, FRotator Rotation, FTransform SpawnTransform, APawn * InstigatorActor);


And here my cpp code:



UClass* AMyActorTest::SpawnObjectActor(UClass* ClassReference, FVector Location, FRotator Rotation, FTransform SpawnTransform, APawn * InstigatorActor) {

    UWorld* const World = ClassReference->GetWorld();


    FActorSpawnParameters SpawnParams;

    //AMyActorTest* const SpawnedActor = World->SpawnActor<AMyActorTest>(GetClass(), Location, Rotation, SpawnParams);
    ClassReference = World->SpawnActor<UClass>(ClassReference, SpawnTransform, InstigatorActor);


Anyone have an idea why my actor is not spawning ?
Thanks

You’re trying to cast an Actor to a UClass - which isn’t going to work. Spawning an actor is pretty simple, just call it from the World object (which you can get from any Actor in that world, or via a couple engine calls), pass it the class you want to spawn, and it’ll return a new actor that inherits from that class.



 UFUNCTION(BlueprintCallable, Category = "Mesh Character")  AActor* SpawnObjectActor(UClass* ClassReference, FTransform SpawnTransform);

AActor* AMyActorTest::SpawnObjectActor(UClass* ClassReference, FTransform SpawnTransform)  
{      
    UWorld* const World = GetWorld();  

    return World->SpawnActor<AActor>(ClassReference, SpawnTransform);

 }



Thanks for your reply,
But it’s not spawning, i think we need to add the collision handling

There is a flag in the FActorSpawnParameters that allows you to force it to spawn, regardless of collision (or alternatively, let it adjust the spawn location) so you could set that and pass it in to the SpawnActor call.

Toss a break point in there and see what is happening would be your next step.

I try this and many things but always not spawn:

https://forums.unrealengine.com/filedata/fetch?type=thumb

FActorSpawnParameters->bNoCollisionFail is deprecated i think, i need to use enum ESpawnActorCollisionHandlingMethod, do you know how to use ?

Do you have an idea how to add it ?

https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116788

I made this:

https://forums.unrealengine.com/filedata/fetch?type=thumb&filedataid=116789

But something goes wrong, it’s still not spawning

It should be



// ...
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
//...



Again, you may need to place a break point, step into SpawnActor and follow it to see what is going on.

It’s working fine when i call my function made in C++ in my child BluePrint, it’s in another class who doesn’t hinerit than it doesn’t work, even with a reference.
But it’s ok.
Problem is, i don’t know if my function is more optimize compared to the “spawnactor” function already made in BluePrint.
At least i will can continue my C++ code with this function.