Godot Version
4.0
Question
I asked this on Reddit, but didn’t get any help there. Hopefully someone here knows what’s up. I’m also open to alternative ways to make my switch, if there are any ideas.
I’m making a game that’s all buttons and sliders and switches meant to be played on touchscreen. I’m trying to use an HSlider to create a binary switch that can be flipped by swiping a finger across it.
Intended behavior: Switch rests in the OFF position. When player swipes a finger across it, the switch flips to the ON position and can no longer be controlled by the player. After a timer goes off, the switch flips back to the OFF position, but remains deactivated until a second timer goes off and reactivates it, making it possible for the player to flip the switch again.
What happens: Everything works correctly … once. After the switch reactivates, the HSlider reacts to mouseover / mouse movement events as if they were click or drag events.
Setup: I took the scene down the bare minimum, and the issue still persists.
NODES
HSlider *script attached here*
|_Timer
|_Duration (Timer node)
The timers are connected to _on_timer_timeout()
and _on_duration_timeout()
respectively. The value_changed()
signal from the HSlider is connected to the _on_switch_flipped()
function. The issue definitely lies with the value_changed()
signal, but I can’t figure how else to get the behavior that I want. I don’t want the player to be able to move the switch back and forth before releasing it, for example. Once it flips, the player should lose control, even if they were still trying to drag it.
HSLIDER INSPECTOR
CODE
extends HSlider
@export var disable_time := 2.0
@export var active_time := 1.0
func _on_switch_flipped(value : float) :
if value == 1.0 :
$Duration.start(active_time)
editable = false
$Timer.start(disable_time)
func _on_duration_timeout() :
$Duration.stop()
editable = true
set_value_no_signal(min_value)
editable = false
func _on_timer_timeout() :
$Timer.stop()
editable = true
I’m using a laptop with a trackpad for the mouse, but it doesn’t look like the input it being processed any differently from a normal mouse. I created a function to print every input event. Here’s a small bit of the output. I can’t see a difference between what’s happening before and after the click, but afterwards is when the slider starts reacting to mouse movement as if it was clicks. (The velocity being 0,0 doesn’t stay and happened at the start before too.)
InputEventMouseMotion: button_mask=0, position=((1028, 188)), relative=((4, -1)), velocity=((-30.0149, 2.143922)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=0, position=((1030, 186)), relative=((2, -2)), velocity=((-30.0149, 2.143922)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=0, position=((1031, 186)), relative=((1, 0)), velocity=((-30.0149, 2.143922)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseButton: button_index=1, mods=none, pressed=true, position=((1031, 186)), button_mask=1, double_click=false
InputEventMouseButton: button_index=1, mods=none, pressed=false, position=((1031, 186)), button_mask=0, double_click=false
InputEventMouseMotion: button_mask=0, position=((1031, 186)), relative=((0, 0)), velocity=((0, 0)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=0, position=((1047, 177)), relative=((16, -9)), velocity=((0, 0)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=0, position=((1060, 171)), relative=((13, -6)), velocity=((0, 0)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=0, position=((1077, 162)), relative=((17, -9)), velocity=((0, 0)), pressure=0.00, tilt=((0, 0)), pen_inverted=(false)
InputEventMouseMotion: button_mask=0, position=((1104, 156)), relative=((27, -6)), velocity=((0, 0)), pressure=0.00, tilt=((0, 0)),
I don’t know why the HSlider thinks that mouse movement is changing its value. Any help is appreciated. Thanks!