Hi!
I’m developing a game with Unreal Engine 5.2. and C++.
I have this structure:
USTRUCT(BlueprintType)
struct FBall
{
GENERATED_BODY()
[...]
class ABallActor* BallActor;
[...]
};
The class ABallActor has this method:
void ABallActor::HideBallActor(bool toHide)
{
if (toHide != IsHiden)
{
SetActorHiddenInGame(toHide);
SetActorEnableCollision(!toHide);
SetActorTickEnabled(!toHide);
IsHiden = toHide;
}
}
I have a class that inherits from UGameInstanceSubsystem where I create all the instances of ABallActor. This method uses an instance variable to store the actors created in a TArray<FBall> TheBalls.
TheBalls is defined:
private:
TArray<FBall> TheBalls;
And the Show method is:
void UMyGameInstanceSubsystem::Show(
TSubclassOf<class ABallActor> BlueprintBall,
float Radius) const
{
TArray<AActor*> FoundActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), ABallsSphereActor::StaticClass(), FoundActors);
if (FoundActors.Num() == 1)
{
if (ABallsSphereActor* BallsSphere = Cast<ABallsSphereActor>(FoundActors[0]))
{
const FAttachmentTransformRules AttachmentTransformRules =
FAttachmentTransformRules(EAttachmentRule::KeepWorld, true);
for (FBall& ball : TheBalls)
{
FVector SpawnLocation;
[...]
ball.BallActor = Cast<ABallActor>(
GetWorld()->SpawnActor(BlueprintBall, &SpawnLocation));
[...]
ball.BallActor->AttachToActor(BallsSphere, AttachmentTransformRules);
ball.BallActor->HideBallActor(!ball.ToShow);
}
}
}
}
In the above method, ball.BallActor->HideBallActor(!ball.ToShow); works fine.
But, after showing the ball, I use HideBallActor again in another method:
void UMyGameInstanceSubsystem::Update()
{
for (FBall& ball : TheBalls)
{
ball.ToShow = [...];
if (ball.BallActor)
ball.BallActor->HideBallActor(ball.ToShow);
}
}
I get an exception in this sentence (inside the HideBallActor method):
if (toHide != IsHiden)
The exception is:
An exception occurred: read access violation. **this** fue 0xFFFFFFFFFFFFFBE7.
I think the problem could be storing the spawned ball actors into the TArray<FBall> TheBalls array. But, it’s strange because it works inside the Show method.
How can I fix this problem?
Thanks!