#Is there a way to get the current autoloads in a @tool script?

1 messages · Page 1 of 1 (latest)

karmic bolt
#

I'm looking for a method somewhere in Engine or SceneTree or ProjectSettings to get all the currently-listed autoload singletons while in a tool script. It seems it might not be possible, but I just want to double-check myself. Has anyone ever done this?

sweet tapir
#

Engine.get_singleton_list()

#

oh, maybe not in a tool script though?

karmic bolt
#

That only returns the global singletons, not the autoload singletons.

#

I think it's because the autoload singletons are only loaded at startup. But I figure there's got to be somewhere that they are stored. Maybe I'll try printing off all the project settings and see if they're included anywhere.

primal widget
#

Autoloads are attatched as nodes to the node at "/root". That might be a good starting point.

sweet tapir
#
    for item in t:
        if item == get_tree().current_scene:
            t.erase(item)
            break```
karmic bolt
#

They are sadly not attached until after the game is in the startup sequence - they don't exist until it begins to start the game. I have a way I'm trying it, but it's...unhappy, and just managed to crash Godot. 😄

primal widget
#

I think they are if the autoload is a @tool
You should also be able to get a list of them through the project settings. But you will have to handle the difference between scene and script autoloads yourself I guess

#

Also you need to erase * from the beginning if the paths starts with it.

karmic bolt
#

They don't seem to be loaded if the autoload is a scene that's a @tool, at least as far as I can tell. I'm not sure yet about a script, although that might be a better plan of action anyway.

karmic bolt
#

The script suggestion, even though it took some finagling, was a great sort of suggestion.

primal widget
#
@tool
extends EditorScript


# Called when the script is executed (using File -> Run in Script Editor).
func _run() -> void:
    for setting in ProjectSettings.get_property_list():
        if ((setting["name"] as String).begins_with("autoload/")):
            print(setting)


karmic bolt
#

Oh, an EditorScript, interesting. I didn't think to try that.

primal widget
#

Should work in other tool scripts as well. EditorScript is just a utility to run the function through a menu item

karmic bolt
#

Oh I know - I ran the Godot addons/@tool script takeover for the Godot official Twitch channel just last month. 😛 It definitely does not work as far as I can tell - the other scripts, I mean. Of course when you run an EditorScript, that'll work.

#

There's no trigger in non-EditorScripts afaik to get the code to actually run is part of the issue. I'll test it out some more, but for now, the exporting a script is actually more effective anyway.

#

I'll actually just try it real quick on a property setter, one sec.

primal widget
#

Well the original question was:

I'm looking for a method somewhere in Engine or SceneTree or ProjectSettings to get all the currently-listed autoload singletons while in a tool script.
Here you have it. Can't do much more without knowing what you are trying to do.

karmic bolt
#

Ahh, it does work on a property setter actually. It's all good, you got it! I'm trying to hack something so ugly it's unbelievable, but I'll post again here when I've got it done. You were right though, that absolutely does work, glad they were in the ProjectSettings like I thought even if I didn't see anything for it immediately.

#

Essentially, someone online was asking for a way to connect a function to a signal when the signal is any ol' object in a scene, and the function comes from an autoload. Because the editor doesn't support that, I've been spending some time looking for this workaround, so what I'll do is take your idea and make it update a property list for the current autoloads as a string enum, then when one of those is selected, I'll look up that autoload and get the associated methods off and add them again as a string enum.

#

The way they did it was at best extraordinarily painful. I figure since I'm sort of known to be an absolute hacker in the community, I might as well attempt it better. 😛

violet yacht
#

maybe you can use the project.godot file

@tool
extends Node

func _ready() -> void:
    var configfile = ConfigFile.new()
    configfile.load("res://project.godot")
    var section = "autoload"
    for key in configfile.get_section_keys(section):
        var value = configfile.get_value(section, key).trim_prefix("*")
        prints(key, value)
karmic bolt
#

That's possible, though I think we did figure it out up above - Roni had the great idea of loading the project settings and looking for any that start with autoload/.

primal widget
#

Oh yea you can retrive the path by using ProjectSettings.get_setting with the name you got from reading the property list. (including autoload/)

karmic bolt
#

Can't I just get the name itself and use that from get_tree().root.get_node(name)? That's where autoloads seem to be loaded. I don't need to physically do anything with them until the game/scene loads up, so just need the current autoload name and method name afaik.

#

Oh wait, right - and remove autoload/ from the front of it.

primal widget
#

As long as you only need it at runtime yes. Thought I'd add it anyway for sake of completness

karmic bolt
#

Oh, great then. Yeah, sounds good!

#

Do you have a Twitter account or anything? I'll be writing this up and would like to credit you.

primal widget
karmic bolt
#

Oh, I didn't know you were Holon! Good stuff. I'm KyleSzklenski in dev chat - we were discussing the refactor tooling proposal.

#

I was the one who had the very mediocre but still somewhat effective plugin that adds the refactoring menu.

primal widget
#

I see. The twitch takeover stuff ringed some bells

hollow dirge
#

As far as I remember, if you make an autoload a tool, it loads automatically the next time you restart the editor.

#

I misread the title xD

karmic bolt
#

No worries! Was actually a decent reply, because I think early on I was struggling to get it to show stuff at all. 😛

hollow dirge
#

In my case, I put them all in a group and then search for them by group, but of course, that's for when they're already loaded in the editor or you need to know during gameplay.

karmic bolt
#

Yeah, exactly. I was considering doing exactly that originally as well.

#

But since I want the editor to update when you select stuff, rather than having to hope you type in a string correctly, I figure this is better.

junior ibex
karmic bolt
#

You can load the scripts though with the paths that are returned, and that's all that matters.

#

(For my purposes.)

karmic bolt
#

Okay, all finished. Sorry for the ping again, but @primal widget, if you want the solution, because you helped me figure it out altogether, let me know, can send it you directly in a PM or whatever, and/or the write-up that I'll be making for my Patreon.

karmic bolt
lost crater