Normally in Unreal, if you wish to spawn a an Actor at runtime, you would simply call “Spawn Actor From Class” in Blueprint, or UWorld::SpawnActor from C++, to make a new instance of it.
There are a couple of problems you run into when doing this in Cavrnus howeverThe CavrnusFunctionLibrary provides an API call that solves two of the main challenges doing this.
The object spawned isn’t must be synchronized for everyone, so only your local client will see the new object.Even if it was synchronized, every instance would .
Every instance should share the same property paths as the base prefab, so they couldn’t can behave individually.
Both of these problems are solved using the CavrnusFunctionLibrary API call SpawnObject.
...
This unique string key is what you will use to actually spawn the object. (Note that this means you can modify or change the prefab and, as long as the key name doesn’t change, old journal operations will now spawn the new prefab.)
...
Spawning your Prefab
Now, let’s write up a simple UI button script to spawn the object:
...
create an object in the local map that will be able to create one or more Cavrnus objects:
As you can see, we’re simply passing in that UniqueId unique string ID from above. The value we get back, meanwhile, is the Container Name of the new Instance. So, when the new prefab is spawned, the Cavrnus Properties Container at it’s its root will use this Container Name.
...
Essentially what happens is the initial root Cavrnus Properties Container’s Container Name is replaced with the new spawned instance ID. Then, every sub-object that starts with the root Container Name swaps out that part of the string for new new Id.
To give an example, let’s look at the above prefab.
The Root has a Unique no Container Name defined:
...
While the sub-parts have Container Names starting with that same root name:
...
Now let’s look at the runtime instance that we got from the SpawnObject call. As you can see, it has a unique name do distinguish it from other instances
Notice that the Unique Contain Container Name on the root has been completely replaced with the new ID.
Note: There is a Cavrnus Spawned Object Flag component also attached here. This will be needed later when we want to delete the object.
...
Meanwhile the Left Arm has replaced the first part of its Container Name only:
...
The new root Container Name is returned synchronously from the SpawnObject call, and can immediately be used to post properties. Alternatively, any ValueSync scripts components on the object will function normally too.
Code Block |
---|
public void SpawnPrefab() { // SpaceConnection here would be a class member of type FCavrnusSpaceConnection, that would be set on Space joined if (spaceConnSpaceConnection == null) return; // SpawnedObjectArrived here would be a delegate class member string instanceContainerName = spaceConn.SpawnObject("Cuby McCubeface"of type FCavrnusSpawnedObjectArrived, that would be bound to a callback during setup FString InstanceContainerName = UCavrnusFunctionLibrary::SpawnObject(SpaceConnection, "PerfTestSphere", SpawnedObjectArrived); spaceConn.UCavrnusFunctionLibrary::PostBoolPropertyUpdate(instanceContainerNameSpaceConnection, InstanceContainerName, "Visibility", true); } |
Deleting Spawned Objects
When deleting a spawned object, you will need to access the Cavrnus Spawned Object Flag attached to ita FCavrnusSpawnedObject
is provided. This can be done with a simple GetComponent callsaved off when FCavrnusSpawnedObjectArrived
is called, or it can be retrieved from the SpawnedObjectsManager
by calling UCavrnusFunctionLibrary::GetIfIsSpawnedObject
. Once you have it, simply call DestroyObject on it.
...
The above system is a good way to spawn well-known prefabs. However, if you want to spawn more advanced or imported objects, you can read this guide here (TODO: LINK).