getting labels to alternate visibility when releasing objects

Godot Version

v4.4.1.stable.official [49a5bc7b6]

Question

i am making a simple game where you pluck petals, and 2 user input options display on the screen, alternating. pluck one petal, option 1 appears; pluck another petal, option 1 disappears, and option 2 appears, etc. i managed to get the labels to show/hide inputs put into lineedit nodes, but when i try to make the show/hide happen when a petal is released from the mouse, it only... sorta works? you release a petal, and option 1 appears, but nothing changes when you release a second petal, instead, it only changes when you release the same petal twice? im unsure how i would code this properly, and would love input from more experienced developers!

Hi,

It depends on how your code is structured; there would be plenty of ways of doing that so it’s hard to help precisely without any code.


Anyway, if I understand the issue correctly, you could simply do it that way:

1/ Define a function switching the options:

func switch_options():
    # Assigning a boolean to itself with "not" will switch between true and false.

    option1.visible = not option1.visible
    option2.visible = not option2.visible

2/ Call that function whenever needed. That’s where it’s on your side, since I don’t know your code structure. But, let’s say you have a function called when a petal is clicked, then you should just call the function there:

func petal_click():
    # Insert petal related code here...
    switch_options()

I know that’s a pretty vague answer but hopefully it helps. Otherwise, please share some more context and code. :slight_smile:

this actually works well, though i was wanting the options to alternate from each other as well! so, one turns on, the other turns off, with each click. how would that be implemented?

I thought that was my 1/, but I must have misunderstood something here?
Could you explain in a bit more details?

this is the code i have for the rigidbody2d petal, with your advice implemented.
right now, when i release a petal, BOTH options display at the same time, and when i release again, they BOTH disappear at the same time. i want only one showing at a time, alternating which is showing.

extends RigidBody2D

signal clicked

var held = false
#calls labels from flower.tscn
@onready var option_1_label: Label = $"../../option1label"
@onready var option_2_label: Label = $"../../option2label"

func _ready() -> void:
	var option_1_label: Label = $"../../option1label"
	var option_2_label: Label = $"../../option2label"

#func to switch option visibility
func switch_options():
	option_1_label.visible = not option_1_label.visible
	option_2_label.visible = not option_2_label.visible

#prevents rigid petals from doing gravity things on startup
func _on_area_2d_body_entered(body: Node2D) -> void:
	if body is RigidBody2D:
		sleeping = true

#debug clicking
func _on_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
		if event.pressed:
			print("clicked")
			clicked.emit(self)

#positions petal to mouse when clicked
func _physics_process(delta):
	if held:
		global_transform.origin = get_global_mouse_position()

#clicked
func pickup():
	if held:
		return
	freeze = true
	held = true

#released
func drop(impulse=Vector2.ZERO):
	if held:
		freeze = false
		apply_central_impulse(impulse)
		held = false
	switch_options()
		#print("weeee")

Oh, then you just need to show one whenever needed, by using:

option_1_label.visible = true

Add this in _ready and that should work. You can then move the line somewhere else, based on your needs.