Search code examples
c++unreal-engine4

Compile errors for C++ Battery Collector tutorial


Sorry very noob question incoming, I have been working through the C++ Battery Collector tutorial from Unreal and I am having some compile issue with the code in the tutorial.

I have followed along in Visual studio and when creating the accessor methods in the header file I used the Intelli sense to generate the corosponding class.

Pickup.h

#pragma once

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

UCLASS()
class BATTERYCOLLECTOR_API APickup : public AActor
{
    GENERATED_BODY()

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

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

    // Return the mesh for the pickup
    FORCEINLINE class UStaticMeshComponent* GetMesh() const { return PickupMesh; }

    // Return whether or not the pickup is active
    UFUNCTION(BlueprintPure, Category = "Pickup");  // <--- error in this line
    bool IsActive();

    // Allows other classes to safely change whether or not the pickup is active 
    UFUNCTION(BlueprintCallable, Category = "Pickup");
    void setActive(bool NewPickupState);

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

    //True when the pickup can be used, and false when pickup is deactivated
    bool bIsActive;

private:
    //Static mesh to represent the pickup in the level
    UPROPERTY(visibleAnywhere, BlueprintReadOnly, Category = "Pickup", meta = (AllowPrivateAccess = "true"))
    class UStaticMeshComponent* PickupMesh;

};

Pickup.cpp

#include "Pickup.h"

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

    // All pickups start active
    bIsActive = true;

    // Create the static mesh component
    PickupMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("PickupMesh"));

}

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

}

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

}

// Visual studio created this definition 
//APickup::UFUNCTION(BlueprintPure, Category)
//{
    //What is should go in here?
//}

// Returns active state
bool APickup::IsActive()
{
    return bIsActive;
}

// Changes active state
void APickup::setActive(bool NewPickupState)
{
    bIsActive = NewPickupState;
}

On the line where I added the comment Visual studio created this definition in the .cpp file this was the definition added by visual studio code but I am not sure what is should be used for and it is failing to compile without this. (This definition was not in the tutorial but VS wants me to add it.)

As far as I can see I have defined the methods for both the UFUNCTIONS however if I don't have the the generated definition I get compile errors in the unreal engine.

D:/Unreal Projects/BatteryCollector/Source/BatteryCollector/Pickup.h(25) : Error: Function return type: Missing variable type

Can anyone see what I am doing wrong or missing?


Solution

  • From the Unreal documentation:

    UFUNCTION([specifier, specifier, ...], [meta(key=value, key=value, ...)])
    ReturnType FunctionName([Parameter, Parameter, ...])
    

    There is no ; in that declaration and most likely that is the problem.

    Note that in the course of editing the question you added a ; also to the other declaration, but you should rather remove both.

    PS: I post this as an answer in the hope of turning semi-spammy comments into something useful. I dont know Unreal and maybe this answer is wrong, then just let me know.