Common decorators
Introduction
Scripts can retrieve instances from the scene by using some common decorators. Those decorators are used to retrieve objects from the scene and link them to properties in the script. This way, you can easily reference other objects in the scene and use them in your script.
@nodeFromScene
This decorator is used to retrieve any Mesh, TransformNode, Light or Camera from the scene by its name. The retrieved node is linked to the decorated property, so you can use it in your script.
typescript
@nodeFromDescendants
This decorator is used to retrieve any Mesh, TransformNode, Light or Camera from the children of the object the script is attached to. The retrieved node is linked to the decorated property, so you can use it in your script.
typescript
@animationGroupFromScene
This decorator is used to retrieve any Animation Group from the scene. The retrieved animation group is linked to the decorated property, so you can use it in your script.
typescript
@sceneAsset
This decorator is used to load and retrieve a scene container.
A scene can be used for multiple reasons. For example, a scene that is used once like a map, or a scene that is set to be instantiated multiple times like an enemies. In the first case, you can load the scene and retrieve the container with the decorator, while in the second case, you can load the scene as a container and instantiate it multiple times in the main scene.
The retrieved scene container instance is of type AdvancedAssetContainer, which is an extended version of the AssetContainer class provided by Babylon.js.
A scene container can be used to instantiate the assets multiple time. The goal of the AdvancedAssetContainer is to add support of extra features to the default AssetContainer, like the possibility instantiate attached scripts to instantiated entries.
Available methods are:
- removeDefault: When a scene is loaded as a container, it is automatically instantiated once and the instances are added to the main scene. This method allows to remove those default instances from the main scene as the container is used to be instantiated on-demand, for example for enemies.
- instantiate: Instantiates the whole container and returns the root nodes of the instantiated hierarchy. More information about instantiated entries in Babylon.js Documentation (Duplicating the models)
typescript
@componentFromScene
This decorator is used to retrieve the unique reference to a script attached to an object that has been instantiated in the scene. The retrieved script reference is linked to the decorated property, so you can use it in your script.
When using this decorator, make sure that only one instance of the script you want to retrieve is attached to objects in the scene. If multiple instances of the same script are found in the scene, an error will be thrown and the project won't be able to run, since it won't know which one to link to.
typescript