Insatantiate same scene more than once with the same name

how can i instantiate the same scene more than once without it changing name? im making a chess-like game and im adding support to Forsyth-Edwards Notation (letters are the type of piece, lower/uppercase is the color, forward slash skips row, number is the amount of empty spaces)

im running into trouble when instantiating the piece scenes, the first piece comes out fine (e.g. Pawn), but every time the same piece is placed within the same parent(Blue or Red teams) it gets named something like @Node2D@2 , @Node2D@3 etc

i need them to spawn with the same name or at least something like Pawn2, Pawn3, otherwise the scriptfor selecting the sprite wont work

func _genUnits():
	var startFEN = "pppppppp" #spawning multiple pawns as a test
	var typeFromSymbol = {
		"K" : King, 
		"P" : Pawn, 
		"N" : Knight,
		"B" : Bishop, 
		"R" : Rook, 
		"Q" : Queen
	}
	
	var file = 0 #columns
	var rank = 7 #rows
	
	for i in startFEN:
		if i == "/": #skip row when forward slash
			file = 0
			rank = rank - 1
		else:
			if i.is_valid_int(): #if number, skip tiles
				file = file + i.to_int()
			else: #if letter
				var pieceColor = {
					"blue": 0, "red" : 1
					}
				if i == i.to_upper(): #if uppercase, blue. if lowercase, red
					pieceColor = 0
				else:
					pieceColor = 1
				
				var unit = typeFromSymbol[i.to_upper()]
				var curUnit = load(unit).instantiate()
				
				if pieceColor == 0:
					$"../../Teams/Blue".add_child(curUnit)
					curUnit.position = Vector2(file*16, rank*16)
				else:
					$"../../Teams/Red".add_child(curUnit)
					curUnit.position = Vector2(file*16, rank*16)
				file = file +1

thanks in advance!

You can’t have the same name because will make impossible to use get_node, but you can rename the node to have your desired pattern, use Node.name = "name you want", example:

if pieceColor == 0:
	$"../../Teams/Blue".add_child(curUnit)
	# The first one will keep the name piece, the subsequent nodes
	# will be piece2, piece3, etc
	curUnit.name = "piece"
	curUnit.position = Vector2(file*16, rank*16)
1 Like

it worked!

i changed the dictionary to only have the names of the pieces, and created another one for the scenes. the nodes are named like Pawn1, Pawn2 and the Sprite2D code removes the last character before setting the texture to TeamUnit (e.g. BluePawn)

@onready var Bishop = "res://Assets/Units/bishop.tscn"
@onready var King = "res://Assets/Units/king.tscn"
@onready var Knight = "res://Assets/Units/knight.tscn"
@onready var Pawn = "res://Assets/Units/pawn.tscn"
@onready var Queen = "res://Assets/Units/queen.tscn"
@onready var Rook = "res://Assets/Units/rook.tscn"

func _genUnits():
	var typeFromSymbol = {
		"K" : "King", 
		"P" : "Pawn", 
		"N" : "Knight",
		"B" : "Bishop", 
		"R" : "Rook", 
		"Q" : "Queen"
	}
	
	var sceneFromType = {
		"King" : King, 
		"Pawn" : Pawn, 
		"Knight" : Knight,
		"Bishop" : Bishop, 
		"Rook" : Rook, 
		"Queen" : Queen
	}
				var unit = sceneFromType[str(typeFromSymbol[i.to_upper()])]
				var curUnit = load(unit).instantiate()
				
				if pieceColor == 0:
					$"../../Teams/Blue".add_child(curUnit)
					curUnit.position = Vector2(file*64+32, rank*64+32)
					curUnit.name = str(typeFromSymbol[i.to_upper()],"1")
				else:
					$"../../Teams/Red".add_child(curUnit)
					curUnit.position = Vector2(file*64+32, rank*64+32)
					curUnit.name = str(typeFromSymbol[i.to_upper()],"1")

sprite2D code (child of unit Pawn or Rook etc which is instantiated as child of node Blue or Red)

extends Node2D

func _ready():
	var unit = self.get_parent()
	var unitName = unit.name.left(unit.name.length()-1)
	var team = unit.get_parent().name
	self.texture = load("res://Assets/Textures/Units/"+str(team)+str(unitName)+".png")

i set startFEN to rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR (the starter positions in chess) and it looks like this

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.