How does const resource work? Edit: It's bugged, probably

Godot Version

4.4

Question

Not really a problem to be solved here anymore, I’m just still trying to wrap my head around the system.

So I just spent like an hour or two stuck in some weird behaviour…Then I finally realize it’s because I used const to declare my resource.
const THE_RESOURCE: MyResource = preload('uid:something')
My thought was simple. I won’t be modifying the content pointer of the variable, so I should make it a const.
As a result, whenever I do “THE_RESOURCE.some_value”, it always return the default value; and trying to directly set the value gives error.
HOWEVER. If I write “THE_RESOURCE.set_value(new_value)” and call it, the resource value DOES gets updated. So…it’s still accessing the unique resource somewhat.
But then trying to get the value back still returns the default, so is it like, keeping a shadow copy of the unique resource, but can still call the function on the resource?
I found nothing about this in documentation, so it’d be nice if someone with deeper low level knowledge can share some insight, then maybe we can make comments on the doc or something.
That’s about it, thanks for reading.

1 Like

There seems to be some sort of ‘caching’ going on when loading resources as constants.
You can reassign you constant resources to a var and then change the value. When read you only get the updated value from the var while the const is still the same - even though they point at the same resource/object.

This is only like that since 4.3 and IMO it is a bug. There is an open issue about this:

Okay, so it’s probably a bug…at least being changed between 4.2 to 4.3 shouldn’t be intended.
I guess there’s not much to learn here, I’ll just use static var as workaround and move on for now.