A global variable is not affected by a duplicate character

Godot Version

4.2.1

Question

I have global script:

extends Node

var selected = false
var attackmove = false

func _ready():
	Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)

And I also have player, which have this piece of code:

func _input(event):
	if event.is_action_pressed("Lclick"):
		if selectable == true:
			Global.selected = true
			selected = true
			get_node("Circle").modulate = Color(1, 1, 1)
		else:
			Global.selected = false
			selected = false
			$Circle.visible = false

Also I have main scene, where I have player and duplicated “Player2”
image

For some reason, when I select “Player” by pressing “Lclick”, Global.selected variable changes to “true”, but when I select “Player2”, Global.selected variable still return “false”, although variables in player script works fine. Why is that?

Selectable variable is working like this btw

func _on_player_clickable_mouse_entered():
	selectable = true
	if selected == false:
		$Circle.visible = true
		get_node("Circle").modulate = Color(1, 1, 1, 0.522)




func _on_player_clickable_mouse_exited():
	selectable = false
	if selected == false:
		$Circle.visible = false

My best guess is that both player-objects are reacting to the Lclick input, with setting Global.selected to false, and another setting it to true. So which result you get depends on the order of execution - one overwrites the other.

Oh well, that’s most likely it. It seems I should have find another way to instantiate mark when one of troop selected. Thanks for help!

I mean, it’s mostly the deselecting that’s the problem, right? The code in your else branch should only run if this was actually the object that was selected. Something like:

func _input(event):
	if event.is_action_pressed("Lclick"):
		if selectable == true:
			Global.selected = true
			selected = true
			get_node("Circle").modulate = Color(1, 1, 1)
		elif selected: # this object is selected, but we clicked outside of it
			Global.selected = false
			selected = false
			$Circle.visible = false

Might do the trick?

Edit: Although that still won’t quite work if we have object A selected, and then click on object B to change the selection…

Well, my brain desided to actually work for once and I figure out that I could create an array in my Mark creating system, so when I select a troop it appends into this array and if array size != 0, mark is creating and it worked

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.