0 votes

Hey, I have a simple problem, but I am not able to find a solution with the little knowledge about Godot, that I have so far:

I want to create a simple ball-and-paddle game like "DX-Ball" just to get used to Godot. That means, I have a Ball and several Bricks in my Scene. If the Ball hits the Brick it should be destroyed and and Ball should change the direction of its movement depending on where exactly it hit a Brick.

The problem is now:
I want to let the game recognizing a hit via "area-shape-entered". if the Ball enters any area shape of the brick, the Brick should be destroyed (I guess via "queue_free()") and the Ball should change its movement direction.
But if I have more than one Brick and connect the "area-shape-entered" signal of the Ball to every Brick, than of course the Signal is emitted to every Brick as soon as the Ball enters one of the Bricks areas and all Bricks are destroyed().
If I do it the other way round and connect the "area-shape-entered" signal from the Brick to the Ball, I don't know how to tell the Brick that it should be destroyed, because the function is in the Script that belongs to the Ball. I tried to emit then another signal out to the Bricks, but in the end I again run into the problem that alle Bricks vanish as soon as the first Brick-area was entered.

So how can I connect the single Bricks to the Ball and apply "queue_free()" only to the Brick-Area that was entered?

Godot version 3.3.4
in Engine by (14 points)
edited by

1 Answer

0 votes

How about making each brick an instance of some base Brick class, then having a function that checks for a collision (e.g. _on_brick_area_entered()), checks the name of the object which collided, and uses queue_free() when it's the ball? Either that, or have the ball check for collisions in a special Area2D collision layer. When it has those collisions, the ball accesses the object that it collided with (using the ball's _on_area_entered() signal), and calls the queue_free() function on that object.

by (2,394 points)

That is more or less what I did. The problem is, that I have to connect onarea_entered (and similar functions) from the Ball to the Brick or vice versa. Therefore there is a connection between the Ball and every Brick and once the Ball enters one of the Bricks, the Signal is emitted to ALL of the Bricks and ALL of them get "queued free".
Maybe I have a barrier in my mind, but I do no find a solution were I am able to track a potential collision for alle of the Bricks, but do the action only on the one were the collision took place.

I have to connect on_area_entered (and similar functions) from the Ball to the Brick or vice versa.

Is it necessary to connect that signal to all of the bricks? Could something like the following code be used for the ball?

# In the ball's script.
  _on_area_entered(body):
     # Try either the name of the brick, or even identify it by its type, i.e. the brick is an instance of a custom class, "Brick".
     if body.get_name() == "Brick":
          body.queue_free()
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 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 webmaster@godotengine.org with your username.