Dynamically loading images possible? (SOLVED)

#1

I’ve been tasked with creating a quiz game app, for mobile devices. The game will be image based, and will have hundreds of small-ish images, that will need to be switched on a per-question basis. I’d like to use Defold for this app.

Does Defold support the dynamic loading of images? Loading all games images up front/into memory will not be practical, so “on the fly” loading in/out is the only option. I’ll only need between 5 and 7 images per questions so, ideally, I’d like to be able to just load those images as they are needed.

I’d really like to make this my first Defold project, but I can’t determine if Defold is a fit.

Thanks

0 Likes

#2

I don’t believe that is currently possible. If you have some way to categorize them into smaller batches, you could split them up into collections and load/unload them when they’ll be needed. Beyond that, I don’t believe this is possible, which is quite a shame.

I suppose there might be some way to load the file using the Lua io utilities, but I’m not sure you can create that into an image in the game.

0 Likes

#3

It is possible, but only with GUI. See this example: https://github.com/britzl/publicexamples/blob/master/examples/loadtexture/loadtexture.gui_script

You would switch it from web to loading from the internal filesystem. The folder with the image textures will need to be defined to be included in the custom_resources game.project tag. Then it should be able to be loaded with sys.load_resource

5 Likes

#4

As @Pkeod said it’s currently only possible within GUI scenes, but for a Quiz game I think that would be a perfect fit anyway! Good luck!

1 Like

#5

Many thanks! I’ll give that a go…

1 Like

#6

Actually, it can be done. Go to /build/ folder, get an appropriate _generated_BLABLABLA.texturec file, copy it to custom resources folder then use resource.load \ resource.set
Ha! :slight_smile:

But seriously, this can be really useful (e.g. for animating cards as 3d models). And it seems to be not so hard to add… e.g. by adding binary buffer property to Image object.

1 Like

#7

So I guess loading an image from disk this way only works for files that are bundled with the application? Is it possible to load images that are not built-in?

1 Like

#8

With the new dmBuffer API you should be able to have dynamically loaded textures through native extensions. This is currently only available in the C API, but after next release (1.2.100) this will also be available in Lua! :slight_smile: @Mathias_Westerdahl has a cool little Lua snippet he showed last week where he updates a texture in runtime.

1 Like

#9

We have image.load() but it currently returns the image as a string for use with stuff like gui.set_texture(). It would be nice if image.load() played nicely with resource.set_texture() or things like that.

4 Likes

#10

Wondering what’s the latest on loading an image that is outside the bundle (from a path on disk in my case). I’ve seen @sergey.lerg extension, but was wondering if we have something already included.

Also do I still have to use GUI to load it or is it possible to load in on a go?

0 Likes

#11

Check this discussion: Factory > load image from web

1 Like

#12

Ohh fantastic. Missed that one!

Let’s see how far I push editor scripts :wink:

1 Like