This lesson covers how to manipulate object hierarchies in Nada.
Learning Objectives
- Parenting / unparenting objects at runtime
- Accessing objects through the hierarchy in code
Level Setup
- Command : New Project
- Create a new project using the {nav icon=clone, name=Empty 2D Project} template
- Command : CreateSprite
- Command : Add Resource
- Create a NadaScript resource using the Component template template and name it
SquareLogic
- Select : Sprite object
- In the
Properties Window
- Rename Sprite object to
Square
- Add Component :
SquareLogic
- Update the
SquareLogic
script to the following:
class SquareLogic : NadaComponent
{
[Dependency] var Transform : Transform;
[Property]
var RotateSpeed : Real = 5.0;
function Initialize(init : CogInitializer)
{
Zilch.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate);
}
function OnLogicUpdate(event : UpdateEvent)
{
var vel = this.RotateSpeed * event.Dt;
this.Transform.RotateAnglesWorld(Real3(0.0, 0.0, vel));
}
}
The Square object rotates
Parenting at Runtime
Let's create an archetype that can be used later to spawn child cogs at runtime.
- Command : CreateSprite
- In the
Properties Window
- Rename Sprite object to
Circle
- Under Sprite
- Set SpriteSource enum to
Circle
- Set Archetype to
CircleArchetype
- Command : Delete
Now let's modify the SquareLogic
component to spawn circles and parent them to the Square object
- Update the
SquareLogic
script to the following:
class SquareLogic : NadaComponent
{
[Dependency] var Transform : Transform;
[Property]
var RotateSpeed : Real = 5.0;
[Property]
var ArchetypeToSpawn : Archetype = Archetype.CircleArchetype;
[Property]
var SpawnLocation : Real3 = Real3(0.0, 2.0, 0.0);
function Initialize(init : CogInitializer)
{
Zilch.Connect(this.Space, Events.LogicUpdate, this.OnLogicUpdate);
}
function OnLogicUpdate(event : UpdateEvent)
{
var vel = this.RotateSpeed * event.Dt;
this.Transform.RotateAnglesWorld(Real3(0.0, 0.0, vel));
if(Zilch.Keyboard.KeyIsPressed(Keys.Space))
{
var obj = this.Space.CreateAtPosition(this.ArchetypeToSpawn, this.SpawnLocation);
obj.AttachTo(this.Owner);
}
}
}
The Space
bar spawns child Circle object objects
Notice that the Circle object is attached to the Square object object as soon as it is created. That is done through invoking the AttachTo function on Cog, which takes a designated parent cog as its only parameter.
Similarly, you can call the Detach function to unparent any object from its hierarchy:
- Add the following to the end of
OnLogicUpdate
function in theSquareLogic
component:
if(Zilch.Keyboard.KeyIsPressed(Keys.D))
{
this.Owner.Children.Current.Detach();
}
The D
key detaches the child Circle object objects
NOTE: Zilch also sends the Attached and Detached events to cogs when the respective operation is performed on them. Additionally, you can connect to the ChildAttached and ChildDetached events on a parent objecct if you want to perform some logic upon acquiring or losing a child.
Traversing Hierarchies at Runtime
Within a script, you can easily access a cog's parent:
- Add the following to the
SquareLogic
class:
[Property]
var Growth : Real = 1.1;
- In the
SquareLogic
class - In the
OnLogicUpdate
function - Below the line,
obj.AttachTo(this.Owner);
- Add the following code:
obj.Parent.Transform.Scale *= this.Growth;
The Space
bar now also grows the parent Square object object
You can also get a range containing all immediate children. Let's implement an addition to our previous script.
- Add the following to
OnLogicUpdate
function in theSquareLogic
component:
if(Zilch.Keyboard.KeyIsPressed(Keys.Enter))
{
foreach(var child in this.Owner.Children)
{
child.Sprite.VertexColor = Colors.Red;
}
}
The Enter
key now turns the child Circle object objects red
Related Materials
Tutorials
Manual
Reference
Classes
Events
Commands
Development Task
- T1189