Hey folks,
I’m new to UE5 and I’m having some troubles here. Since I’ve been debugging this problem for several days (without any step further) and I still don’t understand what I’m doing wrong, the time has come to ask you.
I’m basically trying to make a cube blink in the standard 1st person template game in standalone mode. What I do is substantially the following (cutting hopefully useless stuff to keep things clearer):
//# Called by AReplaysCharacter::BeginPlay()
void UReplayGameInstance::BeginPlay()
{
PreloadMaterials(RegexList, MaterialList);
auto Actor = FindActor("SM_ChamferCube4", true);
auto ActorLabel = UKismetSystemLibrary::GetDisplayName(Actor);
auto SMC = GetStaticMeshComponent(Actor);
//# oldchamfermat and newchamfermat are class members with type UMaterialInterface *
oldchamfermat = SMC->GetMaterial(0);
newchamfermat = Cast<UMaterialInterface>(LoadedMaterials[0].material);
}
LoadedMaterials is just a TArray<LoadedMaterial>
(a struct that matches Actors’ regexes and materials) where I load my materials in this way:
auto material = LoadMaterialInstanceDynamicFromPath(FName(*sPath));
Then, in Tick()
I try to switch materials:
void UReplayGameInstance::Tick(float DeltaTime)
{
tick_counter += 1;
if (tick_counter % 50 == 0)
{
auto Actor = FindActor("SM_ChamferCube4", true);
auto SMC = GetStaticMeshComponent(Actor);
if ((tick_counter/10) % 2 == 0)
{
SMC->SetMaterial(0, newchamfermat);
}
else
{
SMC->SetMaterial(0, oldchamfermat);
}
}
}
FindActor()
is just this:
AActor * UReplayGameInstance::FindActor(const FString & name, bool strict)
{
TArray<AActor*> AllActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AActor::StaticClass(), AllActors);
for (AActor* Actor : AllActors)
{
auto ActorLabel = UKismetSystemLibrary::GetDisplayName(Actor);
if (!strict and ActorLabel.Contains(name))
{
return Actor;
}
else if (ActorLabel == name)
{
return Actor;
}
}
return nullptr;
}
And GetStaticMeshComponent()
only does this:
UStaticMeshComponent * UReplayGameInstance::GetStaticMeshComponent(AActor *Actor)
{
if (Actor)
{
auto ActorLabel = UKismetSystemLibrary::GetDisplayName(Actor);
if (AStaticMeshActor* StaticMeshActor = Cast<AStaticMeshActor>(Actor))
{
UStaticMeshComponent* StaticMeshComponent = StaticMeshActor->GetStaticMeshComponent();
if (StaticMeshComponent && StaticMeshComponent->GetStaticMesh())
{
return StaticMeshComponent;
}
}
}
}
After a random amount of ticks, sometimes just a thousand, sometimes tens of thousands, i get one of these SIGSEGVs:
Unhandled Exception: SIGSEGV: unaligned memory access (SIMD vectors?)
Unhandled Exception: SIGSEGV: invalid attempt to read memory at address 0x0000000000000000
I’ve already tried printing most of the pointers, and none of them are ever null.
What puzzles me the most is that all the pointers, even those of my dynamic materials loaded at the beginning in BeginPlay() keep changing. The addresses keep moving forward, as if the objects were de-allocated and re-allocated every time.
I read that in UE5 one should always use the smart pointers that it makes available, but then I don’t understand why GetAllActorsOfClass()
and many other functions return raw pointers instead of smart pointers.
I’m using UE5.2 under Ubuntu 22.04 x86_64, anyone have any idea what I’m doing wrong?
Thanks!