script-node-prefab.rst 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. .. include:: ../_header.rst
  2. Making a ScriptNode prefab
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. The power of the script nodes is that you can create prefabs of them. Prefabs are a central feature of the editor. You are used to create prefabs of your characters, game items, UI elements. Prefabs provide a flexible architecture (`nested prefabs <./prefab-nested.html>`_, `prefab variants <./prefab-variant.html>`_, `prefab properties <./prefab-user-properties.html>`_, `appendable prefabs <./prefab-instance-children.html>`_) for creating reusable objects. And now it is also available for you to create the behaviors (or part of the beaviors) of your game.
  5. You can create a script node prefab in the same way you create a prefab of a game object:
  6. 1. `Create a prefab file <./prefab-new-file.html>`_.
  7. 2. `Add a script node to the scene <./script-node-create.html>`_.
  8. If you add a script node prefab to a prefab scene, then it creates a prefab variant. It works the same as the game object prefabs. You can add custom properties.
  9. If you are using the built-in `ScriptNode class <./script-node-class.html>`_ (what we recommend you to do), then you can implement any of the methods provided by that class, like the **awake** or **execute** methods.
  10. The code generated for a script node prefab looks like this:
  11. .. code::
  12. // You can write more code here
  13. /* START OF COMPILED CODE */
  14. import OnEventScript from "./OnEventScript";
  15. import ScriptNode from "./ScriptNode";
  16. import Phaser from "phaser";
  17. /* START-USER-IMPORTS */
  18. /* END-USER-IMPORTS */
  19. export default class OnPointerDownScript extends OnEventScript {
  20. constructor(parent: ScriptNode | Phaser.GameObjects.GameObject | Phaser.Scene) {
  21. super(parent);
  22. // this (prefab fields)
  23. this.eventName = "pointerdown";
  24. /* START-USER-CTR-CODE */
  25. // Write your code here.
  26. /* END-USER-CTR-CODE */
  27. }
  28. /* START-USER-CODE */
  29. override awake(): void {
  30. this.gameObject.setInteractive();
  31. super.awake();
  32. }
  33. /* END-USER-CODE */
  34. }
  35. /* END OF COMPILED CODE */
  36. // You can write more code here
  37. The above code is TypeScript. It is the implementation of the **OnPointerDownScript**, which is a `prefab variant <./prefab-variant.html>`_ of the **OnEventScript** prefab. Both prefabs are part of the `Basic Scripts project <script-node-basic-scripts-project.html>`_.