Quick Question: How To Simulate An Action press

Godot Version

4.2.2

Question

i want to add mobile support to my game and i want to make one of the buttons trigger the “flashlight” action. how can i do that

flashlight as in real phone’s flashlight? elaborate?

1 Like

no. just take a look at my code

extends TextureProgressBar

@onready var player : Player = %Player

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	max_value = player.flashlight_component.flashlight_duration
	value = player.flashlight_component.time_left

func _on_button_button_down():
	Input.action_press("flashlight")
	Input.action_release("flashlight")

and the flashlight component:

extends Node
class_name FlashlightComponent

@export var light : Light2D = null
@export var flashlight_key = "flashlight"
@export var flashlight_duration = 5
@export var flashlight_refill = 0.1
@export var flashlight_delay = 0.2

var time_left
var last_refill = flashlight_delay + 1
var state = false

# Called when the node enters the scene tree for the first time.
func _ready():
	time_left = flashlight_duration


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	last_refill += delta
	if(last_refill > flashlight_delay && !state):
		last_refill = 0
		time_left += flashlight_refill
		if(time_left > flashlight_duration):
			time_left = flashlight_duration
	if(state):
		if(time_left > flashlight_duration/5):
			light.show()
			time_left -= delta
		else:
			light.hide()
	else:
		light.hide()

func _input(event):
	if(event.is_action_pressed(flashlight_key)):
		state = !state

edit: i know that just flipping the state works but for movement this will not work (im trying to port to android)

what seems the problem here again? the flashlight doesnt flash?

the problem is that i cant seem to find a way to simulate the press and release of an action

See the docs of Input.action_press().
Using this function will not cause an input event, that reaches _input functions.

i wonder why you are using TextureProgressBar as the button?
you can use basic Button Node and use the built-in button_down and button_up, to simulate the press
and have this button a child that’s TextureProgressBar that actually a texture to show the progress . Yes, set the Button Node’s Style to StyleBoxEmpty once you done set the TextureProgressBar

godot even have a node that let you “press” when holding down, that’s TouchScreenButton node. different than Control’s Button. it let you press when button_down.

1 Like

its a neat trick to use a button to both show the battery left and a button