Search code examples
collision-detectiongodotgdscriptgodot3

`.is_in_group` method not returning `true` despite being used on an object inside the specified group


I'm making a roguelike game in there is a large, procedurally generated level. There are loads of enemies in the level, and in this case they will only be aggravated and start attacking once you get within a certain range of them. So to do this, I create a CollisionShape2D in the main scene. I name it 'AggravationRange', and I put it in the group 'AggrivationRangeGroup'. I then also add a Sprite2D as a child, so that I can see how large the parent CollisionShape2D is whilst inside the game. Then, right after I add the player to the main scene, I create a variable referencing the player node, then making it so that AggravationRange is always at the same position as the player. I do this instead of simply adding AggravationRange as a child to the player, as it prevents the rotation being changed also. Next, I open the script for the enemy and add an _on_area_entered() signal. The code I use for it:

var PlayerDetected
_on_Enemy_area_entered(area):
   if area.is_in_group('AggravationRangeGroup'):
      PlayerDetected = true
      print('The enemy has been aggravated')

So then I load the game, and nothing ever prints to the console, despite the enemy is clearly within the CollisionShape2D.

I've placed another print() statement inside the _on_area_entered() signal, such that it would print whenever any area collides with it, and it works, so there's no problem with it detecting collisions, but just checking if the area is in a specified group seems the problem. I know it can't be it detecting a different CollisionShape2D, as there are only 2 CollisionShape2D's, the AggravationRange and the enemies hitbox. So why doesn't it detect the area being in the group? It's spelt correctly, monitorable is set to true, and I can't find any issues with the code.


Solution

  • Fixed by checking area.name instead of using area.is_in_group().