#Is there a way to get the current autoloads in a @tool script?
1 messages · Page 1 of 1 (latest)
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.
i figured it out
Autoloads are attatched as nodes to the node at "/root". That might be a good starting point.
for item in t:
if item == get_tree().current_scene:
t.erase(item)
break```
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. 😄
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.
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.
The script suggestion, even though it took some finagling, was a great sort of suggestion.
@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)
Oh, an EditorScript, interesting. I didn't think to try that.
Should work in other tool scripts as well. EditorScript is just a utility to run the function through a menu item
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.
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.
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. 😛
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)
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/.
Just a little reminder about this if you need to work with the paths at some point. Besides that happy hacking 
Oh yea you can retrive the path by using ProjectSettings.get_setting with the name you got from reading the property list. (including autoload/)
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.
As long as you only need it at runtime yes. Thought I'd add it anyway for sake of completness
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.
You can credit my github if you want https://github.com/HolonProduction/ . If you like hacking Godot take a look at godot_standalone_plugin xD
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.
I see. The twitch takeover stuff ringed some bells
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
No worries! Was actually a decent reply, because I think early on I was struggling to get it to show stuff at all. 😛
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.
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.
It seems not to be possible to access autoload scripts in tool scripts. See blue info box in section "Editing variables" https://docs.godotengine.org/en/stable/tutorials/plugins/running_code_in_the_editor.html
You can load the scripts though with the paths that are returned, and that's all that matters.
(For my purposes.)
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.
Alright, if anyone's still here, here's my post on it: https://www.patreon.com/posts/connecting-to-107412611?utm_medium=clipboard_copy&utm_source=copyLink&utm_campaign=postshare_creator&utm_content=join_link I actually made it completely free and it includes the code for the solution. It's pretty neat, allowing you to connect to signals in your scenes with methods from any singleton.
woa i didnt know swift syntax highlighting works so well with GDScript