custom_visualscript_nodes.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. .. _doc_custom_visualscript_nodes:
  2. Custom VisualScript nodes
  3. =========================
  4. .. warning::
  5. `Godot 4.0 will remove VisualScript from core entirely. <https://godotengine.org/article/godot-4-will-discontinue-visual-scripting>`__
  6. As a result, creating new projects using visual scripting in Godot is not recommended.
  7. Future Godot 4.x releases may have VisualScript reimplemented as an extension.
  8. While Godot 3.x will keep VisualScript supported, we recommend
  9. :ref:`trying out GDScript <toc-learn-scripting-gdscript>` instead,
  10. especially if you intend to migrate your project to Godot 4.
  11. Custom nodes are written in GDScript and can then be used in VisualScript.
  12. This is useful for offloading complex code to GDScript and reusing it.
  13. Creating a custom node
  14. ----------------------
  15. Create a new script that extends :ref:`class_VisualScriptCustomNode` and put a ``tool`` keyword at the top. This is needed for the script to run in the editor.
  16. There are some functions that can be implemented to set parameters of the custom node.
  17. Only add functions that are needed, a ``_has_input_sequence_port`` function is not necessary if it should return ``false`` for example.
  18. The most important part of a custom node is the ``_step`` function. The logic of the node is defined there.
  19. The ``inputs`` parameter holds the value of the input ports.
  20. The ``outputs`` parameter is an array where the indices represent the output port ids. It can be modified to set the values of the output ports.
  21. ``start_mode`` can be checked to see if it is the first time ``_step`` is called.
  22. ``working_mem`` is persistent each ``_step`` call. It can be used to store information.
  23. If you want to throw an error, for example if the input types are incorrect, you can return the error message as a string.
  24. When everything goes right, return the id of the sequence port which should be called next. If your custom node doesn't have any, just return 0.
  25. Example:
  26. ::
  27. tool
  28. extends VisualScriptCustomNode
  29. # The name of the custom node as it appears in the search.
  30. func _get_caption():
  31. return "Get Input Direction 2D"
  32. func _get_category():
  33. return "Input"
  34. # The text displayed after the input port / sequence arrow.
  35. func _get_text():
  36. return ""
  37. func _get_input_value_port_count():
  38. return 0
  39. # The types of the inputs per index starting from 0.
  40. func _get_input_value_port_type(idx):
  41. return TYPE_OBJECT
  42. func _get_output_value_port_count():
  43. return 1
  44. # The types of outputs per index starting from 0.
  45. func _get_output_value_port_type(idx):
  46. return TYPE_VECTOR2
  47. # The text displayed before each output node per index.
  48. func _get_output_value_port_name(idx):
  49. return "Direction"
  50. func _has_input_sequence_port():
  51. return true
  52. # The number of output sequence ports to use
  53. # (has to be at least one if you have an input sequence port).
  54. func _get_output_sequence_port_count():
  55. return 1
  56. func _step(inputs, outputs, start_mode, working_mem):
  57. # start_mode can be checked to see if it is the first time _step is called.
  58. # This is useful if you only want to do an operation once.
  59. # working_memory is persistent between _step calls.
  60. # The inputs array contains the value of the input ports.
  61. var x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
  62. var y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
  63. # The outputs array is used to set the data of the output ports.
  64. outputs[0] = Vector2(x, y)
  65. # Return the error string if an error occurred, else the id of the next sequence port.
  66. return 0
  67. Using a custom node
  68. -------------------
  69. To use the script, add a ``CustomNode``, select it and drag your custom node script into the ``script`` property shown in the inspector.
  70. .. image:: img/visual_script_custom_node_set_script.png
  71. Result:
  72. .. image:: img/visual_script_custom_node_result.png