Why is the Error below given when trying to use an Object reference as key?
When defining and declaring the dictionary before the init function the compiler accepts it and the application starts.
It also correctly fills the dictionary as seen in the second screenshot.
Error at (13, 24): Invalid index type “GDScriptNativeClass” for a base of type “Dictionary[Object, String]”.
That may be, but this still does not explain the behaviour.
And also the following does not produce any errors, which gives the impression that Object reference and insatance are both of type Object
var object : Object = Object
var object_instance : Object = Object.new()
func _init():
object = Object
It’s tricky because GDScripts can also act as a type, so some types can cast down to Objects. Nevertheless it’s strange behavior I wouldn’t rely on at this point, and it’s certainly not helping your use case. Does my first post help your problem? If not you should post your real code/problem.
I am using it as a caching system.
I create the object the first time it is requested and place it in a cache/loaded dictionary.
This way the object will stay in memory and the next time it is requested i can check if there is already an instance of this object available.
I use it in a base class for UI Control pages.
When there is a child page it can be cached in a modular way and i don’t need to change any code when creating a new page object.
Caching might not be helping as much as you think, especially for UI. At a base level the operating system will already cache files read, so re-reading will be much faster. Godot will also cache loaded resources, guaranteeing a similar much faster re-loading. Adding objects to the scene tree will take resources, usually rendering and especially 3D rendering objects are the worst offenders. If you are trying to save from instancing and destroying the same UI panel, showing/hiding would be more performant but we’re talking about a minuscule operation that only happens on occasion, if it were per-frame then it might be worth looking into optimizations.
If you continue this route anyways, it sounds like you could use any type as the key; it may be better to use a string path to the scene or script you want to instantiate and keep.