script-node-class.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. .. include:: ../_header.rst
  2. The ScriptNode class
  3. ~~~~~~~~~~~~~~~~~~~~
  4. When you create an instance of a |ScriptNode|_, the |SceneCompiler|_ generates a code like this:
  5. .. code::
  6. new ScriptNode(parent);
  7. It means it creates an instance of the ``ScriptNode`` class. But this class is not part of the Phaser_ API, it is a class you should code yourself.
  8. The protocol of this class is simple, it needs a constructor that receives a parent argument. Something like this:
  9. .. code::
  10. class ScriptNode {
  11. constructor(parent) {
  12. }
  13. }
  14. It is simple, but if you need, you can create a prefab with more features, like handling children, events, etc...
  15. The good news is that |PhaserEditor|_ can generate the default implementation of this class for you, with basic features:
  16. 1. In the |FilesView|_, select the folder when you want to add the class file.
  17. 2. Open the |CommandPalette|_ (``Ctrl+K``) and search for ``script``.
  18. 3. Select the command with the desired format (TypeScript, JavaScript, ES modules,...).
  19. 4. **Execute** the command and it generates the class file in the selected folder.
  20. .. image:: ../images/script-node-create-script-node-class-1-20230323.webp
  21. :alt: Create ScriptNode class commands.
  22. This built-in class the editor provides contains a couple of features:
  23. * Keeps a reference to the scene, the game object, and the parent.
  24. * Manages an array of the children nodes.
  25. * Registers listeners to the scene and game object for implementing the **awake**, **start**, **update**, and **destroy** events. It follows the same logic as the `User Components events <./user-components-super-class.html>`_.
  26. * Provides an interface for "action nodes".
  27. The parent
  28. ``````````
  29. When a new instance of the ``ScriptNode`` class is created, it receives a parent node as an argument. This parent could be a scene, a game object, or another script node.
  30. The script node instance keeps a reference to the parent, but also keeps a reference to the game object and the scene. Sure, if the node is added to a scene, the game object reference is not updated, it keeps ``undefined``.
  31. Related to the parent, the class provides the following properties:
  32. - ``parent()``: it's type is ``Phaser.Scene | Phaser.GameObjects.GameObject | ScriptNode``.
  33. - ``gameObject()``: it's type is ``Phaser.GameObjects.GameObject | undefined``.
  34. - ``scene()``: it's type is ``Phaser.Scene``.
  35. The children
  36. ````````````
  37. The ``ScriptNode`` class has an array of nodes for keeping the children. This array is updated when a new node is created. It also provides some related methods:
  38. * ``children ()``: A property for iterating the children.
  39. * ``add()``: A method for adding new children. This method is called automatically when a new child is created.
  40. The events
  41. ``````````
  42. The ``ScriptNode`` class provides a couple of methods for handling special events that may help you on implementing the behaviors. It works just like the `User Components events`_. The methods are:
  43. - ``awake()``: It is called when all the objects of the scene are created. The values of the user properties (prefab) will be available at this time, so you can override this method for making computations that require the value of the properties. It works like the `UserComponent "awake" method <user-components-awake-event.html>`_.
  44. - ``start()``: It is called the first time the scene updates.
  45. - ``update()``: It is called each time the scene updates.
  46. - ``destroy()``: It is called when the game object is destroyed or the scene is shut down.
  47. The action methods
  48. ``````````````````
  49. We find it convenient to establish a protocol for action script nodes. An action script is a script node that will execute a certain task. For that purpose, the class provides the following methods:
  50. - ``execute()``: Contains the code of the action.
  51. - ``executeChildren()``: Executes the children.
  52. In the next chapter, we mention a project with basic node implementations that you can include in your game. These scripts provide a protocol or style you can adopt for your game.