Godot Version
4.3
Question
I’m trying to make a node (flyingblob) move across the screen, and when it is clicked, it disappears and shows a tooltip. But how can I access the position of an instantiated node from a separate function?
4.3
I’m trying to make a node (flyingblob) move across the screen, and when it is clicked, it disappears and shows a tooltip. But how can I access the position of an instantiated node from a separate function?
you need to save the node in a class-variable:
var flyingblob
func _on_flying_blob_timer_timeout():
...
flyingblob = flyingblob_scene.instantiate()
Then you can access the node with the name “flyingblob”
I’ve updated the code like so and while the game now registers a position for the flyingblob scene, it sets it to 300, 64.
If you go to your flyingblob_scene and check the root Node - it most likely has a x=300, y=64 position defined. You can reset it to 0, 0.
By the way, you don’t need to assign the position.x
and position.y
separately, you can apply the whole position
at once.
$ClicksGainedLabel.position = flyingblobpos.position
Thanks for the tip, but the goal is to have the ClicksGainedLabel appear where the flyingblob is, and now the text just appears at 0,0.
Where is your piece of code that makes the label appear? I see that you’re using some kind of signal bus with SignalBus.flyingblob_clicked
signal. Show the code that reacts to this signal
This part of the script is inside the Button scene
This part of the script is also inside the button scene
This is the script that spawns and moves Flyingblob and is in the flyingblob scene
I can’t see anywhere the code that would connect to the SignalBus.flyingblob_clicked
signal. Do you use this signal at all?
You said that the label should appear after you click the blob. I don’t see that anywhere in your code.
Why I’m asking about it - is because you should be changing the position of the Label to the position of the blob only at the time when the blob is clicked. Doesn’t make sense to set it any earlier.
It is connected like this
And right now, the label is always there I’m just trying to get it into the right position first.
Ok, I think I see what’s the issue.
Your code is kinda confusing with the function naming. E.g. flyingblob()
function I would rename to create_new_flyingblob()
. This is then a little more obvious that you are trying to assign the label’s position to the newly created blob instance (instead of the old one that was clicked), which will be 0,0 as you’ve discovered.
You should put this part of the code
$ClicksGainedLabel.position = flyingblobpos.position
In your _on_flyingblob_pressed()
function, before you queue_free()
your old blob. As this script is already on your flyingblob, you don’t need a reference to it, you can just use position
to access its position. Just make sure your $ClicksGainedLabel
is properly referenced, because these might be in a different place in the scene, I don’t know your scene structure.
$ClicksGainedLabel.position = position
Yeah sorry here is the scene structure and you can refer to above posts for how scripts have been attached
Did you try my suggestion?
Try to do this instead:
$"../ClicksGainedLabel".position = position
thanks! it works great now!