Godot Version
4.2.2
Question
Hi,
I have a Scene named “Creature”, this scene contains a Node2D that contains itself a AnimatedSprite2D.
In that Node2D I have the following Script :
class_name Creature
extends Node2D
# code....
func _ready():
position = position.snapped(Vector2.ONE * Constants.TILE_SIZE)
position += Vector2.ONE * Constants.TILE_SIZE / 2
position.y -= y_correction
func spawn(p_id: int, p_pos: Vector2, p_special: bool):
var anims = $Animation
id = p_id
position = Vector2(p_pos.x * 16, p_pos.y * 16)
special = p_special
var new_texture: Texture2D
if special:
new_texture = load("res://assets/animated/creature/overwolrd/" + str(id) + "s" + ".webp")
else:
new_texture = load("res://assets/animated/creature/overwolrd/" + str(id) + ".webp")
# Change AnimatedSprite2D textures
for anim_name in anims.sprite_frames.get_animation_names():
for i in anims.sprite_frames.get_frame_count(anim_name):
var texture : AtlasTexture = anims.sprite_frames.get_frame_texture(anim_name, i)
texture.atlas = new_texture
# code....
I have many creatures and I don’t want to create a scene for each one, so the idea is to instantiate a scene and change the AnimatedSprite2D textures, it works. But if I change the textures for one scene, it changes it for all others.
I tried local to scene, make unique, fake array, sub resource unique and whatever I found online, nothing works.
In my Main scene, I have a Node2D called “Creature_Group” with this Script attached :
extends Node2D
@onready var fauna = $"../../../Actors Manager/Creature"
@onready var creature_s = load("res://scenes/creature.tscn")
func _ready():
var creature_i = creature_s.instantiate()
var creature_1 = creature_i.duplicate()
creature_1.spawn(1, Vector2(8, 8), false)
fauna.add_child(creature_1)
var creature_2 = creature_i.duplicate()
creature_2.spawn(3, Vector2(4, 8), false)
fauna.add_child(creature_2)
So, how do I do so each Scene instance is unique and I can change their AnimatedSprite2D independently ?
Thanks.