Tracking the dynamic position of a specific scene instance - Pong AI

Godot Version

4.2.2

Question

I’ve been somewhat “stuck” with my Pong clone lately, as even though the PvP works just as expected I haven’t been able to implement a vs AI functionality as I had hoped. I’ve tried to build the whole project without a step-by-step tutorial.

It’s not like the concept is that difficult - I need the Y-position of the Pong ball, and I have to move the paddle towards the Y-position. But I have not been able to figure out how to reliably track the dynamic Y-position and pass it to the paddle.

My clone is set up in a way that once a timer times out, a spawner node creates an instance of the ball as a child, and when the ball hits a goal, the instance is freed, and the timer is restarted.

If the ball was just an another node in the scene, I could create a variable that is updated inside the Process() function. But at the start of the game, the ball does not exist, and is only instantiated and added as a child once the timer times out. So how can I grab the live Y-position of that specific instance that was just added as a child? If I try to declare a variable at the top of the script, and I set that to the position of the instance after creating it, I only get a single position as the timeout script only executes once. But I also can’t do this through the Process function, as that instance doesn’t exist yet so I get an error.

I hope I explained the problem well enough! I’m not saying that this is the best way to design an AI like this, but it still bothers me that I can’t figure out how to do it this way.

To give a recap: I want to track the dynamic, real time Y-position of a moving node that does not exist on startup, but is only instantiated once a timer runs out. I know how to get the static position of the instance on creation, and I know how to get the dynamic position of an existing node, but I couldn’t figure out how to access this information in real time after the node has been instantiated.

Thank you!

EDIT: To clarify, there is a script attached to the ball that gives it collision and makes it bounce off of walls etc. And there is a script attached to the ball spawner that instantiates the ball and adds it as a child to itself.

I have created a dynamic pong ai, but its gdscript

AI VS AI
Here the speed of ai paddle is random, sometimes slow and fast

Leaving this comment here - I managed to solve this myself. I couldn’t get the child node (the ball) at the start of the script, as it doesn’t exist on startup. So, I put the GetNode function in the same timeout signal as the ball instantiating piece of code. This way, the moment the ball is created, the ball spawner node gets a reference to it. Then, I put an if statement in the process function that checks if IsInstanceValid(ball) is true, and if it is, it stores it’s live position in a variable. This variable is then passed on to the paddle that can track it.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.