#how to make a progress bar fill while i'm holding a button

1 messages · Page 1 of 1 (latest)

dreamy bluff
#

export var speed = 300
var velocity = Vector2(0, 0)
var click_position = Vector2(0, 0)
var summon_position = Vector2(0,0)

func _ready():

click_position = Vector2(position.x, position.y)
$casttime.value = 0

func _physics_process(_delta):

var target_position = (click_position - position).normalized()
$slimeidle.flip_h = target_position.x < 0

if Input.is_action_just_pressed("right_click"):
    click_position = get_global_mouse_position()

elif position.distance_to(click_position) > 3:
    move_and_slide(target_position * speed)
    $slimeidle.play("walk")
elif Input.is_action_pressed("left_click") && Input.is_action_pressed("q"):
    $slimeidle.play("cast")
    $casttime.visible = true
    
else:
    $slimeidle.play("default")
    $casttime.visible = false
#

this is the current code i want the bar to fill while im doing the $slimeidle.play("cast")

median jackal
#

The _physics_process func is being evaluated 60 time a second when the Node isn't idle, so you're only going to have one frame where

Input.is_action_pressed("left_click")

returns true

#

In order to maintain awareness of whether a button is currently being held down, you'll need to track some additional state inside your object

#

Here is some sample c# code that achieves this (I am not familiar with gdscript)

#
// This code can be executed inside the _PhysicsProcess(double delta) override
bool isForwardHeld = Input.IsActionPressed("forward");

...
// We can increase some other thing (could be progress bar value) incrementally every frame that forward is held
if(isForwardHeld )
    _throttle.IncreaseThrottle(delta*someOtherIncreaseFactor);
#

there may be more elegant ways to achieve this, just wanted to demonstrate the need to track some kind of "isButtonHeldDown" state internally

#

If I were to zoom out and architect the solution, I'd probably opt for the Command Pattern where button presses are objects in memory. Then each button press could implement some kind of "HoldDownable" behavior so I don't have to continuously copy/paste this code

stable eagle
#

You could just check if the button is being held by making a bool go true if the button is pressed Or false I'd the button is released In the physic process

median jackal
#

I added the step of checking the "isButtonHeld" state so we could avoid extra calls to Input.IsActionJustPressed

#

I don't know what Input.IsActionJustPressed is doing under the hood, I assume it's not at all expensive though

stable eagle
#

Actionjustpressed is checking if the button was pressed at the current frame, actionpressed is if the action is being (pressed or held down).

median jackal
#

Oh right, I see

#

I forgot about that haha

stable eagle
#

I think op forgot to increment the value of the progress bar

median jackal
#

Revised the pseudocode example to use IsActionPressed

#

I found this thread because I'm doing something similar, but my progress bar display appears to snap to values of 25

#

Is there some sort of granularity setting I'm missing?

stable eagle
#

In the node page there's a step value, you might want to check that out

median jackal
#

sorry this is gd4, will make a thread over there