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

Add automatic imports for node and class references #3695

Open
Jummit opened this issue Dec 19, 2021 · 5 comments
Open

Add automatic imports for node and class references #3695

Jummit opened this issue Dec 19, 2021 · 5 comments

Comments

@Jummit
Copy link

Jummit commented Dec 19, 2021

Describe the project you are working on

A large projects with many node and class references:

image

Describe the problem or limitation you are having in your project

Storing node references and including scripts as types is something very common, but is not automated inside Godot without using external tools.

I wrote an addon to automate the task, which I use every day: https://github.com/Jummit/node-referencer-addon

It isn't as quick as I would like it though. (I have to open the script I want to include, go to Tools>Reference>Copy Class Reference and then paste the reference in the script I need it in)

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

Something available in many other languages is a feature called "Auto-Importing". When referencing a class that hasn't been imported yet, autocompleting it will add the import to the top of the script. This is also something that can be exposed to the language server.

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

When writing the name of a script in PascalCase, an autocompletion hint is added that when triggered, adds a line like this to the top of the script:

# Automatically generated include:
const PlayerStatus = preload("res://player/player_status.gd")

func _ready():
   var status = PlayerStatus # Written by autocompleting the word "PlayerSt"

Mockup:

image

When writing the name of a node in the scene tree, the node name shows up in the autocompletion, and when triggered, adds the node reference to the top:

# Automatically generated reference:
onready var status_label = $CenterContainer/StatusLabel

func _ready():
   status_label # Written by autocompleting the word "status_l"

Mockup:

image

When the project setting "Add Type Hints" is enabled, these could be typed as well.

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

It will be used often.

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

As I mentioned, I wrote an addon which isn't as well integrated into Godot. It also doesn't give the language server access to the functionality.

@KoBeWi
Copy link
Member

KoBeWi commented Dec 20, 2021

Sounds like perfect use-case for #1047
You can use class_name instead of preloading script (it's technically the same plus you can move the class script around), but right now the issue is that it will pollute your node list.

@Jummit
Copy link
Author

Jummit commented Dec 20, 2021

I know that class_name would fix half of this issue, but I like being explicit about dependencies and dislike polluting the global namespace. Manually importing also gives you the ability to change the class name.

@Mickeon
Copy link

Mickeon commented May 4, 2022

I can see this would be immensely useful, but I can also imagine the way it would work may be a bit too ambiguous.
How would the Engine decide what written name is assigned to any given script? It can't automatically determine it, 'cause it's very much possible two similar scripts around the project can share the same name. So, it needs to be defined within the script, but that can already be done with class_name, exposing them to the global namespace, and having similar functionality just to make this process work would be puzzling.

@Jummit
Copy link
Author

Jummit commented May 4, 2022

I have thought about this proposal after godotengine/godot#60298 was merged, and I agree that using this feature and class_name no automatic imports would be needed. I still think there are use cases for both, and I will leave this proposal open as it has seen some support.

@Mickeon
Copy link

Mickeon commented May 4, 2022

I see potential in the idea, whatever the solution may be. Cluttering the global namespace undeniably scales horribly on larger projects, so some way to make that less of a burden is very welcome.

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

4 participants