script-node-create.rst 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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>`_: select the **Add Script** option in the **Scripting** context menu or press the **Add Script** command (``U``).
  9. .. image:: ../images/script-node-add-script-dialog-20230627.webp
  10. :alt: Add Script command.
  11. It opens the **Add Script** dialog. There you can select the script you want to add:
  12. .. image:: ../images/script-node-add-script-dialog.webp
  13. :alt: The Add Script dialog.
  14. Browsing the Script Nodes
  15. `````````````````````````
  16. The |ScriptNodes|_ are displayed in the |OutlineView|_, below the parent object, or the scene:
  17. .. image:: ../images/script-node-outline-20230322.webp
  18. :alt: Script nodes in the outline view.
  19. Also, you can browse the scripts of an object by pressing the command ``Shift+U``. The command is also available in the **Scripting** section of the context menu. That command opens the **Browse Scripts** dialog:
  20. .. image:: ../images/script-node-browse-scripts-20230322.webp
  21. :alt: Browsing the script nodes.
  22. Code generation of the creation of a script
  23. ```````````````````````````````````````````
  24. When you add a |ScriptNode|_ to an object, it generates a code like this:
  25. .. code::
  26. editorCreate() {
  27. // btn
  28. const btn = this.add.image(359, 223, "ui", "btn.png");
  29. // onPointerDownScript
  30. new OnPointerDownScript(btn);
  31. ...
  32. }
  33. 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`_:
  34. .. code::
  35. editorCreate() {
  36. // btn
  37. const btn = this.add.image(359, 223, "ui", "btn.png");
  38. // scriptnode_1
  39. new ScriptNode(btn);
  40. ...
  41. }
  42. The **ScriptNode** class is not part of Phaser_, but it could be generated by Phaser Editor 2D. The next section is about it.