I decided to make/clone minesweeper. As I figured out I should manipulate input somehow. I guess it should be signals, but I have no idea how to connect them since I can't use godot interface to do so, because cells is an instanciated objects by add_child() method.

Correct me if I wrong: I declare a signal on cell scene's script, so every instantiated scene node(of the cell) should have it's own signal?(i.e. when I click individual cell in a gridcontainer, where cell added as a child, it should affect only that instance of the cell). But how exactly should I connect them to main script where all calculations happen?

If I should use this:

    func _at_some_func():
        instance.connect("your_signal_name", self, "_callback_no_args")

How do I connect instance if I don't know its exact name. And there's a lot instances to sort them manually

How project generally looks: Main scene: Node2D - GgridCOntainer

main.gd
extends Node2D

variables

func _ready():
	matrix()

func matrix():
	while true:
		var cell = Cell.instance()
		$GridContainer.add_child(cell)

And Cell scene: Container

Cell.gd
extends Container

signal clicked

variables

func _input():
	if cell_state == HIDDEN and event.is_action_pressed("click"):
	cell_state[0] = OPEN
		emit_signal("clicked")

Or maybe there other way(s) to solve this problem wich I don't see?

Check the documentation. Here is what you are looking for. So i think this should look like:

main.gd
extends Node2D
variables
func _ready():
	matrix()
func matrix():
	while true:
	var cell = Cell.instance()
	cell.connect("clicked", self, "_callback_no_args")
	$GridContainer.add_child(cell)

I think the easiest way would be to give the signal parameters. Either the cell by itself or an id to identify the cell. The link i put at the top shows you how to use signals, how to connect them, how to connect them via code, how to emit signals and how to write custom signals with and without parameters. That should help you out. If you need help, just ask :)

@Schorsch O, it's so obvious now, I think I finaly comprehend signals, thanks:). Though code does not working as wanted. When I make binded input signal trigered on all instances. Can I bind is_action_pressed() to trigger only when I chose a concrete cell in gridcontainer? Using Viewport or something? If not this code seems useless in this case:(

Cell.gd
func _input(event):
	if event.is_action_pressed("r_click"):
		emit_signal("clicked", container)
#this trigger all instances, because it happend whenever r_click pressed 

Then I finally(the way was severe) stumble upon buttons. The Problem is how I can bind other mouse keys to it the same button? And can I even do this? I tried to add right button shortcut, so I created shortcut resource(change button index to 2 - right mouse button) and add resource to button, but it dosen't work for some reson:(

Maybe you are looking for godots input actions? In godot, navigate to: Project > Project Settings > Input Map. Take an action and put your keys and buttons in it. You don't have to use the actions which are present there. You can also make own actions and put your keys and buttons in there. Maybe just make an "own_mouse_action" or whatever you like and put your mouse buttons in there. Or do i get you wrong?

For your first question. You would have to bind different methods to your different cells. But i think that would be a bad solution. Maybe just put a parameter to your signal and if you trigger the signal, put the event driver as parameter. Take a look here for this.

@Schorsch Signals now alright, I can handle them. Buttons. Oh, no, I talked about button node as a control element, my bad. 1) As I can see there is no way to bind another/additional key to button_node, right?

My main problem now(I kinda solved it) is that every cell instance react when input key is pressed.(i.e. all cells get flagged) What I need is that when input happens, only specific cell(wich was clicked) was affected. But insted, no matter wehre you click, the click itself triggers changes on all cell instances. I end up with this solution:(I check if cursor is in instanced cell's boundaries when input key is pressed, 2) but how it affect only cell wich was clicked?)

cell.gd
....
func _input(event):
	var pos_x_1 = position.x - 15 #cell's boundaries based on container(cell) global_position (rect2)
	var pos_x_2 = position.x + 15
	var pos_y_1 = position.y - 15
	var pos_y_2 = position.y + 15
	mouse_pos = get_global_mouse_position() 
	if mouse_pos.x in range(pos_x_1, pos_x_2) and mouse_pos.y in range(pos_y_1, pos_y_2) and event.is_action_pressed("r_click"):
		change_state()
		put_flag()

it works, but I don't really understand why. I tried random ideas and this appear to work somehow.

there's just my whining, don't bother to click

! It's frustrating - to know not why. Now's a 2nd week I'm struggling with this project and programing in general. I never programmed a thing in my life until one week back, it's really give me hard time.

You are new to programming and you must learn to program in general and you must learn Godot. It is hard to learn two tasks at once. What part is it that you don't understand with your solution? It looks like all cells are getting an input event on click, right? And there, you test if the mouse cursor is over (inside) your cell. If this is true, the cell was clicked. Otherwise it was not. What you are actually doing ist what Godot makes in the background. So it would be better to get another solution. You can keep this like it is if you want. I just want to explain my solution. You have some kind of container which holds your cells. A Cell is somehow clickable. Maybe a cell just contains a button, and if this button is pressed, the cell emits a signal. This signal has one parameter. If the cell emits it, it sets "self" as parameter. The container for all the cells acts as a listener for the events of all these cells. So you give the container a script with the function for the events. If an event is emitted, so a cell is clicked, you can take the parameter of to get the clicked cell. The function maybe looks like this:

func on_cell_clicked(cell):
    do_whatever_you_want_with(cell)

How the cells emits a signal depents on how your cell is build. If it is just a button, you are done. Buttons emit signals for being pressed by themselves. If you cell is more like a button, but a script at the root of your cell-node-tree. If the button inside the cell is clicked, let this script know and let it emit it's signal afterwards. I hope you got my point :)

Keep going

I had already done it as you described, it works, thanks:) And I finaly found what is it I talked about - custom gui. Now almost all functionality complete.

4 years later