I’m doing a Head soccer clone as my third game. I’ve made the movement, the ball physics and all and i was just missing the goals. So i made it so that when the ball enters the net, it destroys itself and reapears by using instanciate. it worked just fine for the first 2 goals, but as soon as it reapers for the third time it just stops working, the ball goes over the post’s sprite (it didn’t happen before) and it does not dettect if you score again, can anyone help?
var ball = preload("res://Escenas/Pelota.tscn")
func reset():
var ball_instance = ball.instantiate()
ball_instance.position = Vector2(584, 511)
get_parent().call_deferred("add_child", ball_instance)
that is my function for reseting the ball. This code is in an autoloaded script which i use to store global variables
I can’t really tell why it’s happening, but you could try just keeping a reference to the ball and reset it’s position, instead of removing it and creating a new one.
var ball = preload("res://Escenas/Pelota.tscn")
var ball_instance = ball.instantiate()
func reset():
ball_instance.position = Vector2(584, 511)
func _on_arco_izq_body_entered(body):
if body.name == "Pelota":
Globales.puntos2 += 1
print(Globales.puntos2)
Globales.reset()
I am curious, Globares is autoloaded and the line get_parent().call_deferred("add_child", ball_instance)
is setting new ball instance as a child of root, am I right?
Put it aside, maybe the cluprit is checking by node name. Could you see remoto tab while running the scene, and check node names of new balls?
Looking at the remote tab, i realized that the ball was getting added as a child of Root, which was the parent of my autoloaded script. I had no idea that an autoloaded script was a node of the root because i never checked the remote tab. So i managed to make the ball instance a child of the main Node2d by making a new script and assigning it to it. I also realized that my instance was not called “Pelota” like the original scene, so i changed the script in the goals to detect if a rigidbody entered, instead of checking the name.
You’ve saved me once again, thank you