godot-extended-libraries / godot-next

Godot Node Extensions - Basic Node Extensions for Godot Engine
MIT License
957 stars 61 forks source link

Attach multiple C# scripts per node #84

Closed GeorgeS2019 closed 3 years ago

GeorgeS2019 commented 3 years ago

Can someone point me to the section which allows attaching multiple cSharp scripts per node?

willnationsdev commented 3 years ago

Hey there. The trick to this is the CallbackDelegator script and the ResourceSet and Behavior scripts.

The CallbackDelegator is the GameObject-like node which maintains a ResourceSet of other scripted objects. The ResourceSet stores a unique collection of resources (only different scripted types), and must be configured with a base class. You can initialize it from the Inspector with the Behavior class or choose your own.

During each of the CallbackDelegator's engine notifications, it will attempt to call the same function on every object in the ResourceSet, thereby allowing you to treat the resources as if they were like Unity MonoBehaviours. This is the manner in which it makes it feel as though multiple scripts are allowed on the node (even though each node still technically has just 1).

Both of them require the use of the DelegationInspectorPlugin to be added to your EditorPlugin in order to function correctly. It allows the mentioned classes to customize the Inspector to add buttons and the like. They may also have a few other dependencies, iirc. The willnationsdev YouTube channel has an example of using them.

Let me know if you have further questions.