Queue_free() problems with a node

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

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

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

Schweini | 2019-08-29 16:49

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

Pacers | 2019-08-29 18:26

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

Schweini | 2019-08-29 18:33

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

Pacers | 2019-08-30 23:25

func delete_ball() : global. pos = position queue_free()
Something like this. global is a singleton: Singletons (AutoLoad) — Godot Engine (3.1) documentation in English

Schweini | 2019-08-31 04:32

:bust_in_silhouette: Reply From: Vrzasq

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

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

Pacers | 2019-08-30 23:23

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 ?

Vrzasq | 2019-09-02 11:41