Godot 4.1.1
I’m not quite a complete novice, but I am self-taught, which is always dangerous. I’ve read as many forum postings on this as I can find, but as far as I can make out, I can’t adapt these to tackle my issues around ‘pressed’ events or signalled variables.
I want to instantiate a series of StaticBody3D nodes and connect a signal from each of them to an auto-loaded global script so that they are mouse-selectable. Crucially, when selected they need to emit a signal of their position and a string for their ‘name’.
I’ve looked at the docs. The “Connecting a signal via code” section has a timer signal as an example, adapting this code slightly makes it work for an instantiated timer. Off to a good start then:
extends Node3D
var timer_node = preload(“res://timer.tscn”)
func _ready():
var timer = timer_node.instantiate()
add_child(timer)
timer.timeout.connect(_on_timer_timeout)
func _on_timer_timeout():
print(“timed_out”)
My own signalling works fine when the nodes are already in the scene tree, and the signal connections are made from the front-end interface, which auto-generates code on the target node’s script which can then be expanded on:
func _on_item_input_event(_camera, event, position, _normal, _shape_idx, extra_arg_0, extra_arg_1) → void:
if event is InputEventMouseButton:
if event.button_index == MOUSE_BUTTON_LEFT and event.pressed == true:
var item_position = Vector3i(round(position.x), round(position.y), round(position.z))
var item_name = str(_extra_arg_0 + " " + extra_arg_1)
do_stuff(item_name, item_position)
But I absolutely have to be able to do this same signalling from instantiated nodes, the alternative is creating hundreds of nodes in the scene tree and giving each one a specific and unique name - with no typos! So the code below is as close as I’ve got, it doesn’t crash, but it doesn’t print my test message either:
extends Node3D
var item_node = preload(“res://item.tscn”)
func create_items() → void:
var item_count: = 0
var item_position: = Vector3i(some vector)
var item_name: = “some name”
while item_count < (some number):
var item = item_node.instantiate()
add_child(item)
item.position = item_position
item.input_event.connect(on_create_items_item_selected, 0)
item_count: += 1
item_position: = Vector3i(some new vector)
item_name: = “some new name”
func on_create_items_item_selected():
print(“it got this far”)
Where, oh where, does ‘MOUSE_BUTTON_LEFT’ go?
More broadly, the .input_event.connect only accepts two parameters (the last of which is an integer whose purpose is beyond me). The front-end auto-generated code has (amongst other things) an event, a position, and as many extra string arguments as you care to define. Even if it did get my code to print that “it got this far”, how will the instantiated nodes ever be able to signal their position and name?
Any help would be greatly, greatly appreciated.