Archive 17/01/2023.

[SOLVED] Loading texture from user documents dir in lua

Mike

I’m desperatly trying to load a texture in lua using:

local texture = Texture2D():Load(fileSystem:GetUserDocumentsDir() .. "MyTexture.png")

I get “ERROR: Null image, can not load texture”.

friesencr

all resources are loaded from resource directories. urho has a virtual filesystem it imposes on its users. if you wanted to load a resource from the users directory you would have to add that folder as a resource directory.

Edit: sorry i didn’t see you were using load. i need sleep.

cadaver

This is a Lua bindings order issue. The string overload should be specified last in the .pkg file, but in this case the string overload is in the base class (Resource) while Texture2D Load() that takes an image pointer is registered later. I think you can work around it by loading an image from filename, then loading the image into your Texture2D.

Mike

I’ve tried this one with no success (same error):

local texture = Texture2D():Load(Image():Load(fileSystem:GetUserDocumentsDir() .. "Procedural.png"))

Is this what you were thinking about?

aster2013

Hi, mike,

Dont write all code in one line.[quote]local texture = Texture2D():Load(Image():Load(fileSystem:GetUserDocumentsDir() … “Procedural.png”))[/quote]

You need write like this:

[code]local image = Image()
image:Load(fileSystem:GetUserDocumentsDir() … “Procedural.png”)

local texture = Texture2D()
texture:Load(image)
[/code]

Mike

Thanks Aster,

From Cadaver’s explanations, it indeed makes sense to split the 2 loads and it works fine now.
However a segmentation fault is always reported when I exit (using ‘Esc’ key).

For example, if I replace the ‘logoTexture’ in Utilities/Sample.lua, a segfault is issued on exit.

aster2013

[quote]
However a segmentation fault is always reported when I exit (using ‘Esc’ key).
For example, if I replace the ‘logoTexture’ in Utilities/Sample.lua, a segfault is issued on exit.[/quote]

It because the Sprite keep the texture as shared pointer. For this issue you must create Texture2D with Texture2D:new() and then assign to Sprite.

Mike

Many thanks, it now works perfectly :stuck_out_tongue: