Hi all, here's a complicated one:

In a script, is it possible to refer to the script itself in a static function?

I mean, in an instance, you refer to the instance itself by using 'self', and you can get the script from 'get_script()'. But in a static function, there's no instance, how can I there refer to the script itself?

My problem is that I have the following script: (I include some surrounding context so to make it easier to understand)

extends "res://Structure.gd"

static func can_build(this, faction, region):
	var n = region.get_num_structures(this, faction)
	...
	return false

The parameter 'this' will hold the script itself. It's name could be anything, so 'this' is just a name.

The static function 'can_build' is called like this, from another script:

func create_menu():
	for o in db.data.structure.values():
		if o.template_script.can_build(o.template_script, player.faction, player.character.current_region):
		...

Here, the 'o.template_script' has previous been loaded like this:

template_script = load("res://structure/%s.gd" % json.script)

thus, loading a 'gd' file.

All this works, but notice the construction

o.template_script.can_build(o.template_script, ...

When calling the static function on the script, I need to pass in the script itself as a parameter.

But I would like to be able to directly refer to this from within the script. Like this:

static func can_build(this <- remove, faction, region):
	var n = region.get_num_structures( <get the script itself>, faction)

However, I cannot find any keyword, property or method to achieve this. Is it possible?

If not possible, I think that the 'self' keyword could be used for this purpose.

As it is now, if you put it in a static function, you get the error that an instance is required. I would suggest that could be changed, so that the self keyword instead could be a reference to the calling script, when used inside a static function.

It would mimic the non-static case, where 'self' inside a function is a reference to the calling object.

8 months later