godotgdscriptduck-typing

How do I set a function in another node in Godot?


I have a custom integrator written in a parent node's script, and I need to put it on multiple child nodes.

I read Godot's intro to GDScript and what it said about duck typing. From what I understood, I thought i could just do this:

child_obj._integrate_forces = my_integrator

But this freezes the app and gives a "property doesn't exist in node" error. What's the correct way to do this?


Solution

  • How do I set a function in another node in Godot?

    You can't.

    I do not know how you deduced this from duck-typing, but no... GDScript objects do have methods, not just properties that happen to have functions (which you would find, for example, in JavaScript).

    You could place your method in a separate Script (perhaps make it an static func, or make the script an autoload, or some Node you know will always be available - which is what you did) and call it.

    Alternatively, if it makes sense, you could use inheritance.