Plugin: How to add a custom node that user can add a script to?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By fede0d
:warning: Old Version Published before Godot 3 was released.

I want to transform a current implementation of state machine and use the new plugins API.

It works like this:

  • StateControllerNode
  • StateNode 1
  • StateNode 2
  • StateNode 3

In the StateControllerNode you can define the target node you want to control, so you can get access to it by using just a variable from the state nodes.
In the StateNode you can create a custom script wich will extend from StateNode. Inside you get access to callback functions like enter_state(), exit_state(), process_state(delta), etc.

I checked the current plugin demos, but the closest thing to this is the HeartNode demo, but in that plugin the script that is attached to the node is always present, and is a direct reference to the script .gd file, so if the user changes the content of the node’s script, all instances of the node will also be changed.

I want to have a “virtual” ScriptNode class with the base implementation that users can extend and override.

I know this is possible by writing a custom C++ module, but I want to do it using the plugin system.

I don’t think that kind of architecture is possible with the current plugin system (I can be wrong, though; I’m exploring it too, haha).

But what I think you can do is, instead of adding the StateNode as a custom type, you only allow his instancing on the scene through a plugin GUI (something like Path2D node, in which when you click on the node, buttons appear on the upper tab of the 2D editor - in your case, when you click on the StateControllerNode, a button to add States appear). With that, is possible for you to instance inside the ControllerNode a StateNode.scn you create yourself (I’m imagining something like an empty node as a root, and as his child a node with a script attached to it, with all the StateNode methods you want the user to have access; it will be a ‘composing over inheritance’ architecture).

I’m not adding this option as an answer because people may overlook your question if it has already an answer, and I can possibly be wrong thinking that what you said is not possible. But at least this is a possible design for your plugin, although you will have some more work to do with the interfaces and stuff.

henriquelalves | 2016-03-10 04:42

Note: you can surround code in grave accent characters –How to write underscore "_" in comments? - Archive - Godot Forum

Bojidar Marinov | 2016-03-10 09:54

Thanks for your answer. Yeah I think I can do that for now.
I’ll try to find another way. I think that for now I can have only the StateController node and make the users add a simple Node as a child of it and add a custom script that extends “res://addons/state_controller/state.gd”.
Its not the best solution but it works fine.

fede0d | 2016-03-10 15:24