Is it possible to toggle the visibility of a specific actor in a world?

Hi Guys, I am using Carla to create a small dataset and need to do some work on UE4. Now I have realized the “toggle visibility” of actors with class by pressing the key “1” with the blueprint class. Now I want to realize it in C++. But I don’t know if there is an interface to the level so that I can do something with it, or if I can easily create an empty c++ class. Could someone let me know, please?:slight_smile:

My implementation for converting that BP graph to C++ would probably look something like this:

  • Create a C++ class that inherits APlayerController if you don’t already have one
  • In that class implement a function for toggling the visibility:
void AYourPlayerControllerClassName::ToggleVisibility()
{
    TArray<AActor*> OutActors;    // array where our actor will end up
    UGameplayStatics::GetAllActorsOfClass(GetWorld(), ATheClassYouWantToGetAllActorsOf::StaticClass(), OutActors);    // this is actually the same function you used in BP, just the C++ syntax
    for (auto Current : OutActors)    // a basic for each loop (AKA range based for loop) in C++
        Current->GetRootComponent()->ToggleVisibility(false);    // toggling the visibility for each actor's root component as you did in the BP
}
  • Override APlayerController::SetupInputComponent
  • In that function bind the input to your ToggleVisibility function:
void AYourPlayerControllerClassName::SetupInputComponent()
{
    Super::SetupInputComponent();

    FInputKeyBinding ToggleVisibilityBinding(EKeys::One, IE_Pressed);    // creating an input key binding so that we can bind the ToggleVisibility function to an input key
    ToggleVisibilityBinding.KeyDelegate.BindDelegate(this, &AYourPlayerControllerClassName::ToggleVisibility);    // binding the ToggleVisibility function to our input key binding object
    InputComponent->KeyBindings.Add(ToggleVisibilityBinding);    // actually adding the input key binding to the input components array of key bindings so that the input component knows to call our bound function when the key '1' is pressed
}

Hopefully that helps you understand implementing stuff in C++ a bit better :slight_smile:

2 Likes

Thank you very much. I’ll try it soon.

Oh, and also remember to use that C++ player controller as your GameMode’s PlayerControllerClass.

Also since your actor class you want to find all references of seems to be a BP class, you might need to check this out UE4 - Blueprints to C++ - Class References and TSubclassOf - YouTube

thank you again.
And I have attempted to finish this part according to your code. But it failed because there are always Errors with LNK2019 and E1574(“TAtomic is only usable with trivial types”) while compiling. I have attempted to solve this problem by myself, but so far don’t find any solutions. Could you help me again, please? I am using UE4.24 btw.

#include “MyPlayerController.h”
#include “Kismet/GameplayStatics.h”
#include “Engine/StaticMeshActor.h”
#include “Engine/World.h”

void AMyPlayerController::ToggleVisibility()
{
TArray<AActor*> OutActors;
UGameplayStatics::GetAllActorsOfClass(GetWorld(), AStaticMeshActor::StaticClass(), OutActors);
for (auto Current : OutActors)
Current->GetRootComponent()->ToggleVisibility(false);
}

void AMyPlayerController::SetupInputComponent()
{
Super::SetupInputComponent();

FInputKeyBinding ToggleVisibilityBinding(EKeys::One, IE_Pressed);   
ToggleVisibilityBinding.KeyDelegate.BindDelegate(this, &AMyPlayerController::ToggleVisibility);
InputComponent->KeyBindings.Add(ToggleVisibilityBinding); 

}

#pragma once

#include “CoreMinimal.h”
#include “GameFramework/PlayerController.h”
#include “MyPlayerController.generated.h”

/**
*
*/
UCLASS()
class MYPROJECT3_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()

protected:

UFUNCTION()
void ToggleVisibility();

UFUNCTION()
void SetupInputComponent();

};

If you have Visual Studio up with your project’s source (assuming you’re building engine from source), double clicking a C++ sourced blueprint node should bring you to the source code for that node. If not, then a global search for ToggleVisibility should help you find the implementation, so you can see how to use it.