Keep node not seeing group of colliding object

Godot Version

godot-4

Question

Hi everyone,

In my game I have a node of resources going from a town hall to a keep. everything is working as expected until the resource node collides with the keep node.

The resource node is being added to a group called “resource_bundles_to_keep” and when the keep checks if the colliding object is in that group, it says the colliding object has no group. But at the same time i can print the list of object in that group and the resource node is in the group.

Any idea why this is happening?

Keep script:

extends Area2D
var resourceStone : int = 0
var resourceWood : int = 0
var resourceFood : int = 0

func _ready():
	add_to_group("keeps")
	connect("area_entered", self._on_area_entered)

# Signal handler for when the bundle collides with the Keep
func _on_area_entered(object):
	print("Area entered: ", object.name)  # Debug print to check which area is entered
	#Print the type of the area object
	print("Object type: ", typeof(object))
	# Print all the groups the area is part of
	print("Groups: ", object.get_groups())
	print("Incoming: ", get_tree().get_nodes_in_group("resource_bundles_to_keep"))
	
	if object.is_in_group("resource_bundles_to_keep"):
		print("Resource type: ", object.resourceType)
		increase_resources(object)
	else:
		print("not it")

func increase_resources(resource_bundle):
	print("Keep hit with: ", resource_bundle.resourceType)
	# Check the type of resource received and increment the corresponding variable
	match resource_bundle.resourceType:
		"Stone":
			resourceStone += resource_bundle.myAmount
			print("Stone: ", resourceStone)
		"Wood":
			resourceWood += resource_bundle.myAmount
			print("Wood: ", resourceWood)
		"Food":
			resourceFood += resource_bundle.myAmount
			print("Food: ", resourceFood)
		_:
			print("Unknown resource type:", resource_bundle.resourceType)
	resource_bundle.queue_free()  # Remove the resource bundle after collecting

Resource script:

extends CharacterBody2D

var speed : float = 100.0
var target_keep: Node2D = null
var myAmount : int = 0
# Variable to store the resource type of the bundle
var resource_bundles_to_keep: String = ""

# Called when the node enters the scene tree for the first time.
func _ready():
	add_to_group("resource_bundles_to_keep")
	find_keep()
	# Debug print to confirm addition to group
	print("Current groups: ", get_groups())  # Print the current groups of this node

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	if target_keep:
		move_towards_target(delta)
		#print("Current groups in process: ", get_groups())  # Periodic group check

func find_keep():
	var keeps = get_tree().get_nodes_in_group("keeps")
	if keeps.size() > 0:
		target_keep = keeps[0]

func move_towards_target(delta):
	var direction = (target_keep.global_position - self.global_position).normalized()
	velocity = direction * speed
	move_and_slide()

# Function to update the resource image based on the assigned resource type
func update_resource_image(resourceType):
	var image_path = "res://Assets/" + resourceType + ".png"
	var texture = load(image_path)
	$Sprite2D.texture = texture
	print(resourceType, " Added to group ", get_groups())

Here is what is printed:
Current groups: [&“resource_bundles_to_keep”]
Wood Added to group [&“resource_bundles_to_keep”]
Area entered: Area2D
Object type: 24
Groups:
Incoming: [resource_bundle_to_keep:<CharacterBody2D#38386271776>, @CharacterBody2D@2:<CharacterBody2D#38537266401>]
not it

As you can see the Groups line is blank meaning it see no group.

Any help is appreciated.

Ok I figured out the issue, but it raised a new issue.

The original issue was the area2d that was checking the collision is a child of a character2d (for ease of movement). So while the character2d was in the group the area2d was not. When I added the area2d to the group then the keep saw the group correctly.

However, now I need to figure out how to reference the parent node for all the scripting info (like the variables to pass and such). Any help on that is appreciated.

Look at me figuring stuffs out.

I didn’t see this info spelled out anywhere, so I’m adding here for posterity.

The solution was to create a new var and put the get_parent() AFTER the object name. I’m sure that may common sense to some, but was not for me. =)
var parent_object = object.get_parent()