Hi There,
I’ve merged your two topics together as they both tie-in quite nicely under the general heading of symbols/subsymbols.
Before I proceed any further it’s worth mentioning that a lot of what I’ll be mentioning is covered in more detail in the subsymbols documentation so it’s worth checking out.
Symbol construction
You can construct a symbol in ZapWorks Studio in a script by simply dragging the symbol from the symbol definitions panel into the editor with a script node selected. For a reusable object like a bullet you’ll probably want to use a blank symbol.
You can then add this symbol instance to the Hierarchy at runtime using the Z.Group push function, using the remove function to remove it. The root node acts as a group in case you don’t have a specific group in the hierarchy to push your symbol to.
It’s worth noting that once constructed there’s no way at the moment for “deleting” the memory used, even when removing the symbol from the Hierarchy - so we recommend creating a “pool” of objects that you can reuse, like your array method.
Subsymbol access
To answer your questions on accessing functionality of a subsymbol from a parent symbol the first step is to have access to a variable containing the symbol instance. This was mentioned previously with dragging in the symbol to a script node and can also be done with dragging in a symbol instance in the Hierarchy to a script node. once you have that variable the syntax is as follows:
To activate a state or timeline
mySymbol.controllers.symbolController.elements.controllerState.activate();
To access functions and variables from within a script
The below assumes that the function or variable has the export keyword preceding it, for more information on this please see this post - How to change plane 'materials' property (texture) from my script
mySymbol.nodes.symbolScript.myVariable
mySymbol.nodes
is the way of accessing the nodes within a symbol, which is also useful.
To access events
At the moment we don’t expose any events from a symbol but you can fire your own custom events from within a subsymbol using the emit function and listen for them within the parent symbol.
For example you could have the following code within a subsymbol, parent being a plane object the pointerdown script was created from:
parent.on("pointerdown", (e) => {
parent.emit("custom:pointerdown");
});
And then listen for the custom:pointerdown
event in the parent symbol to know when subsymbol’s plane has been pressed:
mySymbol.nodes.Plane0.on("custom:pointerdown", () => {
// run some code
})
It is highly recommended to prepend custom events with the custom: syntax to avoid potential conflicts with built-in events, including ones that may be added in future.
I hope this has helped explain symbols a little better and how they can be used, any further questions let us know
Thanks,
Mark