Topic was automatically imported from the old Question2Answer platform.
Asked By
Fenisto
I’m trying to parse through a node’s children and get their indexes. Which is successful with get_child().
Then i’m trying to access every child’s child which is a sprite and change it’s texture value. But it doesn’t seems to work.
For example:
for i in range($Heroes.get_child_count()):
$Heroes.get_child(i).Sprite.texture = load(“res://Sprites/Archer.png”)
Editor says “Invalid get index ‘Sprite’ (on base: Node2D’).”
The point is to access Hero1, Hero2 etc. nodes, and their sprite nodes. And change sprite.texture of every Hero* on a go.
Your suggestion is if there is only “Heroes” node that has a bunch of sprites.
Fenisto | 2020-01-16 10:43
As far as I know get_children() and get_child() will get only the direct children (opposed to find_node()). So it doesn’t recurse.
You could do it this way:
for i in range($Heroes.get_child_count()):
var spr = $Heroes.get_child(i).get_node("Sprite")
if spr != null:
spr.texture = load("res://Sprites/Archer.png")
var hero_child_node_arr:Array = $Heroes.get_children()
for i in range(hero_child_node_arr.size()):
if hero_child_node_arr[i].get_class() == "Sprite":
hero_child_node_arr[i].set_texture(load("res://Sprites/Archer.png"))
The idea is that you need to check if the child node is a Sprite first, then if it is, set the texture.
Sorry, same thing as the previous answer.
Need to get “Heroes\Hero*\Sprite.texture”. Not “Heroes\Sprite*”