How to add new methods to existing nodes

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Andrea

Hi guys, can you point out a good tutorial on how to modify the built in nodes in godot?
In the docs i found about plug-ins, but they are good to add new custom nodes, while i would like to add a new methods to an existing one.

In particular, i would like to do an rpc call using the network ID from an array list.
It’s nothing complicated by itself, but since i need to use it many time in my project i would like my base node to have it by default. Something like:

func rpc_list(list, function_name):
   for id in list:
      rpc_id(id, function_name)
:bust_in_silhouette: Reply From: Wakatta

Pretty sure that’s the purpose of the extends keyword as you can create a custom class using Reference, Object, Node as the base then have every other node in your game extend from that.

Example Base

class_name Base extends Node
var foo_list = []

func rpc_list(list, function_name):
   for id in list:
      rpc_id(id, function_name)

Example other

class_name Ball extends Base

func roll():
    do_roll()

Example extra

class_name Glove extends Ball

func _ready():
    rpc_list(foo_list, roll)

Other than that to do exactly what you want would require building your own version of Godot from the source

yeah, extends and classes are very similar to plugins, they are good for creating custom objects, but i need to modify an existing one, othervise i need to create a custom class for every godot node type (eg: static body, rigid body, node2d, control, etc), while if i could add the new method to Node, every other node would inherit automatically

i think i have to learn more about the source then, thanks for pointing it out

Andrea | 2021-03-06 21:28

Yeah that’s a downside to this approach however you only need to do it for top level classes in your scene tree.

Example

[+] node_that_extends_base_class
  [-] every other nodes who's functionality i need

A simpler less invasive approach to achieve what you want though would be to have a singleton with all the functions you’ll need to use that accepts a node as one of its parameters

For each node that can access those methods use set/get_meta to set your lists

Wakatta | 2021-03-06 21:54

that’s it :slight_smile: i never used autoload, this solution is a good compromise between ease of use (accessible to every node) and ease of implementation
thank you!

Andrea | 2021-03-07 13:09