DestroyObject (Unreal Engine)
- Declaration
static void DestroyObject(FCavrnusSpawnedObject SpawnedObject);
- Description
Destroys the given object.
You can only destroy an instance that was created via SpawnObject. If you want to get rid of something that was built-into the scene try synchronizing its “Visibility” boolean using a No-Code Component or a script.
- Blueprint Sample
Destroy Multiple Objects
- Code Sample
This example is a hypothetical HUD interface that allows users to edit the space. After selecting an object in the world (not implemented here) the Delete button allows them to remove the selected object.
SpaceEditHUD.h:
#pragma once
#include "CoreMinimal.h"
#include "Components/Button.h"
#include "SpaceEditHUD.generated.h"
class AActor;
UCLASS()
class MODULE_API USpaceEditHUD : public UUserWidget
{
GENERATED_BODY()
public:
void Initialize(FCavrnusSpaceConnection& InSpaceConnection);
void OnDeleteButtonClicked();
UPROPERTY(BlueprintReadWrite, meta = (bindWidget))
Button* DeleteButton = nullptr;
private:
FCavrnusSpaceConnection SpaceConnection;
AActor* SelectedActor;
};
SpaceEditHUD.cpp
#include "SpaceEditHUD.h"
#include "CavrnusFunctionLibrary.h"
#include "GameFramework/Actor.h"
void USpaceEditHUD::Initialize(FCavrnusSpaceConnection& InSpaceConnection)
{
SpaceConnection = InSpaceConnection;
SelectedActor = nullptr;
if (DeleteButton)
{
DeleteButton->OnClicked.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(USpaceEditHUD, OnDeleteButtonClicked));
}
}
void USpaceEditHUD::OnDeleteButtonClicked()
{
if (SelectedActor && SelectedActor.IsValidLowLevel())
{
FCavrnusSpawnedObject SpawnedObject = UCavrnusFunctionLibrary::GetIfIsSpawnedObject(SpaceConnection, SelectedActor);
UCavrnusFunctionLibrary::DestroyObject(SpawnedObject);
SelectedActor = nullptr;
}
}