Disabling / Enabling an Actor in Unreal Engine 4

Maybe you are like me and have been working with the Unity3D game engine. However, how could you resist from trying the new Unreal Engine 4? Especially, since you can get the whole engine including the source code for free.

Well, the time may come that you notice some minor differences between the two engines… Thus, soon the single most important question will come to your mind: “How do I disable and enable actors in UE4? Where is the checkbox and why is there no SetActive() method?” For example, when you are thinking about improving the performance for mobile projects, patterns like object pooling come to your mind. Or you simply want to disable an item when the player stores it in his inventory.

The short answer: You have to do it yourself. There is no SetActive() method in Unreal Engine 4 (yet). The “Hidden in Game” option only does what it should: It hides the objects. However, the components are still being updated, the collisions enabled and the actor mesh is blocking the path of your navmesh.

Enough talking, here is the code I use to deactivate an item actor:

void UInventoryComponent::AddItem(AActor* CollectibleItem)
{
	UInteractionComponent* collectibleComp = Cast<UInteractionComponent>(CollectibleItem->GetComponentByClass(UInteractionComponent::StaticClass()));

	if (collectibleComp != NULL)
	{
		//Disable Actor
		CollectibleItem->SetActorHiddenInGame(true);
		CollectibleItem->SetActorEnableCollision(false);
		CollectibleItem->SetActorTickEnabled(false);
		UStaticMeshComponent* meshComp = Cast<UStaticMeshComponent>(CollectibleItem->GetComponentByClass(UStaticMeshComponent::StaticClass()));
		if (meshComp != NULL)
		{
			UNavigationSystem::OnComponentUnregistered(meshComp);
		}

		//Add to inventory
		InventoryItems.Add(CollectibleItem);
		OnInventoryItemAdded.Broadcast(collectibleComp);
	}
}

So, basically you could copy the highlighted part into a SetActive() method and use your own actor class that is inherited by all your other actor classes. This could look like the following code:

/* Header file */
UCLASS(Blueprintable)
class AToggleActor : public ACharacter
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, Category = Actor)
	void SetActive(bool active);

	UFUNCTION(BlueprintCallable, Category = Actor)
	bool IsActive() const;
public:
	AToggleActor(const FObjectInitializer& ObjectInitializer);
private:
	bool bIsActive = true;
};
/* CPP file */
void AToggleActor::SetActive(bool active)
{
	//Don't set the same state twice
	if(bIsActive == active)
	{
		return;
	}

	//Our very own private boolean
	bIsActive = active;

	SetActorHiddenInGame(!active);
	SetActorEnableCollision(active);
	SetActorTickEnabled(active);
	UStaticMeshComponent* meshComp = Cast<UStaticMeshComponent>(GetComponentByClass(UStaticMeshComponent::StaticClass()));
	if (meshComp != NULL)
	{
		if (active)
		{
			UNavigationSystem::OnComponentRegistered(meshComp);
		}
		else
		{
			UNavigationSystem::OnComponentUnregistered(meshComp);
		}
	}
}

bool AToggleActor::IsActive() const
{
	return bIsActive;
}

Of course, this is just an example. Theoretically, you could also add this to the AActor class itself because Epic gives us the source code. Or maybe you have an even greater idea how to implement this :)

3 thoughts on “Disabling / Enabling an Actor in Unreal Engine 4”

  1. Even better is to use interface so that any class could implement it. Inheriting actor is great till you need the same e.g. in pawn.

  2. Also components has their Activate()/Deactivate(). This should be used instead of OnComponentRegistered()/OnComponentUnregistered()

Leave a Reply

Your email address will not be published. Required fields are marked *