Hello! I’m making a top-down space shooter, and I’m trying to make the enemy killing function. Whenever the player shoots, a Bullet scene spawns in game, which on its own scene has the group “bullet”, but when the enemy (which also spawns progressively, not at the start of the game) gets shot, the game doesn’t detect if the body that has entered the collision box is inside the “bullet” class.
The thing is that if I try to detect the “player” group, it works as intended (although with errors), and I think that it is because the player is spawned in when the main scene is ready and the other game component aren’t.
If in need, I can provide any part necessary to solve the issue.
Thank you very much in advance!
need to see how you call the group, how player is spawned , and what’s the error?
I have 3 main groups on the Main scene, called “player”, “bullet”, and “enemy”. The player is a child of the Main scene, and is a scene itself, so it can be inside of the main “player” group. Then, I have a scene for the bullets, that spawns inside of a node called “bullet manager”. Inside of its own scene, the bullet is inside of a group called “bullet” (exactly the same as the one on the main scene). The exact same thing is done for the enemy scene.
I don’t understand what you mean by main group. Is that a node where you put all your instanced scene in?
To use groups you would open the scene, click the top node (group will count for every child then) and in the inspector assign a group to it.
Now every scene you instantiate of that scene is in that group.
This is the structure of that “main” node. The player starts on screen, along with a BulletManager and an EnemyManager. What you say is to get the manager nodes inside of the groups, so they can inherit their group to the child node? Because I’ve tried it but it didn’t work.
I have also tried to give the player the “player” group, and then whenever the player is in contact with the enemy hitbox print the array of groups that the player node is in, but it only prints a blank array (“”).
You’re right. Children don’t inherit the group. I thought wrong.
Can you show some code how the enemy hitbox works? Is it an area2d?
The code that I’ve made is really simple and only meant for prototyping, so it doesn’t have much to do.
Here is the enemy code, with an example function inside of the event function
The enemy scene (can’t upload it on a single post) consists of a CharacterBody2D as the parent, then a CollisionShape for the solid body, a sprite (unanimated), then an Area2D as the hitbox that will then be used for the killing logic (with another CollisionShape inside). The Area2D has already the body_entered event function inside.
this, im pretty sure you connected the Area2d’s body_entered(body) signal from editor, right?
print(body) first on the
func _on_enemy_hitbox_body_entered(body): when you shoot the enemy, see if the bullet is too quick to be calculated into the body (if there’s no body detected when you shot the enemy, then the bullet is too quick)
In the on_body_entered event you will get the body and not its collision shape.
You don’t have to use get_parent.
If you just want to check if an object is in one group there is also a function “is_in_group()”. Just in case you missed this.