godot_luaAPI icon indicating copy to clipboard operation
godot_luaAPI copied to clipboard

can't hold resource?!?!?!?

Open BestestHD opened this issue 2 years ago • 2 comments

pretty big issue i've been running into, it's seemingly impossible to load resources. i print the resource and godot just says [Object:null] here's my sample code

local bgImg
bgImg = load_texture("res://gfx/tex/bg/tex_bg_terminal_room.png")
print(bgImg)

load_texture returns a Texture2D, though no resource i've tried works. i haven't tried custom resource yet but that would be seriously damning, for the texture example i can just write a function that returns a sprite and loads the texture from the path onto the TextureRect's texture property but that isn't always an option for some of my game's resources.

please tell me if it works on your end, for all i know it's just because my build of godot 4 is a little out of date 😅 (waiting until a few specifics PRs are merged...)

BestestHD avatar Jul 14 '22 02:07 BestestHD

ok i realized that the issue is that reference-counting doesn't work for lua vars, pushing the resource into a reference array works as the resource stays referenced in gdscript. so change the issue to references i guess, i imagine this should(?) be trivial to fix

BestestHD avatar Jul 14 '22 02:07 BestestHD

While in theory this should work godot gets touchy when it dosnt own the memory. I will need to do some testing to figure out what the best method of resource loading from lua would be. In a ideal world lua wont own the resource but only have a reference to it. Sometimes lua owning the resource is fine but it also can cause lots of issues.

Trey2k avatar Jul 14 '22 12:07 Trey2k

ok i realized that the issue is that reference-counting doesn't work for lua vars, pushing the resource into a reference array works as the resource stays referenced in gdscript. so change the issue to references i guess, i imagine this should(?) be trivial to fix

As mentioned here the issue is with RefCounted specifically. We currently do not test if the object being passed is a RefCounted or not.

Trey2k avatar Jan 17 '23 18:01 Trey2k

I found out we are able to use metatables on lightuserdata. So that is what we are doing.

Trey2k avatar Jan 18 '23 14:01 Trey2k