I'd like to create a script with a couple of references and simple methods that I can call from any node in my project.

For instance, in my projects, I tend to create a Main node that everything else is a child of, and, in each script, to define an "abbreviated self.name" variable called sn that I can quickly drop into print() calls so that when I'm testing, I can see where each printed message came from.

At the moment, I'm pretty much putting a copy of this code into every single script, which is far from ideal. It feels extraneous to have to tell every script where Main is, and if I ever want to adjust that initialise() function, I'll have to copy the changes into every script in my game. It would be ideal to be able to get every node to inherit the code by default, but there doesn't seem to be an elegant way of achieving that.

I've seen others asking similar questions before, and the suggested solution was usually either "define a custom class for every single node" or "modify the Godot source code." Surely there must be a simpler way?

The simplest solution in general would probably be to make a global object with a method like

func anything(obj, param):
	pass

and pass self to it, if needed.

func _ready():
	G.anything(self, param1)

However, you might want to look at print_debug(). It produces annotated output.

func _ready():
   print_debug('test')
test
   At: res://world.gd:15:_ready()

What duane said is good way. Another way is to add a node as child to your nodes. Which would use more resources.

Also, you could try writing a class function which is global created, although never put in a node it can be defined in your nodes.

    You can use an Autoload for this, with static functions that take Variants and can do whatever.

    Thanks for the suggestions, guys. For the moment, I've made things slightly less clunky by putting the functions in the Main node:

    And while I still need to include a reference to the Main node in every other script, at least this means if I change those functions, or decide to add more, it'll be easier to do so:

    newmodels Also, you could try writing a class function which is global created, although never put in a node it can be defined in your nodes.

    Could you please elaborate on this suggestion? I don't quite follow.

      See here for how Autoload works (also called Singleton or Global).
      https://docs.godotengine.org/en/stable/tutorials/scripting/singletons_autoload.html

      You can use an Autoload to save a reference to the main script. Then all other scripts can call it like this (assuming your global autoload is called Global, it can be named whatever).

      Global.main.set_self_name(self)

      Or you can put all the functions in the Autoload, essentially make main the autoload and do this:

      Main.set_self_name(self)
      19 days later

      Thanks again, guys. All good to know.