How to write UPROPERTY?

The following is my code for NPC which is a sub-class of a chracter

UCLASS()
class GOLDENEGG_API ANPC : public ACharacter
{
	GENERATED_UCLASS_BODY()
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	TSubobjectPtr<class USphereComponent> ProxSphere;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	FString NpcMessage;
	// When you create a blueprint from this class, you want to be
	// able to edit that message in blueprints,
	// that's why we have the EditAnywhere and BlueprintReadWrite
	// properties.
}

The compiler returns missing ; on line 25 (which is after the closing bracket)

I’ve tried to simplify it and only adds UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision) But it still return an error.

How to properly write UPROPERTY?

Hi Dratone, thanks for the input.

I’ve tried that before and still have compiler error.

If I remove everything except UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision) it will return “Missing variable type”. Any idea what that might be?

Cheers

Its not the UPROPERTY that is missing the ;.

Try to add a ; after your closing curly bracket of your ANPC class. I.e.:

 UCLASS()
class GOLDENEGG_API ANPC : public ACharacter
{
GENERATED_UCLASS_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
TSubobjectPtr<class USphereComponent> ProxSphere;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
FString NpcMessage;
// When you create a blueprint from this class, you want to be
// able to edit that message in blueprints,
// that's why we have the EditAnywhere and BlueprintReadWrite
// properties.
};

After a UPROPERTY you always need to define the actual variable. The UPROPROPERTY macro generates code that needs that variable.

Could you paste the complete file? It might be that you have other syntax errors in other parts of the file.

The completed code is as follow

UCLASS()
class GOLDENEGG_API ANPC : public ACharacter
{
GENERATED_UCLASS_BODY()
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category =
Collision)
TSubobjectPtr<class USphereComponent> ProxSphere;
// This is the NPC's message that he has to tell us.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
NPCMessage)
FString NpcMessage;
// When you create a blueprint from this class, you want to be
// able to edit that message in blueprints,
// that's why we have the EditAnywhere and BlueprintReadWrite
// properties.
}

And the CPP file have nothing inside.

Can u tell me how to define the variable of the UPROPERTY? The book doesn’t seems to declare anything and it’s the complete code that was given in the text. I’m using 4.7 atm the tutorial using 4.5 build. I hope it doesn’t make any problem. Can you please tell me how to declare the variable to resolve this issue?

You need to add the ; at the end of the Class body. So after the last }

After that, come back and post the new compile error.

We can’t help you if you don’t give us the exact errors.

He already told you how to fix the “;” error, now fix it and come back with the new one. (:

Sorry about that guys. I’ve forgot to pasted the new code.

This is the header file

#pragma once

#include "GameFramework/Character.h"
#include "NPC.generated.h"

/**
 * 
 */
UCLASS()
class GOLDENEGG_API ANPC : public ACharacter
{
	GENERATED_UCLASS_BODY()
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	//TSubobjectPtr<class USphereComponent> ProxSphere;
	//UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	//FString NpcMessage;
	// When you create a blueprint from this class, you want to be
	// able to edit that message in blueprints,
	// that's why we have the EditAnywhere and BlueprintReadWrite
	// properties.
};

The CPP file is empty.

I’m following a tutorial where I make a subclass out of character. And the above are the codes I need to paste on the header file. Strangely it didn’t work for me. Could you please which variable to declare?

I;m just trying to expose this class to the blue print I;'m not sure what to declare. Again thanks for the help. My compiler error is still the same as above “Missing variable type”

You are not declaring any variable anymore. Try something like this:

 #pragma once
 
 #include "GameFramework/Character.h"
 #include "NPC.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class GOLDENEGG_API ANPC : public ACharacter
 {
     GENERATED_UCLASS_BODY()
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
     FString NpcMessage;
     // When you create a blueprint from this class, you want to be
     // able to edit that message in blueprints,
     // that's why we have the EditAnywhere and BlueprintReadWrite
     // properties.
 };

Hey Dratone I got it working. Thank you very much for your help.

I’m using 4.7 and TSubobjectPtr can no longer be used.

This is the final code for viewers. Bare the noobness I just started C++.

#pragma once

#include "GameFramework/Character.h"
#include "NPC.generated.h"

/**
*
*/
UCLASS()
class GOLDENEGG_API ANPC : public ACharacter
{
	GENERATED_UCLASS_BODY()
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Collision)
	USphereComponent* ProxSphere;

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage)
	FString NpcMessage;

	// When you create a blueprint from this class, you want to be
	// able to edit that message in blueprints,
	// that's why we have the EditAnywhere and BlueprintReadWrite
	// properties.
};

Cpp file

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

#include "GoldenEgg.h"
#include "NPC.h"




    ANPC::ANPC(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
    {
    	//GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "interact");
    }

I got that same error message once bc I had accidentally added a ; after the UPROPERTY parenthesis i.e.

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = NPCMessage); ← a semicolon here will cause this error
FString NpcMessage;

I don’t see a typo like that in the section of code you’ve given us, but keep an eye out for that in the rest of your file.

Would be nice you mark the right answer as the answer, please.

Same here …" ; " caused the issue …