0 votes

Currently I am making a pong game and I'm having problems deleting the ball when it exits the screen.

CPU Paddle

func _physics_process(delta):
    screensize = get_viewport_rect()
if (!BALL.position.y > screensize.end.y):
    velocity = (BALL.position - position).normalized() * speed
    var new = Vector2(velocity.x,0)
    velocity = 180
    var old = Vector2(velocity,0)

Ball scene

func _physics_process(delta):
    var bodies = get_colliding_bodies()
    for body in bodies:
        if body.get_name() == "Player":
            speed = linear_velocity.length()
            direction = position - body.get_node("Anchor").get_global_position()
            velocity = direction * min(speed+SPEEDUP*delta, MAXSPEED*delta)
            linear_velocity = velocity

func ball_delete():
    pos = position

The initial if statement doesn't seem to work as the velocity always returns a previously freed instance error. I know that the CPU position depends on the position of the ball so if I delete the ball there is a problem with the velocity of the CPU paddle and I was wondering if there is anyway I can delete the ball when it exits the screen

in Engine by (20 points)

Whrn you delete the ball, you wanna knoe its positiob afterwards? Try to detect the pos before

I would like to know the position of the ball before it exits the screen so the CPU paddle can track its position.

Yeah. But you can't detect the position when there's no object cuz you deleted it

So once I delete the ball I should spawn a new ball and somehow reset the position of the CPU paddle, but how should I approach this

func delete_ball() : global. pos = position queue_free()
Something like this. global is a singleton: https://docs.godotengine.org/en/3.1/getting_started/step_by_step/singletons_autoload.html

1 Answer

+1 vote
Best answer

If I am understanding correctly You would like to delete ball if it exit game area but when You do that error happen. If that is the case You can do couple things.
1. Test if ball variable is not null:

If ball:
Do something
  1. Add node2D with default position for cpu paddle in case ball is not present in the scene
  2. Spawn new ball before checking its position inside cpu paddle

Hope this is what are You looking for

by (183 points)
selected by

So far I was able to store the position of the CPU paddle but when I try to spawn a ball I get a contact monitor error, when the error was resolved by enabling contact monitoring on the ball, the physics of the ball doesn't seem to work.

Main scene

var BALL_SCENE = preload("res://Ball.tscn")
var initial_cpu

func new_game():
    initial_cpu = $CPU.position
    var ball = BALL_SCENE.instance()

    if $Ball.position.y > $CPU.position.y:
        ball.position = Vector2(300,500)

Is there another way I could spawn the ball

Your code should spawn a ball inside scene and if Ball_SCENE has all required references it should work. If Your ball doesn't work then the issue is probably somewhere else.

From the snippet above it is hard to deduct. Could You paste ball physic code or link to repo ?

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 Frequently asked questions and 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 [email protected] with your username.