Hello. Help a newbie. In a new C++ project I’m creating a character called Cube.
ACube::ACube()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you dont need it.
PrimaryActorTick.bCanEverTick = true;
static ConstructorHelpers::FObjectFinder<UStaticMesh>C(TEXT("StaticMesh'/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube'"));
UStaticMeshComponent* Cube = CreateDefaultSubobject<UStaticMeshComponent>("Cube");
Cube->SetupAttachment(RootComponent);
Cube->SetStaticMesh(C.Object);
}
#include "Cube.h"
// Sets default values
ACube::ACube()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you dont need it.
PrimaryActorTick.bCanEverTick = true;
static ConstructorHelpers::FObjectFinder<UStaticMesh>C(TEXT("StaticMesh'/Game/Shapes/1M_Cube_Chamfer.1M_Cube_Chamfer'"));
UStaticMeshComponent* Cube = CreateDefaultSubobject<UStaticMeshComponent>("Cube");
Cube->SetupAttachment(RootComponent);
Cube->SetStaticMesh(C.Object);
}
// Called when the game starts or when spawned
void ACube::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void ACube::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
// C++ version of tick
FString m = "Cube is ticking from c++ Delta :" + FString::SanitizeFloat(DeltaTime);
ShowMessage(m, FColor::Orange,2);
}
void ACube::ShowMessage(FString Message, FColor Color, int32 key)
{
if (GEngine) {
GEngine->AddOnScreenDebugMessage(key, 1, Color, Message);
}
}
Thank you friend, you spent a lot of time on me.
I figured it out a little, I have to put the Blueprint version of the Cube on stage then only the Blueprint works. So the question is closed and I have another question.
I created a variable in blueprint.
Then from within the blueprint derived from the function you could set a blueprint variable or retrieve it by implementing the functions (they will show up to be implemented in the functions section on the left side inside of the blueprint).
This way is less susceptible to breaking if you change the variable from within blueprints.
At startup I have two cubes on the stage, and the message is displayed once. In the second cube Blueprint does not work. Can I create a Blueprint version of the second cube so that Blueprint can work in it too?
You shouldn’t be spawning the cube at begin play of the same class. You will cause the engine to run a recurring function, each following cube will spawn a next one causing the engine to lock up.
If you want to spawn a specific class derived from a class then you can use TSubclassOf
Then you can pass in the blueprint variation of the class.
Note: This does not let you access the bp’s variables created via blueprints though only the c++ ones.
The TSubClassOf is just a way to spawn blueprint object variants of a c++ class from within c++. Normally you don’t have access to them directly.
If you expose a variable to the engine as TSubClassOf then you can pick the corresponding blueprint actor from within the engine.
Then you can modify if from withing c++ (at least the attributes that are exposed to the c++ class)
Normally there is a barrier between c++ and blueprints and this is a way to get around it