0 votes

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)
->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")      

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?

in Engine by (72 points)
edited by

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

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.

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.