So, I have a set up where I use signals to connect the mouse click to "select" (I am a pragmatist not super strong in my programming, sorry if this is the worst way to do what I am trying). I have succeeded in "clicking" the objects and have the selector sprite appear, and it will also disappear as I want when I click another object, however I want it to disappear when I click the same object again.

I chain signals, not sure if this is the best way to do it, maybe it would be better to go directly, while writing this I thought of a way to skip the middle man that should work, I might try that while waiting for responses, however, that doesn't fix my original problem.

My problem is I haven't figured out how to make it disappear when I click it again, this could be due to my frankenstein signal code, or the way I have it set up, is there a way that I can make the "select" sprite go invisible when I click the same object again. Here is the code...

#Code on child that is emitting a signal after receiving a signal from the Area2D node that is its child
signal selection(source)

func _ready():
	self.add_to_group("spaceships")
	find_child("Selection").add_to_group("selected")

func mouse_select(_viewport, event, _shape_idx):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			emit_signal("selection", self)
		else:
			return
#On parent node that controls
func _ready():
	for ship in arrShip:
		ship.connect("selection", selection)

func selection(source):
	var selected = get_tree().get_nodes_in_group("selected")
	var origin = get_node(get_path_to(source.find_child("Selection")))
	for selector in selected:
		selector.visible = false
	if origin.visible == false:
		origin.visible = true
  • xyz replied to this.
  • xRegnarokx When click signal is received, check if ship is selected. If yes - deselect it. Otherwise - select it.

    You just need a way to determine if ship is selected. The simplest way is to add a boolean property to the ship class, that tracks the selection status of the ship.

    xRegnarokx When click signal is received, check if ship is selected. If yes - deselect it. Otherwise - select it.

    You just need a way to determine if ship is selected. The simplest way is to add a boolean property to the ship class, that tracks the selection status of the ship.

      xyz okay cool, that works this is how I ended up doing it, as well as adding a general deselection when you aren't clicking a unit.

      func _input(_event):
      	if selectorActive != true and Input.is_action_just_pressed("select"):
      		deselect()
      
      func selection(source):
              selectorActive = true
              var selected = get_tree().get_nodes_in_group("selected")
      	var origin = get_node(get_path_to(source.find_child("Selection")))
      	var ship = get_node(get_path_to(source))
      	for selector in selected:
      		selector.visible = false
      	if ship.select == false:
      		for ships in arrShip:
      			ships.select = false
      		origin.visible = true
      		ship.select = true
      	elif ship.select == true:
      		for ships in arrShip:
      			ships.select = false
      		ship.select = false
      		origin.visible = false
              selectorActive = false
      
      func deselect():
      	var selected = get_tree().get_nodes_in_group("selected")
      	for selector in selected:
      		selector.visible = false
      		for ship in arrShip:
      			ship.select = false