Entity Structure Propagation (Prefab‐Like Behavior) - bryanedds/Nu GitHub Wiki
Similar to Unity's Prefabs - https://docs.unity3d.com/Manual/Prefabs.html - and Godot's Scenes, Nu offers a form of design-time structural entity replication and synchronization. Let's give an example of creating a few block-based entities -
First, we'll create a ground using a Block3dDispatcher with a scale of [10 0.1 10] -
Now create another Block3dDispatcher (do NOT copy paste yet!), stretch it on the Y and place it on top of the ground for a building-like structure -
Now, moving the camera up and orienting it downward, use Ctrl+Drag on the building-like entity to create more of them -
On the original building (Block3d-0002) in the Entity Hierarchy, you will notice additional UI appearing -
This is the magic of Entity Structure Propagation in Nu. Let's try it out!
With the original building selected, click the AlbedoOpt check box in the Material Properties and then adjust its Red color property to 64 like so -
Now we will synchronize this entity's changes to the entities that were copied from it by clicking the Push
button in the Entity Hierarchy next to it -
Notice how copied from entity's colors are synchronized! This happens because each time you copy and paste an entity, the new entity's PropagationSourceOpt property is set to the address of entity it was copied from -
(sorry that the Propaga
property name is cut off...)
If you want to sever this connection, you can click the check box on the PropagationSourceOpt (don't do this yet, tho). Alternatively, you can change that source to any other entity. If you do, the Push
button will appear on the entity that becomes the source.
Now, let's change the color of a building other than the propagation source -
Next, let's change the color of the original building and then hit its Push
button -
You'll notice that when you Push again, it only affects the color of the entities whose color weren't manually changed by the user. When you change a property of a entity that is a propagation target, that property's value is overridden. So when the same property of the propagation source is changed, it won't propagate when pushed. The non-overridden properties will continue to be propagated tho!
Now let's make some structural changes to the original building by copy dragging on the original building above it -
Next, click the check box on the PropagationSourceOpt property to unlink it from the entity you just sourced it from -
(we don't want this entity to be the target of the original building's propagated changes)
Next, change the scale of the new block and move around where you like it relative to the original building. And then drag the new entity in the Entity Hierarchy on top of the original building entity to make it a child of it like so -
Now when we click the Push
button again, and you'll see that child block be propagated to the other targets -
As you can see, it's not just properties that propagate, but also child entities and their properties. This is why this is called 'structural propagation'.
If you want to save the propagation source entity to a file to use it elsewhere, say in other projects, you can select it and use the Save Entity feature -
Then load it in other projects via the Open Entity feature.
As you can imagine, you can mix and match this feature use across entity hierarchies, recursively building up propagation sources with significant complexity. There are trade-offs to this way of doing structural propagation as opposed to, say, Unity Prefabs. Unlike Unity Prefabs, structure propagation is driven manually by clicking the Push
button. That is, changing the propagation source will not automatically result in changes to its propagation targets.
While the manual interaction required might be a bit of a downside, if you've used the Prefab workflow in Unity before, you'll see how this system is as expressive yet much easier to use and manage in the common case. Because propagation links are automatically created via the existing copy-paste workflow, there's very little manual intervention required to leverage this system. You don't have to manage a separate asset resource for each propagation source. Additionally, this approach yielded a significantly simpler implementation, running around 300 lines of code or so.
This set of techniques can be a good way to 'block-out' various scenes, adding detail as you go along -
Because structural propagation works recursively, you can reparent the buildings under the ground block, then duplicate the block like so -
And if you change a building in the original area, you can click Push
on the ground block to propagate those changes like so -