Changing sprite of duplicated nodes

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

I have a node having a sprite which duplicated about 20 times. All these duplicated nodes load same image which I want to replace all at same time. How to replace the sprites of all duplicated nodes at the same time? Is it possible or do I have to do it manually? Is there any other way besides changing filename of new image to original image’s name? It would be better if it could be done by both through editor and script but editor is more important for me.

:bust_in_silhouette: Reply From: Zylann

In the editor, using instancing:
This is the most common solution. If you want to replace the sprite texture of all duplicate nodes to another one, then you need to make that node a separate scene, and instance it instead. This way, the instances will inherit all properties of the scene, and you’ll be able to change the texture in the scene, and that change will replicate everywhere you instanced it. More about instances here: http://docs.godotengine.org/en/stable/tutorials/step_by_step/instancing.html


In the editor, using multiselect:
If you don’t want to make your node a scene, you can probably select all sprites you want to change, and change their texture. I don’t use this often so I don’t know if it works or not.


In script, ingame:
You can’t modify base scenes the same way as in the editor here, so if you need to change all textures at once, get all the sprites and change the texture with a loop. Putting these into a group might help.


Doing a hack with AtlasTexture:
This solution is kind of a hack because it was not designed for this I think, but I mention it anyways:
You can introduce an indirection to the texture you chose for your sprite. Instead of putting a Texture directly, create an AtlasTexture and save it as a .res. Then assign the same one to your sprites. You end up with sprites referencing a shared AtlasTexture, which in turn references a Texture. So if you change the Texture of the AtlasTexture, it will change the Sprites automatically because they share the same resource.
I didn’t try this myself too, but it could probably work.

:bust_in_silhouette: Reply From: eons

Option 1: select all the sprites you want to change the texture image and load another image for it, not sure if this works by code, it may but never tried.

Option 2: edit the texture resource of a single sprite, load a new image, this may not update the image resource name, can be useful for some things but could make a mess too. At least on the editor, this won’t change the texture rect until you “touch” it.
Changing the image of the texture by code should affect all the instances that share the texture.

Option 3: edit the resource file name on the tscn file (close the scene on the editor first), the scenes and resources text format is really simple and you may find it faster to mass edit or copy parts of scenes or resources than using the visual editor (for animations, paths, tilesets, the text version is better sometimes).