script-node-class.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 to, you can create a prefab with more features, like handling children, events, etc...
  15. The good news is that this class is included in the |PhaserEditor|_ project templates. If your project is not based on a template, you can install it from the ``@phasereditor2d/scripts-core`` script library. `Learn more about the script libraries <script-node-libraries.html>`_.
  16. Also, |PhaserEditor|_ can generate the default implementation of this class for you, with the basic features:
  17. 1. In the |FilesView|_, select the folder when you want to add the class file.
  18. 2. Open the |CommandPalette|_ (``Ctrl+K``) and search for ``script``.
  19. 3. Select the command with the desired format (TypeScript, JavaScript, ES modules,...).
  20. 4. **Execute** the command and it generates the class file in the selected folder.
  21. .. image:: ../images/script-node-create-script-node-class-1-20230323.webp
  22. :alt: Create ScriptNode class commands.
  23. This built-in class the editor provides contains a couple of features:
  24. * Keeps a reference to the scene, the game object, and the parent.
  25. * Manages an array of the children nodes.
  26. * 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>`_.
  27. * Provides an interface for "action nodes".
  28. The parent
  29. ``````````
  30. 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.
  31. 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``.
  32. Related to the parent, the class provides the following properties:
  33. - ``parent()``: it's type is ``Phaser.Scene | Phaser.GameObjects.GameObject | ScriptNode``.
  34. - ``gameObject()``: it's type is ``Phaser.GameObjects.GameObject | undefined``.
  35. - ``scene()``: it's type is ``Phaser.Scene``.
  36. The children
  37. ````````````
  38. 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:
  39. * ``children ()``: A property for iterating the children.
  40. * ``add()``: A method for adding new children. This method is called automatically when a new child is created.
  41. The events
  42. ``````````
  43. The ``ScriptNode`` class provides a couple of methods for handling special events that may help you implement the behaviors. It works just like the `User Components events`_. The methods are:
  44. - ``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>`_.
  45. - ``start()``: It is called the first time the scene updates.
  46. - ``update()``: It is called each time the scene updates.
  47. - ``destroy()``: It is called when the game object is destroyed or the scene is shut down.
  48. The action methods
  49. ``````````````````
  50. 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:
  51. - ``execute()``: Contains the code of the action.
  52. - ``executeChildren()``: Executes the children.
  53. 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.
  54. The action's target
  55. ```````````````````
  56. Most of the action nodes modify or read the game object of the node. This is fine for a lot of cases, but a bit limited. Let's see an example.
  57. You want to make a collider between a player object and a list of coin objects and destroy the coin when the player touches it:
  58. .. image:: ../images/scene-editor-script-node-target-action-game-20240109.webp
  59. :alt: The scene for collecting coins
  60. We have a **Make Collider Action** node that you can add to the player. This node has a parameter to select the other object you want to collide with. In this case, it is a container with the coins:
  61. .. image:: ../images/scene-editor-script-node-demo-collider-node-20240109.webp
  62. :alt: Make collider node.
  63. .. image:: ../images/scene-editor-script-node-demo-collider-node-props-20240109.webp
  64. :alt: Make collider node properties
  65. When the collision happens, the collider node executes the children and passes the two objects as arguments. In the first argument goes the player, and in the second argument goes the coin.
  66. So, we add **Destroy Object Action** to the collider node. This node is an action to destroy an object. By default, it destroys the game object associated with it: the player. But we want to destroy the coin, not the player. So we need to change the target of the destroy action from the game object to the second argument.
  67. To do this, we add the **Action Target Config** user component to the destroy object action, and set the **Target** to **ARG_2**. The destroy action then will get the second argument and destroy it.
  68. .. image:: ../images/scene-editor-script-node-collider-demo-destroy-action-20240109.webp
  69. :alt: The destroy action node
  70. .. image:: ../images/scene-editor-script-node-collider-demo-destroy-action-properties-20240109.webp
  71. :alt: The properties of the destroy action
  72. The **Target** parameter shows an array of options, **GAME_OBJECT** (default), **ARG_1**, ..., **ARG_8**.
  73. In addition to the **Target**, you can set the **Target Name** of the action. This name is only about syntax sugar and makes the node more readable in the Outline view. Look in the previous screenshot, the node starts with the value of **Target** plus the **Target Name**.
  74. Custom action script node
  75. `````````````````````````
  76. Making a new action node is very simple. Just create a prefab of a **Script Node**, and implement the ``execute()`` method. If you want to support the **Action Target Config** component, you can use the ``ScriptNode.getActionTargetObject()`` utility method:
  77. .. code::
  78. execute(...args: any[]) {
  79. const obj = this.getActionTargetObject(args);
  80. obj.doSomething();
  81. }
  82. That utility method does the following:
  83. - Check if the **Action Target Config** user component is present in the node. If it is present and has a target configured to return an argument, then returns the right argument.
  84. - Else, it returns the node's game object.