Unreal Actor Constructor Setup & Errors

Fat Scout
Sat, 12 Feb 2022 08:15:28 +0000
C++

Howdy! so I’ve been diving into c++ actor building recently and I’ve been encountering a few problems within my constructor. Random things don’t give expected results or cause crashes and I think it might have to do with how an actor instantiates but Idk.

So, here’s some code for a character constructor:

ALlamaCharacter::ALlamaCharacter()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	//**************Part 1 - Static references for instantiation. Get Assets needed for Character*****************//
	static ConstructorHelpers::FObjectFinder<UStaticMesh> ConeFileRef(TEXT("StaticMesh'/Game/Geometry/Cone.Cone'"));
	static ConstructorHelpers::FObjectFinder<USkeletalMesh> LlamaFileRef(TEXT("SkeletalMesh'/Game/Llama/LlamaModel/LlamaCharacter.LlamaCharacter'"));
	static ConstructorHelpers::FObjectFinder<UMaterialInterface> FootMatRef(TEXT("MaterialInstanceConstant'/Game/Llama/Textures/LlamaFootMatInstance.LlamaFootMatInstance'"));
	bool allFilesLoaded = (ConeFileRef.Succeeded() && LlamaFileRef.Succeeded() && FootMatRef.Succeeded()) != false;
	UE_LOG(LogTemp, Warning, TEXT("File Ref succeeded : %d"), allFilesLoaded);


	//**************Part 2 - instantiate all Compoenents. Set default values and positions*****************//
	// 2a--skeletal mesh
	LlamaMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("Mesh"));
	//currently the mesh I made was too large so it needs to be scaled down.
	LlamaMesh->SetRelativeScale3D(FVector(0.5f, 0.5f, 0.5f));

	SetRootComponent(LlamaMesh);

	if (ConeFileRef.Succeeded()) {
		//LeftFrontFoot->SetStaticMesh(ConeFileRef.Object);
		//RightFrontFoot->SetStaticMesh(ConeFileRef.Object);
		//LeftBackFoot->SetStaticMesh(ConeFileRef.Object);
		//RightBackFoot->SetStaticMesh(ConeFileRef.Object);

		LlamaMesh->SetSkeletalMesh(LlamaFileRef.Object);
	}

	// 2c--feet Meshes. The material call belows don't seem to work so I am avoiding them for now.
	//UMaterialInstanceDynamic* FootMat = UMaterialInstanceDynamic::Create(FootMatRef.Object, this);
	//FootMat->SetVectorParameterValue("Color", FLinearColor(0.942708, 0.133357, 0.866919, 1.0));
	//LeftFrontFoot->SetMaterial(0, FootMat);

	//testing making the left front foot
	LeftFrontFoot = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Left Front Foot"));
	LeftFrontFoot->SetCollisionEnabled(ECollisionEnabled::NoCollision);
	FName bone = "l_front_foot";
	bone = LlamaMesh->GetBoneName(24);
	LeftFrontFoot->SetWorldLocation(LlamaMesh->GetBoneLocation(bone, EBoneSpaces::WorldSpace));
	UE_LOG(LogTemp, Warning, TEXT("Bone Location : %s"), *LlamaMesh->GetBoneLocation(bone, EBoneSpaces::WorldSpace).ToString());
	UE_LOG(LogTemp, Warning, TEXT("Bone Location : %s"), *bone.ToString());

	RightFrontFoot = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Right Front Foot"));

	LeftBackFoot = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Left Back Foot"));
	RightBackFoot = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Right Back Foot"));



}

Now, several things are commented out because for unknown reasons (to me) they crash my program
for example, the if statement on line 20ish which will set the meshes refrences crashes the program if I try to set their world location below. I commented all of them out except the skeletal mesh set and it works (?). Also, the Material functions also cause a crash. and on top of this, I cannot get a bone location despite having the exact name (I checked).

So what am I missing here? All of these different problems are unrelated except for the fact that they involve using a constructorHelper to reference things in the content folder; and I know these references work because I made a check to confirm (the bool allFilesLoaded). At least I’m 95% confident.

I have to me missing something and if someone sees or knows about it I would appreciate it. Maybe I’m going about setting up the default elements of my Actor all wrong; or I’m using the wrong functions. Idk. But whatever I’m doing here is causing a lot of crashes (they often are access violation so it could be the references but then why is my bool set to true?).
Thank you.

1 post - 1 participant