0 votes

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 Player.gd 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.

in Engine by (68 points)
edited by

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.

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.

1 Answer

0 votes
Best answer

OK I can answer my own question after working on the minimal example. :-)
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 :-))?

by (68 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.