![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | turtilla955 |
the code is meant to make the player sprint when the ‘ability’ button is pressed and held then stop after 3 seconds. then another timer starts where the player cannot sprint for 3 seconds. after the 3 seconds are up the player can sprint again and the loop reapetes.
this is not what happens. the player can sprint but it just continues too while the ‘ability’ button is pressed. how can i make it do what i said it should above.
it will probably be clear by my code that i have no idea what im doing. please dont be to harsh.
important code segments:
var speed = 3
var default_speed = 3
var sprint_speed = 5
onready var sprint_timer = $sprint_timer
onready var sprint_wait_timer = $wait_timer
func _process(delta):
speed = default_speed
if sprinting == true:
speed = sprint_speed
if sprinting == false:
speed = default_speed
if Input.is_action_pressed("ability") and can_sprint == true:
sprinting = true
sprint_timer.start()
elif Input.is_action_just_released("ability"):
sprinting = false
func _on_sprint_timer_timeout():
sprinting = false
sprint_wait_timer.start()
can_sprint = false
func _on_wait_timer_timeout():
can_sprint = true
A few questions:
- Are you really using Godot 3.0? If so, I wonder why? That’s REALLY old…
- Are the
timeout
events firing as expected? If you’re not sure, add a print statement to each to verify they’re working when/as intended.
Since the speed
variable is only ever assigned the higher value if sprinting
is true and since sprinting
is only ever set to true when can_sprint
is true, which is only ever set in _on_wait_timer_timeout()
, I suspect your timeouts aren’t working as expected…
Additionally, I’ll say this seems overly complex for the intended result.
jgodfrey | 2023-02-08 20:48
- when i open the project it says godot 3.0 but the launcher says v3.4.4
- after adding:
print ("on sprint works")
to the end of the first timetimeout
the message does not print to the output as you expected
turtilla955 | 2023-02-08 20:59
when i open the project it says godot 3.0 but the launcher says v3.4.4
In the editor, what version do you see when you use Help | About
? I’d recommend that you update to the latest release version (3.5.1 as of now).
the message does not print to the output as you expected
That means that the timeout
events on the Timer
nodes aren’t properly connected. You’ll need to fix that to get those callbacks to work.
jgodfrey | 2023-02-08 21:04
Not really what you’re asking but you’re making this extremely hard for yourself, i did the same when i was new.
You don’t need timers, you can just use the delta (wich is the time since last frame in seconds) to subtract a variable, for example:
var timer = 3.0
func _process(delta):
if timer > 0:
timer -= delta
And then you can verify if the timer has run out like so:
if timer > 0:
DoStuff()
And set it by, for example, 5 seconds like so:
timer = 5.0
Guliver_Jham | 2023-02-08 21:08
You don’t need timers, you can just use the delta
This is certainly a viable option too, though whether it’s simpler or not depends on the details of what you’re doing, how many timed events your trying to handle, and really, personal preference.
However, I would suggest the pattern should look something like this:
var timer_wait = 3.0
var timer = timer_wait
func _process(delta):
if timer > 0:
timer -= delta
else:
do_stuff() # timer has reached 0, do your thing...
timer = timer_wait # reset the timer to count down again
jgodfrey | 2023-02-08 21:27
i implemented this and it works to print things. but its printing at a rate way faster then every 3 seconds. if im not mistaken delta time is based on framerate, and since i have a high framerate 3 seconds goes by mych faster. what can i do to fix this?
turtilla955 | 2023-02-08 21:40
delta
is correct here. It’ll be the elapsed time since the last call to _process()
. So, simply subtracting it from some initial value is what you want. If it’s not working as expected, something else must be wrong. Maybe post your current code?
jgodfrey | 2023-02-08 22:09
here are the updated code segments:
var timer_wait = 3.0
var timer = timer_wait
var speed = 3
var default_speed = 3
var sprint_speed = 5
var sprinting = false
var can_sprint = true
func _process(delta):
speed = default_speed
if sprinting == true:
speed = sprint_speed
if sprinting == false:
speed = default_speed
if Input.is_action_pressed("ability") and can_sprint == true:
sprinting = true
if timer > 0:
timer -= delta
print ("working")
elif Input.is_action_just_released("ability"):
sprinting = false
i also updated to the latest version of godot
turtilla955 | 2023-02-08 23:30
You neglected to reset the timer (jgodfrey’s code)(note the last line):
if timer > 0:
timer -= delta
else:
do_stuff() # timer has reached 0, do your thing...
timer = timer_wait # reset the timer to count down again
LeslieS | 2023-02-09 06:35
it still prints at the same rate.
turtilla955 | 2023-02-09 20:57