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

Path independent loading/instancing #3108

Closed
ctwobosius opened this issue Aug 9, 2021 · 4 comments
Closed

Path independent loading/instancing #3108

ctwobosius opened this issue Aug 9, 2021 · 4 comments

Comments

@ctwobosius
Copy link

Describe the project you are working on

Large scale project where I've had to refactor file names and locations quite a few times.

Describe the problem or limitation you are having in your project

Having to refactor all the preloads is clunky, and in general having to preload before instancing is also not the most elegant.

I saw that you could do

class_name SomeScene

and then do

SomeScene.new().

This works fine for built-in types. However, this is not the same as preloading then instancing custom classes/scenes (ie: SomeScene above)

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Preloading user designated classes automatically, then having UserClass.instance() would be much more elegant in my opinion, and save a lot of time refactoring (as well as abstract away file paths, which is nice :)

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

I'm thinking of two solutions:

  1. Mark stuff to be preloaded with a keyword, eg global like so:
# Yay.gd
global class_name Yay  # gdscript interpreter makes Yay a singleton or automatically preloads it in every other scene
# ...
# Main.gd
func _ready():
    var yay = Yay.instance()  # gdscript interpreter uses singleton or preloaded scene
    yay.position = Vector2.ZERO
# ...
  1. No marking of user defined classes, not sure how much of a pain it will be to code this version up
# Yay.gd
class_name Yay
# ...
# Main.gd
func _ready():
    var yay = Yay.instance()  # gdscript interpreter sees Yay.instance and preloads it
    yay.position = Vector2.ZERO
# ...

If this enhancement will not be used often, can it be worked around with a few lines of script?

We could keep the status quo I guess :3

Is there a reason why this should be core and not an add-on in the asset library?

This is something that can't really be achieved with an add-on as far as I know.

@ctwobosius
Copy link
Author

I'm also open to exploring and doing the PR myself, but not sure how to modify the gdscript interpreter, if anyone would like to give pointers on how to start doing that :)

@KoBeWi
Copy link
Member

KoBeWi commented Aug 9, 2021

Related: godotengine/godot#21187
class_name already supports custom icons, so adding a scene to it shouldn't be too difficult.

@dalexeev
Copy link
Member

Related: #1935.

@KoBeWi
Copy link
Member

KoBeWi commented Dec 16, 2021

This can be already achieved with a static method, as described here #1935 (comment)
Also for path-independence you can also use UIDs in Godot 4.0.

Closing, as it's mostly duplicate of #1935.

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