0 votes

how to add event for dynamic object?

func _ready():
    var csg = CSGBox.new()
    ... ?
    add_child(csg)

func _on_Area_input_event(camera, event, position, normal, shape_idx):
in Engine by (37 points)

Thanks
But if there are many objects, how to find out which one was clicked ?

var spheres = [
    {"x": 1,"y": 1,"z": 1},
    {"x":-1,"y": 1,"z": 1},
    {"x": 1,"y":-1,"z": 1},
]

func _ready():
    for s in spheres:
        addCSG(s)

func addCSG(s):
    var cs = CollisionShape.new()
    cs.shape = SphereShape.new()

    var a = Area.new()
    a.add_child(cs)
    a.connect("input_event", self, "_on_Area_input_event")

    var csg = CSGSphere.new()
    csg.transform.origin = Vector3(s.x, s.y, s.z)
    csg.add_child(a)

    add_child(csg)

func _on_Area_input_event(camera, event, position, normal, shape_idx):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed == true:
            print(position, shape_idx)
  • connect doesn't pass custom parameters
  • emit_signal - how to bind to onclick ?

1 Answer

+1 vote
Best answer

When connecting in code, you can pass additional parameters. In this case, you can pass the object itself:

func addCSG(s):
    ...
    a.connect("input_event", self, "_on_Area_input_event", [a])

func _on_Area_input_event(camera, event, position, normal, shape_idx, area):
    # Now you have access to area as well
by (286 points)
selected by
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.