Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistencies in retrieving scripts: preload, load, get_script() with const/var #12719

Closed
willnationsdev opened this issue Nov 7, 2017 · 3 comments

Comments

@willnationsdev
Copy link
Contributor

Godot 3.0 master b279f64

Let's assume my goal is to create a property on a base script (base.gd) that will be a reference to its own script resource. Ideally, derived nodes at an arbitrary depth in the inheritance tree will still be able to fetch the base class's resource_path to know where that file exists, and to pull other information related to that script if necessary. Creating this variable in a preferred way is proving to be impossible at the moment.

# base.gd
# Attempt 1: preloading a constant
const Base = preload("base.gd") # error, Can't preload itself (use 'get_script()').
# Attempt 2: get_script()-ing a constant
const Base = get_script() # error on the following line, Expected constant expression
# Attempt 3: get_script()-ing a var
var Base = get_script() # Works! but now if a derived class references it, it will return the derived script rather than the base type's script. Not what we want.
# Attempt 4: preloading a var
var Base = preload("base.gd") # same problem as with const. To be expected.
# Attempt 5: loading a var
var Base = load("base.gd") # this is the only version that ACTUALLY does what we want. It compiles, it works, and it allows derived types to access the base.gd script simply by referencing their Base property.

It would be preferable if this could be handled with a const and if preloading the same script were permitted.

@KoBeWi
Copy link
Member

KoBeWi commented May 7, 2019

Let's assume my goal is to create a property on a base script (base.gd) that will be a reference to its own script resource.

But why would you need that if you can just use get_script()?

@willnationsdev
Copy link
Contributor Author

Because you can't call get_script() from a static function, but you can access constants, even inherited ones.

@KoBeWi
Copy link
Member

KoBeWi commented Apr 23, 2024

Closing in favor of godotengine/godot-proposals#391

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants