Attach an area2d script programmatically - making a hotspot

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

Hi, I am making a point and click game and I need some directions on making an area2d as a hotspot:

The area2d configuration is as follows:

Area2D (ObjTemplate) (no script)
->Sprite
->CollisionShape2D (no collision set)

At this time in my program, I can get a JSON file with various x,y, hotspot-x, hotspot-y and images and setup custom made area2d based on a preloaded template of above file.

What I am trying to do is attach a script that has an _input() function and try to use this as a hotspot click:

for object in objects:
    var tmp = ObjTemplate.instance()
    tmp.get_node("sprite").texture = object.image
    tmp.position(object.x, object.y)
    var shape = RectangleShape2D.new()
    tmp.get_node("CollisionShape2D").shape = shape
    shape.extents = Vector2(object.hs_x / 2, object.hs_y / 2)
    tmp.get_node(".").set_script = load("res://game_assets/"+object.script+".gd")      
    add_node(tmp)

Assume object.script is Lobby, so in Lobby.gd I have the following:

extends Area2D

var count = 0

func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton:
		if event.pressed:
			count += 1
			print("Click:" + str(count))

I don’t get any errors during runtime, but I don’t get any _input interactions either. If I attach the script directly to ObJTemplate then it will work.
So, does it need anything to “refresh” or enable the contents of Lobby.gd somehow to be parsed and interpreted at runtime?

Is it possible the event never makes it to the object you’ve attached the _input() script to? This is a helpful chart from the docs that describes how they are passed around:
enter image description here
From the InputEvent section

DDoop | 2020-06-27 21:26

Originally, my thought was to have this dynamically linked to a signal event or use call_group.

But I need to setup the clicking somehow programmatically and individually for each created area2d. If I activate the visible collision shapes, I can see the collision area in area2d, I just need to define the click dynamically so that the next part, the callback of separate functions will occur.

Panagiotis Halatsako | 2020-06-28 07:37