Unreal Engine 4.18 C++ Basics Class Definition Part-2
Class File and Header File Introduction
Class file generate the files open in your IDE so you can editing it.
Code:::
#include "GameFramework/Actor.h"
#include "MyActor.generated.h"
UCLASS()
class AMyActor : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyActor();
// Called every frame
virtual void Tick( float DeltaSeconds ) override;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
};
Class constructor below contains the line..
AMyActor::AMyActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you do not need it.
PrimaryActorTick.bCanEverTick = true;
}
Making a property show up in the Unreal Engine Editor
Class created so let’s make some property and showing on editor.
Special Marco Of Unreal Engine called it.
UPROPERTY().
All you have to do is use the
UPROPERTY(EditAnywhere) maro.
-> MyActor.h (in Your header file)
UCLASS()
class AMyActor : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere)
int32 TotalDamage;
...
};
Passing more information::
UPROPERTY(EditAnywhere, Category="Damage")
//Damage is heading marked with category name
int32 TotalDamage; //total damage variable..
Now add property for to Blueprint::
BlueprintReadOnly for const (fix value or do not change the value const float pi = 3.14f ) in Blueprint.
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Damage")
int32 TotalDamage;
Let’s add Code::
UCLASS()
class AMyActor : public AActor
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Damage")
int32 TotalDamage;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Damage")
float DamageTimeInSeconds;
UPROPERTY(BlueprintReadOnly, VisibleAnywhere, Transient, Category="Damage")
float DamagePerSecond;
...
};
DamageTimeInSeconds Only is property the designers can modify.
DamagePerSecond property is a calculated value using the designers setting.
VisibleAnywhere flag marks propery as viewable. Not Editable in Editor.
You can see this in Editor.
Add Default Values in constructor
Setting up default values in constructor works with same as your typical C++ class.
Note -> this define in AMyActor.cpp file
AMyActor::AMyActor()
{
TotalDamage = 200; // in header file define
DamageTimeInSeconds = 1.f; // in header file define
}
AMyActor::AMyActor() :
TotalDamage(200),
DamageTimeInSeconds(1.f)
{
}
Default values in Editor :
Set values by hooking into the PostInitProperties() call chin.
If default value gives by designer this working with our gives values.
Else engine automatically set that property values zero or nullptr in this case.
void AMyActor::PostInitProperties()
{
Super::PostInitProperties();
DamagePerSecond = TotalDamage / DamageTimeInSeconds;
}
We change the vales in editor Damage Per Second is changed.
Comments
Post a Comment