1
\$\begingroup\$

I am making a sidescrolling shooter and I am working on the shooting mechanism. I have two scenes, one contains a KinematicBody2D with a Sprite, position2D and a collisionshape2D and a second KinematicBody2D with a CollisionShape for testing bullet collision. The scene also has a Tilemap called Level. In the second scene I have an Area2D with sprite and collisionshape as my bullet.

Code for the bullet (Only important parts):

extends Area2D

onready var sprite = get_node("/root/Game/Player/Sprite")

func _on_VisibilityNotifier2D_screen_exited():
    print("Freed")
    queue_free()


func _on_Bullet_body_entered(body):
    print("Entered: ", body.name)

Player shoot() function:

func shoot():
    var bullet = Bullet.instance()
    get_node("/root/Game").add_child(bullet)
    bullet.global_position = $Position2D.global_position

Upon starting the game this is printed out, without the player having shot:

Entered: Player
Entered: Level
Freed

When shooting into the second KinematicBody2D, nothing happens at all, the bullet only gets freed after exiting the screen.

What am I doing wrong?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You say that:

Upon starting the game this is printed out, without the player having shot:

Entered: Player
Entered: Level
Freed

This suggest that you have a an instance of the Area2D in the scene tree before hand. What you want is to have the bullet and whatever it needs as a separate scene that you instance.

Which is what I expect the line Bullet.instance() does. To be clear, here Bullet must refer to a PackedScene loaded from a separate scene file.


And you say that:

When shooting into the second KinematicBody2D, nothing happens at all, the bullet only gets freed after exiting the screen.

This suggest that the "body_entered" signal is not connected on the Bullet scene (on the separate file, that you loaded into Bullet the signal is not connected, probably). I'm guessing you only connected it on the instance you have on your scene tree before hand, which explains why that one prints Entered: Player and Entered: Level.


I might be guessing wrong. But, in general terms: You want a separate scene file for the bullet, that you can load and instance on demand (I'm guessing you already have it). Make sure that separate scene file has all that it needs to work, so that they work regardless or where or when you instance it. You may take advantage of Autoloads - which are available everywhere all the time, unless you remove them explicitly - if you need to get something from outside. Also make sure you don't have a bullet instanced in the scene before hand.

\$\endgroup\$
3
  • \$\begingroup\$ Ok, now it doesn't fire at the start, but in the Node tab I cannot connect the body entered signal to the nodes in the main scene \$\endgroup\$
    – pion
    Jan 7, 2022 at 9:48
  • \$\begingroup\$ @pion Right, you cannot use the editor to connect signals from one scene to another. That was true before, and it remains true now. So, how is this connection you are trying to make exactly? I was convinced you connected from the Area2D to itself, which should be no problem. Edit: If you really want a connection from one scene to another you will have to do it form code. In that case, I suggest connect where you instance the scene. Using signals on an Autoload also works. You can - but I suggest against - reach out of the scene if you know the path to the node you want, which is fragile. \$\endgroup\$
    – Theraot
    Jan 7, 2022 at 9:55
  • \$\begingroup\$ Oh, I didnt connect it to itself this time! Thanks a lot, now it works just fine \$\endgroup\$
    – pion
    Jan 7, 2022 at 10:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .