I’m new to programming in UE4, I’m not sure how it all works. All I’ve gathered so far is that doing things causes crashes.
I’ve set up the following public property and functions in my actor class as follows:
Territory.h:
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* terrMesh;
UFUNCTION(BlueprintCallable, Category="functions")
void GenerateArmy(int32 troops);
UFUNCTION(BlueprintCallable, Category = "functions")
void ChangeMesh();
UFUNCTION(BlueprintCallable, Category = "thing")
int32 testFunction();
Territory.cpp:
ATerritory::ATerritory()
{
PrimaryActorTick.bCanEverTick = true;
static ConstructorHelpers::FObjectFinder<UStaticMesh> meshObj(TEXT("/Game/Geometry/Meshes/TemplateFloor.TemplateFloor"));
terrMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("placeolder"));
terrMesh->SetStaticMesh(meshObj.Object);
}
void ATerritory::BeginPlay()
{
Super::BeginPlay();
//Copy-pasted generateArmy
FVector loc = this->GetActorLocation();
GetWorld()->SpawnActor<AActiveArmy>(GetClass(), (loc + FVector(0.0, 0.0, 100.0)), FRotator::ZeroRotator);
}
void ATerritory::Tick( float DeltaTime )
{ //snip }
void ATerritory::GenerateArmy(int32 troops)
{
FVector loc = this->GetActorLocation();
/*armyDood =*/ GetWorld()->SpawnActor<AActiveArmy>(GetClass(), (loc + FVector(0.0, 0.0, 100.0)), FRotator::ZeroRotator);
armyDood->troopCount = 42;
}
void ATerritory::ChangeMesh()
{
static ConstructorHelpers::FObjectFinder<UStaticMesh> fountainObj(TEXT(
"/Game/InfinityBladeGrassLands/Environments/Plains/Env_Plains_Ruins/StaticMesh/SM_Plains_Castle_Fountain_01.SM_Plains_Castle_Fountain_01"));
//terrMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("newMesh"));
terrMesh->SetStaticMesh(fountainObj.Object);
}
int32 ATerritory::testFunction()
{
return 0;
}
I’ve tried calling each of the three functions via a trigger blueprint. GenerateArmy and ChangeMesh cause a crash whenever they’re called but the TestFunction executes fine.
I’ve also tried directly calling both the functions and the code contained in the functions in BeginPlay. Every time I got a crash. This leads me to believe that there’s something wrong with the way I’m writing the code, not something wrong with the blueprints.
Also, here’s the ActiveArmy class:
ActiveArmy.h:
protected:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta=(AllowPrivateAccess = "true"))
class UTextRenderComponent* troopText;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
class UStaticMeshComponent* armyMesh;
public:
default stuff
UPROPERTY(BlueprintReadWrite, EditAnywhere)
int32 troopCount;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
int32 teamNum;
UPROPERTY(BlueprintReadWrite, EditAnywhere)
class ATerritory* homeTerritory;
ActiveArmy.cpp:
AActiveArmy::AActiveArmy()
{
FVector loc = this->GetActorLocation();
PrimaryActorTick.bCanEverTick = true;
//Set up static mesh
static ConstructorHelpers::FObjectFinder<UStaticMesh> meshObj(TEXT("/Game/Geometry/Meshes/1M_Cube.1M_Cube"));
armyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("meshObj"));
armyMesh->SetStaticMesh(meshObj.Object);
//Set up text render
troopText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("TextRender"));
troopText->SetText(FText::AsNumber(troopCount));
troopText->SetRelativeLocation(loc + FVector(0.0f, 0.0f, 20.0f));
}
void AActiveArmy::BeginPlay()
{
Super::BeginPlay();
if (homeTerritory)
{
FVector newLoc = homeTerritory->GetActorLocation() + FVector(0.0, 0.0, 20.0);
SetActorLocation(newLoc);
}