Loading textures for sprites that are created dynamically. Texture atlas/sprite sheet tips.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By steely
:warning: Old Version Published before Godot 3 was released.

My project contains many sprites that are loaded dynamically, rather than placed in a scene with the editor. These sprites must be able to dynamically change which texture they use. The obvious solution is a texture atlas, but I’m unclear how this should be done in Godot. The texture atlas documentation has very little information about the correct way to use it for my project.

How should I create the texture atlas? Is there a Godot tool that can handle this, or do I need to do it with a 3rd party tool/manually in Photoshop? I’ve seen the pseudo-atlas that Godot produces when the appropriate export option is set, but I don’t know if that would be useful in my case.

When and how should I load the texture atlas to make it available to dynamically created sprites? Should there be a texture object of some sort in the main scene, or a function call in the ready function of the main scene?

What’s the best way to select a sprite from the atlas? Are sub-textures indexed in some way, or do I need to retrieve them with a rect of texture coords?

Any other texture atlas issues I need to consider (neighbor pollution, etc.)?

:bust_in_silhouette: Reply From: volzhs

You can make texture altas automatically and don’t need any changes of code or anything else.

Open Export > Image Groups
Make a group name and select images you want to make a atlas.

So how do I apply a sub-texture from this atlas to a dynamically created sprite? Do I retrieve sub-textures based on their filename somehow? If I retrieve them with and index or texture coords, how does it handle adding new images to the atlas?

steely | 2016-06-24 14:55

You can just do load("res://path/texture.png").
Just pretend to make your game without atlas.
Godot does rest things magically when export.

volzhs | 2016-06-24 16:39

So I need to have ~100 lines at the start of the dynamic sprite script, loading all possible images? Is the engine smart enough that creating ~1000 instances of the dynamic sprite won’t incur a huge cost in load times, memory usage, or poor performance by flooding the cache?

steely | 2016-06-25 00:46

You need to test it for yourself.

volzhs | 2016-06-25 01:48