Compiling issues with Introduction to C++ Programming (Documentation)

Greetings, everyone.

I am having issues compiling the example under Introduction to C++ Programming using Visual Studio 2013 Community. The game consist of a header file (Actor.h) and a source file (Actor.cpp). It compiles for the generated c++ class. But it doesn’t when I add the UPROPERTIES. See the attachment for the list of compiling errors. I will appreciate any help you may provide me. Thank you.

This is the header file Actor.h:



#pragma once
 
 #include "GameFramework/Actor.h"
 #include "MyActor.generated.h"
 
 UCLASS()
 class CODEPROJECT_API AMyActor : public AActor
 {
     GENERATED_BODY()
 
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
     int32 TotalDamage;
 
     UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Damage")
     float DamageTimeInSeconds;
 
     UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Transient, Category = "Damage")
     float DamagePerSecond; 
 
 public:    
     // Sets default values for this actor's properties
     AMyActor();
 
     // Support per instace designer set properties
     virtual void PostInitProperties() override;
 
     // Called when the game starts or when spawned
     virtual void BeginPlay() override;
     
     // Called every frame
     virtual void Tick( float DeltaSeconds ) override;
 };

This is the implementation file Actor.cpp:



// Fill out your copyright notice in the Description page of Project Settings.
 
 #include "CodeProject.h"
 #include "MyActor.h"![alt text][1]
 
 
 // Sets default values
 AMyActor::AMyActor()
 {
      // 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;
 
 }
 
 void AMyActor::PostInitProperties()
 {
     Super::PostInitProperties();
     DamagePerSecond = TotalDamage / DamageTimeInSeconds;
 } 
 
 // Called when the game starts or when spawned
 void AMyActor::BeginPlay()
 {
     Super::BeginPlay();
     
 }
 
 // Called every frame
 void AMyActor::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
 }


To fix it you can do two things:



UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Transient, Category = "Damage")
float DamagePerSecond;


Move that underneath ‘public:’, but this isn’t good for encapsulation as you probably already know.

The other solution is to leave it where it is, but you need to pass a specific property that I can’t quite remember into meta = …

Change the following lines before the “public:” to this:


  UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Damage", meta = (AllowPrivateAccess = "true"))
     int32 TotalDamage;
 
     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Damage", meta = (AllowPrivateAccess = "true"))
     float DamageTimeInSeconds;
 
     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Damage", meta = (AllowPrivateAccess = "true"))
     float DamagePerSecond; 

Whenever you want to modify private variables inside of blueprints, you must do add this tag to your UPROPERTY macro after Category:


meta = (AllowPrivateAccess = "true")

With VisibleAnywhere and BlueprintReadOnly.

Then use Getter and Setter methods to access the variable through other C++ classes. This is the best way to approach to exposing variables to blueprints because sometimes, for example, if you had a Mesh that was exposed to blueprints with “EditAnywhere” and “BlueprintReadWrite” then you could change the entire mesh to a completely different thing which would essentially cause problems.

So whenever you want to expose your variables to a blueprint, use the above tag in the UPROPERTY macro

Thank you MaxL. You’re answer was helpfull. I tried the first option and it worked. Unfortunately, like you said, it doesn’t go well with encapsulation. However, I found out that meta = … may go inside of UCLASS. According to UE4 documentation:


UCLASS([Specifier, Specifier, ...], [meta(key=value, key=value, ...])

If anyone else knows more about the subject I’ll appreciate any help you may provide me. Thank you.

This goes perfect with encapsulation. It is what I was looking for. Thank you, very much.