script-node-create.rst 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. .. include:: ../_header.rst
  2. Creating a Script Node
  3. ~~~~~~~~~~~~~~~~~~~~~~
  4. A |ScriptNode|_ object shares a lot with the game objects, you can add it to the scene by dragging it from the |BlocksView|_ and dropping it on the scene:
  5. .. image:: ../images/script-node-create-1-20230322.webp
  6. :alt: Create script node from the blocks view.
  7. That action creates an instance of the `ScriptNode class <./script-node-class.html>`_ and adds it to the object selected in the scene. If no object is selected, then it adds the script node to the scene.
  8. In addition to an instance of the `ScriptNode class`_, you can add an instance of a `ScriptNode prefab <./script-node-prefab.html>`_. The script node prefabs are shown next to the other prefabs in the |BlocksView|_:
  9. .. image:: ../images/script-node-prefab-in-blocks-view.webp
  10. :alt: Script Node prefabs in the Blocks view.
  11. another way of adding a script node is by selecting the **Add Script** option in the **Script** context menu. It is also available as a command (``U``).
  12. .. image:: ../images/script-node-add-script-command.webp
  13. :alt: Add Script command.
  14. It opens the **Add Script** dialog. There you can select the script you want to add:
  15. .. image:: ../images/script-node-add-script-dialog.webp
  16. :alt: The Add Script dialog.
  17. Browsing the Script Nodes
  18. `````````````````````````
  19. The |ScriptNodes|_ are displayed in the |OutlineView|_, below the parent object, or the scene:
  20. .. image:: ../images/script-node-outline-20230322.webp
  21. :alt: Script nodes in the outline view.
  22. Also, you can browse the scripts of an object by pressing the command ``Shift+U``. The command is also available in the **Script** section of the context menu. That command opens the **Browse Scripts** dialog:
  23. .. image:: ../images/script-node-browse-scripts-20230322.webp
  24. :alt: Browsing the script nodes.
  25. Code generation of the creation of a script
  26. ```````````````````````````````````````````
  27. When you add a |ScriptNode|_ to an object, it generates a code like this:
  28. .. code::
  29. editorCreate() {
  30. // btn
  31. const btn = this.add.image(359, 223, "ui", "btn.png");
  32. // onPointerDownScript
  33. new OnPointerDownScript(btn);
  34. ...
  35. }
  36. It is the code generated by the |SceneCompiler|_ when you add a script node prefab. If you add an instance of the built-in ScriptNode, the code is similar, but using the `ScriptNode class`_:
  37. .. code::
  38. editorCreate() {
  39. // btn
  40. const btn = this.add.image(359, 223, "ui", "btn.png");
  41. // scriptnode_1
  42. new ScriptNode(btn);
  43. ...
  44. }
  45. The **ScriptNode** class is not part of |Phaser|_, but it could be generated by Phaser Editor 2D. The next section is about it.