XML parse img error

GODOT-4.2
Hi have a question, im trying to use XML to load sprite from my game from scraps
this is the code
pawn.gd

func _load_stats_from_xml():
	pawn_stats = Utils.load_pawn_stats_from_xml("res://stats/pawn_stats.xml")
	if pawn_stats.has(pawn_class):
		var stats = pawn_stats[pawn_class]
		move_radious = stats["move_radious"]
		jump_height = stats["jump_height"]
		attack_radious = stats["attack_radious"]
		attack_power = stats["attack_power"]
		max_health = stats["health"]
		curr_health = max_health
		
		# Leer la ruta de la sprite desde el XML
		if "sprite" in stats:
			var sprite_path = str(stats["sprite"])
			print("Sprite path:", sprite_path)  
			$Character.texture = load(sprite_path)
	else:
		print("Error: No se encontraron estadísticas para la clase de peón:", pawn_class)

and XML files is

<pawn class="0">
		<sprite>assets/sprites/characters/chr_pawn_knight.png</sprite>
		<move_radious>3</move_radious>
		<jump_height>0.5</jump_height>
		<attack_radious>1</attack_radious>
		<attack_power>20</attack_power>
		<health>50</health>
	</pawn>

idk why my sprite arent loading correctly.
and debugger error is:

print from script

Stats de peones cargados:{ 0: { "sprite": 0, "move_radious": 3, "jump_height": 0.5, "attack_radious": 1, "attack_power": 20, "health": 50 }, 1: { "sprite": 0, "move_radious": 5, "jump_height": 3, "attack_radious": 6, "attack_power": 10, "health": 35 }, 2: { "sprite": 0, "move_radious": 5, "jump_height": 3, "attack_radious": 6, "attack_power": 10, "health": 35 }, 3: { "sprite": 0, "move_radious": 5, "jump_height": 3, "attack_radious": 6, "attack_power": 10, "health": 35 }, 4: { "sprite": 0, "move_radious": 5, "jump_height": 3, "attack_radious": 6, "attack_power": 10, "health": 35 }, 5: { "sprite": 0, "move_radious": 5, "jump_height": 3, "attack_radious": 6, "attack_power": 10, "health": 35 }, 6: { "sprite": 0, "move_radious": 5, "jump_height": 3, "attack_radious": 6, "attack_power": 10, "health": 35 } }
Sprite path:0
E 0:00:02:0306   pawn.gd:58 @ _load_stats_from_xml(): Resource file not found: res://0 (expected type: )
  <Error de C++> Condition "!file_check->file_exists(p_path)" is true. Returning: Ref<Resource>()
  <C++ Source>   core/io/resource_loader.cpp:279 @ _load()
  <Stack Trace>  pawn.gd:58 @ _load_stats_from_xml()
                 pawn.gd:160 @ _load_stats()
                 pawn.gd:179 @ _ready()

the png files have the correct path*
im trying to load sprite from a xml file, but idk if im doing it correctly, can you help me? thanks a lot, sry for my bad english :slight_smile:

You did not post the parsing function so my only guess is that it’s not parsing the path correctly.

1 Like

this is the parse funct from utils.gd

static func load_pawn_stats_from_xml(file_path: String) -> Dictionary:
	var parser = XMLParser.new()
	var pawn_stats = {}

	var error = parser.open(file_path)
	if error != OK:
		print("Error al abrir el archivo XML:", error)
		return {}

	while parser.read() != ERR_FILE_EOF:
		if parser.get_node_type() == XMLParser.NODE_ELEMENT:
			var node_name = parser.get_node_name()
			
			if node_name == "pawn":
				var pawn_class = int(parser.get_named_attribute_value("class"))
				var stats = {}

				while parser.read() != ERR_FILE_EOF:
					var node_type = parser.get_node_type()
					if node_type == XMLParser.NODE_ELEMENT_END and parser.get_node_name() == "pawn":
						break

					if node_type == XMLParser.NODE_ELEMENT:
						var stat_name = parser.get_node_name()
						parser.read()  # Leer el nodo de texto
						var stat_value = float(parser.get_node_data())  # Obtener los datos del nodo de texto
						stats[stat_name] = stat_value

				pawn_stats[pawn_class] = stats

	print("Stats de peones cargados:", pawn_stats)
	return pawn_stats

float('path_to_sprite') will return 0

now i have it like this

					if node_type == XMLParser.NODE_ELEMENT:
						var stat_name = parser.get_node_name()
						parser.read()  # Leer el nodo de texto
						var stat_value = float('path_to_sprite') will return 0  # Obtener los datos del nodo de texto
						stats[stat_name] = stat_value

but have another error
Línea 38:Expected end of statement after variable declaration, found "Identifier" instead.

uh? I just said that float('a_path_to_a_sprite') will return 0 which is the issue you are having when parsing the <sprite /> content.

when i parse the xml, dont have issue with stadistics, but i have issue with sprite path
xml file is this

<pawns>
	<pawn class="0">
		<sprite>res://assets/sprites/characters/chr_pawn_knight.png</sprite>
		<move_radious>5</move_radious>
		<jump_height>3</jump_height>
		<attack_radious>6</attack_radious>
		<attack_power>10</attack_power>
		<health>35</health>
	</pawn>
</pawns>

and this line
<sprite>res://assets/sprites/characters/chr_pawn_knight.png</sprite>
cannot be read correctly from the script just as i said in the first post

finally i cant load textures from an xml file, but i made another godot file and it work, thanks you, if anybody knows how to load sprite files from a xml, pls let me know :slight_smile:

if node_type == XMLParser.NODE_ELEMENT:
	var stat_name = parser.get_node_name()
	parser.read()  # Leer el nodo de texto
	var stat_value 
	if stat_name == "sprite": 
		# Si el tag es "sprite" el contenido es un String
		stat_value = parser.get_node_data()
	else:
		# En caso contrario el contenido es un float
		stat_value = float(parser.get_node_data())  
	stats[stat_name] = stat_value