godotengine / godot-proposals

Godot Improvement Proposals (GIPs)
MIT License
1.16k stars 97 forks source link

Expose script dependencies to editor plugins #10298

Open passivestar opened 3 months ago

passivestar commented 3 months ago

Describe the project you are working on

A components plugin that makes it easy to selectively add external scripts to the project

Describe the problem or limitation you are having in your project

There's no way to get a list of resources that a script preloads despite them being known at the parse time. As a result there's no easy way to copy script's dependencies when a script is copied to the receiving project

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

An API method that would return all of the dependencies for a given Script

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

Array[String] Script::get_script_dependencies()

It would return a list of paths for all of the preloads (and possibly other dependencies, I'm not sure how the GDScript parser works and if there's anything else that it treats as a dependency)

Alternatively it could return a list of resources that you can then get the paths to yourself via Resource.resource_path, whichever makes more sense

Note: There's also ResourceLoader.get_dependencies() which works for regular resources but not for scripts. An argument could probably be made that it could also return dependencies for scripts, but since it would break compatibility I think it makes more sense for this API to be on the Script class instead where all of the other reflection methods are

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

I could manually parse preloads from the source code but it won't be a few lines. Given that the Script class already provides some very useful methods to inspect scripts (getting methods, properties, etc) it would make sense for it to also expose dependencies as it can be useful for all kinds of editor plugins

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

This is already for an addon and there's no way to get it from it 🙂

AThousandShips commented 3 months ago

This would be specific to GDScript as there's no such method to expose on Script

passivestar commented 3 months ago

This would be specific to GDScript as there's no such method to expose on Script

I haven't looked at the source code but in the docs I can see that there are things like get_script_signal_list() on the Script class which I assume won't work with any language

Or if it does work with languages other than GDScript then I can't say that I see why get_script_dependencies() wouldn't, theoretically speaking. Two generic reflection-related methods seem to belong on the same class 🤔 I've never touched the .NET version of godot so I don't know what the existing reflection methods of the Script class return for C# scripts

AThousandShips commented 3 months ago

If it's just a matter of exposing existing functionality it's only available on GDScript, not general Script, but if it's about adding new functionality it could be added to Script

AThousandShips commented 3 months ago

For GDScript this is a bug btw:

Which was temporarily solved but unfortunately the fix had to be reverted