Sprite visibility not toggling at correct inputs

Godot Version

4.4.1

Question

I’m creating a scene where when a sprite is clicked, it moves the object after a click in another location. To show off exactly where the object will be after movement, I made a “follower” to act as a sort of ghost version of it.
The follower always follows the cursor, but is hidden when the sprite hasn’t been clicked yet.
The follower only shows up half of the time, and only after a complete “movement” has been done. Its difficult to take screenshots thanks to how the code works, but it essentially goes like this:

  • Sprite is unclicked, follower is not visible
  • Sprite is clicked, follower should be visible but isn’t
  • Sprite is moved by clicking elsewhere, follower suddenly appears when it shouldn’t be visible
  • Sprite is clicked again, follower is still visible
  • Sprite is moved elsewhere again, follower is no longer visible

This cycle repeats, unsure of why though.

code of the object:

extends Area2D
var move_act: bool = false
var clicked: bool = false
var hovered: bool = false
var init_pos = Vector2(position.x, position.y)
signal hue_act
signal hue_def
signal follower_mark
signal follower_stop

func _input(event):
	if event.is_pressed() and hovered == true:
		move_act = true
		emit_signal("hue_act")
		emit_signal("follower_mark")
	
	if event.is_pressed() and move_act == true and hovered != true:
			position.x = init_pos[0]
			position.y = init_pos[1]
			position.x = get_local_mouse_position()[0]
			position.y = get_local_mouse_position()[1]
			move_act = false
			emit_signal("hue_def")
			emit_signal("follower_stop")

func _on_mouse_entered() -> void:
	hovered = true

func _on_mouse_exited() -> void:
	hovered = false

Code of the follower:

extends Sprite2D

var init_pos = Vector2(position.x, position.y)

func _process(float):
	position.x = get_global_mouse_position()[0]
	position.y = get_global_mouse_position()[1]
	

func _input(event):
	if event.is_pressed():
		position.x = init_pos[0]
		position.y =init_pos[1]

func _on_area_2d_follower_mark() -> void:
	print("visible")
	$".".visible = $".".visible

func _on_area_2d_follower_stop() -> void:
	print("non visible")
	$".".visible = not $".".visible

I am unsure of why this isn’t working, especially because the print for “visible” and “not visible” display at the correct times, but the visibility does not toggle

This is not changing anything.
By the way the $ is shorthand for get_node(). The “.” points to the current node.
Skip both of them and just use the property.
Since you are using two functions rather than toggling you must set them specifically

func _on_area_2d_follower_mark() -> void:
	print("visible")
    visible = true  #So much easier to read.     
func _on_area_2d_follower_stop() -> void:
	print("non visible")   
    visible = false

If you were toggling you could set it up like so:

func _on_area_2d_follower_toggle() -> void:
    visible = !visible #visible = not visible

Also get_global_mouse_position() returns a Vector2.
Instead of setting x and y separately just use:
position = get_global_mouse_position()
Here as well:

#var init_pos = Vector2(position.x, position.y)
var init_pos:Vector2 = position   #I recommend specifying the type

And in the other places in your code.

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