Download

Variable already defined when UPROPERTY is used

Hi all!

I have this header:



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

#pragma once

#include "Components/ActorComponent.h"
#include "MovementHelper.generated.h"


USTRUCT(BlueprintType)
struct FMovementFlags
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Movement flags")
    bool goForward;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Movement flags")
    bool goBackwards;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Movement flags")
    bool goLeftyards;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Movement flags")
    bool goRightwards;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Movement flags")
    bool goUp;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Movement flags")
    bool goDown;
};


USTRUCT(BlueprintType)
struct FRotationFlags
{
    GENERATED_USTRUCT_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Rotation flags")
    bool rotateUp;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Rotation flags")
    bool rotateDown;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Rotation flags")
    bool rotateLeft;

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper|Rotation flags")
    bool rotateRight;
};


/**
* This component allows to move and rotate an actor very easily at a fixed speed.
*
* Jumping is managed by the CharacterMovementComponent, so, if you want to jump,
* you should add this component to a Character actor.
*
* If this actor is a Pawn (or derived), you can also use Pawn AddMovementInput,
* AddControllerPitchInput, AddControllerRollInput and AddControllerYawInput
* methods if you want a more gradual movement.
*
* To use these, you'll need to create axes in the project's settings.
*/
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class UMovementHelper : public UActorComponent
{
	GENERATED_BODY()

public:
    **// Flags for actor movement
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper")
    FMovementFlags movementFlags;

    // Flags for actor rotation
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Helpers|Movement Helper")
    FRotationFlags rotationFlags;**

    // The speed at which the actor will rotate
    UPROPERTY(BlueprintReadWrite, Category = "Movement")
    float moveSpeed;

    // The speed at which the actor will move
    UPROPERTY(BlueprintReadWrite, Category = "Movement")
    float rotateSpeed;


    // Sets default values for this component's properties
	UMovementHelper();

	// Called when the game starts
	virtual void InitializeComponent() override;
	
	// Called every frame
	virtual void TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction ) override;

    // Makes this actor jumping. Only works if the actor is a character
    UFUNCTION(BlueprintCallable, Category = "Movement")
    void Jump();
};


And i have a problem with the lines in bold.

If i remove the UPROPERTY macro from movementFlags and rotationFlags variables, everything compiles fine.
But, with the UPROPERTY macro, i get this error:

“error : In MovementHelper: Member variable declaration: ‘movementFlags’ already defined M:\Documentos\Copy\Programacion\Proyectos\UE4\GameFactoryCPP\Source\GameFactoryCPP\Components\MovementHelper.h 73 1”

What i’m doing wrong? :S

See ya!

Had this issue before when using a variable name almost equal to class name, in this case FMovementFlags and movementFlags. Try naming your variable something different like myMovementFlags.

Waw, it works! :open_mouth: .
I added a simple underscore before the variable names and now compiles like a charm :D.

Thanks a lot, also for answering so quickly :slight_smile: .

See ya!