Last created PackedScene doesn't handle input

Godot Version

4.1.2

Question

I have a packed scene consisting of:

 Cluster: Node2D
 | - clickArea: Area2D
   | - col: CollisionPolygon2D

clickArea has input_pickable set to true

I also have a simple PackedScene called “Soldier” with a RigidBody2D.

I am adding the “Soldiers” into the Cluster and then I calculate the new CollisionPolygon so that I can handle clicking anywhere inside of it to select the cluster of Soldiers.

func _process(_delta):
	var points = PackedVector2Array()
	var children = get_children()
	for child in children:
		if child is Soldier:
			points.append_array(child.getCornerPoints())
	var hull = Hull.new() # quick hull algorithm
	hull.calc(points)
	if hull.hull.size() > 3:
		$clickArea/col.set_polygon(hull.hull)

Here is the code that creates the Cluster and adds Soldiers to it:

var cluster = pc_soldierCluster.instantiate() as Cluster
add_child(cluster)
for i in range(0, soldiersToSpawn):
	await get_tree().create_timer(randf()*0.2).timeout
	var soldier = pc_packedSoldier.instantiate() as Soldier
	cluster.add_soldier(soldier)
	soldiersToSpawn-=1

cluster’s add_cluster:

func add_soldier(s:Soldier):
	s.target = self.target.position
	add_child(s)
	soldierCount+=1

and I have clickArea’s input_event connected to the Cluster’s

func _on_click_area_input_event(viewport, event, shape_idx):
	print("event")

The weird thing is, the latest created area’s input won’t work.

I create 3 Cluster objects and 2 of them work as intended printing event but the latest created one won’t. What is happening here?

Yes, I tried showing collisions and everything works as intended there.

image

Okay, the problem was that the set_polygon function was inside the _process instead of _physics_process function which caused this weird behaviour

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.