From scene script, access that scene child node - scene instantiated multiple times

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

Hello Community,

I have a scene “Player”, that is instantiated twice in the parent scene “Game” using Editor’s button “instance scene”:

}- Player # instance of scene Player
}- <children of player>
}- Enemy # second instance of scene Player
}- <children of player>

From the instanced scene script, how do I access that particular instanced scene child, if there are multiple instances?

More details:
In the scene of type “Player” I have attached script with some variables, pointing to the children:

onready var avatar = $AvatarContainer/Avatar

I’d like to animate one player attacking the other, so I have come up with something like this:

// in script attached to root of Player.tscn
func animate_attack(other):
var jump_in_pos = Vector2(other.rect_position.x, avatar.rect_position.y)
var jump_back_pos = avatar.rect_position
#tween.interpolate_property... etc

Then in the main game script I invoke attack as follow:

onready var enemy = # valid path ...
onready var player = # valid path...

For both instances (‘enemy’ and ‘player’) the variable onready var avatar point to the same element in the SceneTree. Thus animation gets the same position, because function parameter other refers to player, and variable avatar refers to the player’s avatar (not enemy’s, like I expected). Final effect is no animation of enemy attacking player is visible.
If this is still vague, then perheps re-read the question after this explanation.
onready var avatar = $AvatarContainer/Avatar
results in both scenes setting the variable to the first instanced scene child node.
find_node(str(get_path()) + AvatarContainer/Avatar) results in null instance

So far I’ve read the tutorial and skimmed the docs/google, but with no effect.

Can you post the player.hit(enemy) code? I feel that the answer is somewhere in there. Or, better yet, upload a minimal example project if you can. I suspect that you do not reference the right nodes.

johnygames | 2019-08-13 13:29

Hey, it was error on my side when typing question.
The method should be animate_attack.
Forget about hit() - it’s unrelated, it updates model only.
I have separate player model (with hit()) and player view (with animate_attack()).
To reference the child I simply use avatar variable initialized with onready keyword, like mentioned earlier. I’ll try to come up with some minimal example.

bad_exception | 2019-08-13 15:20

:bust_in_silhouette: Reply From: bad_exception

OK I can answer my own question after working on the minimal example. :slight_smile:
The issue is not that I access wrong instance of a node.
The issue is with the accessed position of a node - but relative to it’s parent.
I close the quesiton and thanks @johnygames for being my… toy duck (is that a correct word for it in English :-))?