SpawnObject (Unreal Engine)
- Declaration
static FPropertiesContainer SpawnObject(FCavrnusSpaceConnection SpaceConnection, const FString& UniqueIdentifier, CavrnusSpawnedObjectFunction spawnedObjectArrived);
Delegates Options:
DECLARE_DYNAMIC_DELEGATE_TwoParams(FCavrnusSpawnedObjectArrived, FCavrnusSpawnedObject, spawnedObject, AActor*, spawedActorInstance);
typedef TFunction<void(const FCavrnusSpawnedObject&, AActor*)> CavrnusSpawnedObjectFunction;
- Description
Instantiates the given object with no set properties.
Note: to make an prefab spawnable you will first need to:
Add an instance of
CavrnusSpatialConnector
to your Unreal levelCreate an entry in the
Spawnable Identifiers
map field.Assign a unique identifier to the map entry key. This string is what the journal will use to select which object type should be spawned.
Set the value for this map entry to the asset class that should be spawned
The matching unique identifier string is passed in as the parameter to SpawnObject
. It will then synchronously return the ContainerName
of the newly spawned instance, which can then be used to post and/or bind properties for the newly spawned instance. Without any posted properties its transform will default to being at the origin with no rotation and 1x scale.
Once the new object has been created in the journal, SpawnedObjectsManager
will spawn the actor instance and fix up any CavrnusPropertiesContainer
components that have been added to the asset class template with the correct container name, so the spawned object will reference the correct journal properties entries for its unique instance.
The C++ version of this function returns an FPropertiesContainer
which has a ContainerName
string inside of it. The Blueprints version simply returns a ContainerName
string.
- Blueprint Sample
- Code Sample
SpawnObjectButton.h:
#pragma once
#include "CoreMinimal.h"
#include "Components/Button.h"
#include "Types/CavrnusSpaceConnection.h"
#include "SpawnObjectButton.generated.h"
UCLASS()
class MODULE_API USpawnObjectButton : public UButton
{
GENERATED_BODY()
public:
void Initialize(FCavrnusSpaceConnection& InSpaceConnection);
void OnSpawnButtonClicked();
private:
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Object Spawning")
FString ObjectIdentifier;
FCavrnusSpaceConnection SpaceConnection;
};
SpawnObjectButton.cpp:
#include "SpawnObjectButton.h"
#include "CavrnusFunctionLibrary.h"
void USpawnObjectButton::Initialize(FCavrnusSpaceConnection& InSpaceConnection)
{
SpaceConnection = InSpaceConnection();
OnClicked.BindUFunction(this, GET_FUNCTION_NAME_CHECKED(USpawnObjectButton, OnSpawnButtonClicked));
}
void USpawnObjectButton::OnSpawnButtonClicked()
{
FString Container = UCavrnusFunctionLibrary::SpawnObject(SpaceConnection, ObjectIdentifier);
// Place the new object directly in front of the user's camera. If no camera is found, object will spawn at the origin.
APlayerCameraManager* CameraManager = UGameplayStatics::GetPlayerCameraManager(this, 0);
if (CameraManager)
{
FVector CameraLocation = CameraManager->GetCameraLocation();
FRotator CameraRotation = CameraManager->GetCameraRotation();
FVector StartPos = CameraLocation + (500.0f * CameraRotation.Vector);
FTransform StartTransform(CameraRotation, StartPos, FVector::OneVector);
UCavrnusFunctionLibrary::PostTransformPropertyUpdate(SpaceConnection, Container, "Transform", StartTransform);
}
}