Topic was automatically imported from the old Question2Answer platform.
Asked By
bgegg
but print statement always print false.
i want make toggle button.
when i pushed a key at first time,i want print true.
the second time i want print false.
the third time i want print true.
can i this action?
extends Node2D
var toggle:bool
func _ready():
toggle = false
func _process(delta):
if Input.is_key_pressed(KEY_A) and toggle == false:
toggle = true
elif Input.is_key_pressed(KEY_A) and toggle == true:
toggle = false
print(toggle)
HI there,
I know this was two years ago, but I wanted to put this in for posterity.
Your script example is extending Node2D. It should be extending the button type you are attaching it to. For example, CheckButton or CheckBox.
See the existing toggle_mode variable.
thanks for comment.
is not it possible to toggle a boolean without using button node?
I don’t want to use buttons. But I want a boolean that flips for pause view for get_tree().paused.
i want flip this.
I made a workaround that you can use. If you use keypressed you can use this.
var a = 1
var b = true
func _input(event):
var just_pressed = event.is_pressed() and not event.is_echo()
if Input.is_key_pressed(KEY_SPACE) and just_pressed:
print (var2str(b))
toggleswitch()
func toggleswitch():
a = a * -1
if a == 1:
b = true
elif a == -1:
b = false
If you want to use Input actions you can try this.
var a = 1
var b = true
func _process(_delta):
if Input.is_action_just_pressed("ui_accept"):
print (var2str(b))
toggleswitch()
func toggleswitch():
a = a * -1
if a == 1:
b = true
elif a == -1:
b = false