How to check if a Collided Object is part of a group when it hits a static body 2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AnAverageProgrammer

So I have a bullet in my space invaders game, and I also have a barrier of a StaticBody2D. I want the bullet when it hits the barrier, to check if the barrier is part of the group Enemy, then disappear, and not break the barrier. I’m getting some weird errors. My most recent error is an Invalid call. Nonexistent function ‘is_in_group’ in base ‘KinematicCollision2D’.

Here is my code for the bullet script. Hope it helps :slight_smile:

extends KinematicBody2D

var speed = 500
var level = 1

func _physics_process(delta):
var collidedObject = move_and_collide(Vector2(0, -speed * delta))

check if object is in enemy group

if collidedObject:
	if collidedObject.is_in_group("Enemy"):
		#if collidedObject is Enemies
		collidedObject.get_collider().queue_free() #removes what it collided with
		queue_free() # removes itself
	else: 
		queue_free()
		# if enemy group is size 0 then advance level variable by 1
var numEnemies = get_tree().get_nodes_in_group("Enemy").size()
if numEnemies <= 1: 

warning-ignore:return_value_discarded

		get_tree().change_scene("Lvl_" + level + ".tscn")
	# check if object is in enemy group
:bust_in_silhouette: Reply From: umma

the problem is the variable “colliided objects”. Collided objects is not a node so if you ask for its ‘‘group’’ it’s going to return an error, try replacing its value. ie.

var collided objects = get_node(that)

So just create a new variable called CollidedObjects and it should work?

AnAverageProgrammer | 2022-01-28 01:47

just try this instead, also are youfamiliar with godot or are you new. if the code is showing error make sure there are no space between codes.

extends RigidBody2D

var active = true
var speed = 2000
var level = 1

func _ready():
  set_as_toplevel(true)


 #change bullet main node to rigidbody imstead of kinematicbody
func _process(delta):
if active == true:
	linear_velocity.y = -speed
	print("shoul b  moving")

 #add an area node as a child of bullet. then go to inspector, look to the 
 #right you will see a menu which says "nodes", click signals then click the signal which says
#"on_area_body_entered and connect to your bullet script. unti you see a exit like
 #icon beside the function below.

func on_area_body_entered(body):
if body.is_in_group("enemy"):
	queue_free()
	body.queue_free()
	print("bullet is working")

umma | 2022-01-28 03:19

let me know if you are still having difficulties :slight_smile:

umma | 2022-01-28 03:26