How to instance a 2D Node where there are multiples possible images by GDScript?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cloa513

There is only mouse clicking on objects in this game so it doesn’t need the physics engine. When creating instances of the 2D Node, what is the easiest way to create them with one of nine images (and set some variables)?

There is no need to interchange these images midgame.

:bust_in_silhouette: Reply From: Inces

Node2d with an image is a Sprite node. If You really don’t want to add any script to your sprites You can use Autoload singleton ( or any menaging node in the scene tree ). Your manager node will have variable of type area containing all possible images. It will also have a method called on mouse click to pick one of those images according to chosen algortihm ( or randomly ) and instance new sprite, set its texture as chosen image, set sprite metadata as your chosen variables, and finally add child of complete Sprite.

Some typo like area rather than array as you mean. I intend to have Sprite nodes instanced on starting because there 900 of them and they will have probably need to have script. It is a normal integer variable needed to set for each of them. The Sprites nodes will be mouse clickable.

cloa513 | 2021-10-28 12:37

You don’t need to instance all 900 sprites. Preload all of your 900 images instead, it will be faster. Then on mouse click there will be only one Sprite instanced and childed at a time, and one of those images will be chosen for it. Images are of type Resource and you only need to preload them once, no need to instance, multiple sprites can use the same preloaded image. And as I said, if You only want sprites to keep one integer value, You also don’t need script for them, instead use set_metadata() and get_metadata()

Inces | 2021-10-28 13:50

There are not 900 images, only 9 are stated. There will be script to use the integer value- its a Sudoku. As always GODOT jargon trips me up.

cloa513 | 2021-10-28 23:16

I see :). If so You can think of a TIleMap instead of Sprites. TileMap has useful methods, that will make counting total values in row and column much easier. It is always symmetrical, You won’t have to count positions for sprites to be adjacent to each other. Tilemap has methods to keep images and set them to your sprites easily. You can also use another tilemap on top of the main one to hide tiles from the player.

Inces | 2021-10-29 08:33

Buttons seems to have the all the functionality that I need.

cloa513 | 2021-11-03 02:41