derkork / godot-resource-groups

An add-on for the Godot engine that simplifies handling of resources
MIT License
194 stars 6 forks source link

Scan for resource groups define in exported vars #9

Open clm805 opened 3 months ago

clm805 commented 3 months ago

Currently the plugin only scans for ResourceGroup resource files. Would it be possible to also scan for ResourceGroup's that are attached to exported var's in the editor via the "New ResourceGroup" option? This would allow me to avoid creating files for resource groups that will only be used in a single node.

Screenshot 2024-08-05 at 3 07 50 PM

derkork commented 3 months ago

Technically it should be possible to do so, however it would incur finding all scenes in the project, then opening every scene, instantiating it, walking over all nodes, extracting the exported properties of each node, if the node exports a resource recursively walking that resource's properties, etc. and thus finding any exported resource group, updating the resource group and then re-save the scene. There may also be editor scripts attached to nodes which may interfere with the process. This may inadvertently break scenes in the project. If scenes are particularly large it may also take a while. A similar thing would need to be done for resources which include an exported ResourceGroup.

Another option would be to patch the scene/resource files low level, by parsing the tscn/tres format manually, finding entries for resource groups and patching these at text level. This would probably execute faster (though we still would need to open a lot of files in the project) and also has less chance of messing up the scenes but would require a low level tscn/resource file parser/writer which isn't exactly straightforward to implement in GDScript (I have implemented one in Python, so I know how to do it, but for Python there exists proper tooling for parser generators like ANTLR, which sadly doesn't exist for GDScript).

So neither approach is particularly great. If you have another idea on how it could be done, ideally without opening every resource file and scene in the project, I'd love to hear about it.

clm805 commented 3 months ago

I’m pretty new to godot and completely new to plugins, but is there a build hook for tool scripts similar to the one I see you implement for the plugin? Such that the ResourceGroup script could itself request the scan at build time.

derkork commented 3 months ago

I am only aware of a generic hook indicating "build starts now". And this is only called on editor plugins, so a resource group inside of a scene would not get called.

clm805 commented 3 months ago

I think I understand it better now, that the plugin has to access only files at build time and not loaded nodes. In that case, to reduce the number of files, how about creating a "ResourceAtlas", where it exports an Array[ResourceGroup] and processes each of those. You can add an id var to ResourceGroup and the atlas will insert the processed ResourceGroup into a dictionary with its id as its key for easy access (I'd say just use a dictionary from the start but as I understand it gdscript doesn't support typed Dictionaries). That way I can define all of my groups in one file.

derkork commented 3 months ago

Yes that would be an option, however this is really so simple that its probably better done in a custom script for each project e.g..

class_name ResourceAtlas

@export var all_levels:ResourceGroup
@export var all_enemies:ResourceGroup

etc.. The amount and content of the fields is of course specific to the project.