Download

ToggleVisibility() causes a crash

calling this function from another class causes a crash. why?

void AFlashLight::ToggleOnOff()
{
if (FlashLight)
{
FlashLight->ToggleVisibility();
}
}

Use IsValid instead:



...
if (IsValid(FlashLight))
...


If still not, the we need the stack error or more info.

Our flashlight uses:



_flashlight->SetActorHiddenInGame(true / false);


@Shmoopy1701 is right. *ToggleVisibility *should only be used for components. For actors, SetActorHiddenInGame.

guys i found the problem in the other class where I was calling the function from nullptr

void AMain::ToggleFlashLight()
{

AFlashLight* FlashLightRef;

if (bHasFlashlight && FlashLightRef)
{
UGameplayStatics::PlaySoundAtLocation(GetWorld(), ClickSound, GetActorLocation());
FlashLightRef->ToggleOnOff();
}
}

now i have got a bigger problem. i don’t quite know the function to get child actors to initialize the pointer. Main class is the owner of AFLashLight and the actor is also attached to main character when this function is called I’m sure there’s a way to access child classes?

How did you created and attached the flashlight? Can’t you get the reference from there? It would be a lot easier that way. Did you do it in C++ or in BP?

In any case, there are lot of ways to get what you want. You can for example do something like this:



TArray<AActor*> actors;
GetAttachedActors(actors);

for (auto& actor : actors)
{
   if (actor->ActorHasTag("flashlight"))
   {
      actor->SetActorHiddenInGame(true);
   }
}


where I used *tags *to id the flashlight.

this is how the flashlight gets attached on begin overlap

void AFlashLight::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor)
{
AMain* Main = Cast<AMain>(OtherActor);
if (Main)
{
UE_LOG(LogTemp, Warning, TEXT(“Cast succeded”));
Root->AttachToComponent(Main->FlashLightArmComponent, FAttachmentTransformRules::SnapToTargetIncludingScale);
Main->bHasFlashlight = true;
Box->DestroyComponent();
}
}
}

also i don’t want to hide the actor in the game cuz its flashlight. i want be able to turn it on and off and I know what I’m supposed to do except one part and that is Creating a an instance from the flashlight class, that’s the tricky part but I assume if the flashlight is attached to main it should give me some solution or function to get access to the flashlight right?

Then you have everything you need. When you attach the flashlight, also set the flashlight reference:

In AMain.h:



**class AFlashLight* FlashLightRef = nullptr;**


In AFlashlight.cpp



void AFlashLight::OnBeginOverlap(UPrimitiveComponent* OverlappedComponent, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
    if (OtherActor)
   {
      AMain* Main = Cast<AMain>(OtherActor);
      if (Main)
      {
         UE_LOG(LogTemp, Warning, TEXT("Cast succeded"));
         Root->AttachToComponent(Main->FlashLightArmComponent, FAttachmentTransformRules::SnapToTargetIncludingScale);
         Main->bHasFlashlight = true;
         Box->DestroyComponent();
         **Main->FlashLightRef = this;**
      }
   }
}


Now, you can use the *ToggleFlashLight function … just remove the AFlashLight FlashLightRef; line from it.

my brain is too small to come up with that kind of stuff : p