custom_visualscript_nodes.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. .. _doc_custom_visualscript_nodes:
  2. Custom VisualScript nodes
  3. =========================
  4. Custom nodes are written in GDScript and can then be used in VisualScript.
  5. This is useful for offloading complex code to GDScript and reusing it.
  6. Creating a custom node
  7. ----------------------
  8. 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.
  9. There are some functions that can be implemented to set parameters of the custom node.
  10. Only add functions that are needed, a ``_has_input_sequence_port`` function is not necessary if it should return ``false`` for example.
  11. The most important part of a custom node is the ``_step`` function. The logic of the node is defined there.
  12. The ``inputs`` parameter holds the value of the input ports.
  13. 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.
  14. ``start_mode`` can be checked to see if it is the first time ``_step`` is called.
  15. ``working_mem`` is persistent each ``_step`` call. It can be used to store information.
  16. If you want to throw an error, for example if the input types are incorrect, you can return the error message as a string.
  17. 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.
  18. Example:
  19. ::
  20. tool
  21. extends VisualScriptCustomNode
  22. # the name of the custom node as it appears in the search
  23. func _get_caption():
  24. return "Get Input Direction 2D"
  25. func _get_category():
  26. return "Input"
  27. # the text displayed after the input port / sequence arrow
  28. func _get_text():
  29. return ""
  30. func _get_input_value_port_count():
  31. return 0
  32. # the types of the inputs per index starting from 0
  33. func _get_input_value_port_type(idx):
  34. return TYPE_OBJECT
  35. func _get_output_value_port_count():
  36. return 1
  37. # the types of outputs per index starting from 0
  38. func _get_output_value_port_type(idx):
  39. return TYPE_VECTOR2
  40. # the text displayed before each output node per index
  41. func _get_output_value_port_name(idx):
  42. return "Direction"
  43. func _has_input_sequence_port():
  44. return true
  45. # the number of output sequence ports to use
  46. # (has to be at least one if you have an input sequence port)
  47. func _get_output_sequence_port_count():
  48. return 1
  49. func _step(inputs, outputs, start_mode, working_mem):
  50. # start_mode can be checked to see if it is the first time _step is called
  51. # this is useful if you only want to do an operation once
  52. # working_memory is persistent between _step calls
  53. # the inputs array contains the value of the input ports
  54. var x = int(Input.is_action_pressed("ui_right")) - int(Input.is_action_pressed("ui_left"))
  55. var y = int(Input.is_action_pressed("ui_down")) - int(Input.is_action_pressed("ui_up"))
  56. # the outputs array is used to set the data of the output ports
  57. outputs[0] = Vector2(x,y)
  58. # return the error string if an error occurred, else the id of the next sequence port
  59. return 0
  60. Using a custom node
  61. -------------------
  62. To use the script, add a ``CustomNode``, select it and drag your custom node script into the ``script`` property shown in the inspector.
  63. .. image:: img/visual_script_custom_node_set_script.png
  64. Result:
  65. .. image:: img/visual_script_custom_node_result.png