in the game i want there to be a like power up thing called “cool” in the code.
ive tried pretty much everything with putting it in order and in and out of the process function (the cool is activated from another script i do not believe the issue is that) also there is a timer witch is a child of the player in the level scene.
This is the player script
extends CharacterBody2D
func _on_timer_timeout() -> void:
cool = false
print("it worked yipee")
@onready var sprite_2d: AnimatedSprite2D = $AnimatedSprite2D
var flipx = false
var SPEED = 400.0
var cool = false
var gravity = 20
var jump = -600
@onready var timer: Timer = $Timer
func _physics_process(delta: float) -> void:
# Add the gravity.
velocity.y+= gravity
if is_on_floor():
if Input.is_action_just_pressed("ui_accept"):
velocity.y = jump
if Input.is_action_just_released("ui_accept") and velocity.y < 0:
velocity.y *= 0.2
var direction := Input.get_axis("a", "d")
if direction:
velocity.x = direction * SPEED
if velocity.x < 0:
Sprite2D
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if cool:
jump = -850
SPEED = 600
sprite_2d.animation = "cool"
timer.start()
move_and_slide()
func _on_bounceything_wowhigh(value: Variant) -> void:
velocity.y+=1000
Hi, I can’t tell if the part of your code underneath the print(“it worked yipee”) line is part of the same function. If it is, then the code will never run because cool will always be false.
Could you show this other script? it seems like the cool variable has basic usage here that can’t go wrong (assuming the indentation issues are not present in the real code)
nothing ever tells timer to call _on_timer_timeout()
your if cool: in _physics_process() is going to get called every update if cool is set, so you’ll be firing a new timer off every update
You need something like:
var trigger_cool = false
var cool_active = false
func _on_timer_timeout():
cool = false
func _physics_update:
if cool_active:
# hook the timer up to _on_timer_timeout() here
timer.start()
cool = true
cool_active = false
# set up speed, jump, etc
if cool:
# per update stuff
move_and_slide() # presumably you want this whether cool is set or not...
True, but the effects of “cool” with a higher jump height, speed, and new animation should still play out.
I suppose the author did not specify what they expected to happen vs what really happens so I’m just assuming it’s about the extra effects, rather than setting cool to false.
Do you have any errors or warnings in the output/debug panel? Which part of the script do you believe is failing? What do you expect to happen vs what really happens?
I’m not sure either, but it won’t hurt (and might help) to fix the logic so the “entering cool state” logic doesn’t get rerun every update that cool is set.
As the others have said, you are calling timer.start() every physics tick as long as cool is true. Calling start on a running timer resets it - so it can never finish. So you should only start it once when cool is set.
Even if _on_timer_timeout would be called, you would never leave the ‘cool state’ because you never reset the variables (at least in your provided code). You should do that when the timer ends. A quick fix could look something like this:
func _pyhsics_process(delta: float) -> void:
# your other code
if cool:
jump = -850
SPEED = 600
sprite_2d.animation = "cool"
timer.start()
# immediately reset cool so that this branch is only entered once (i.e., 'cool' is only set up once)
# if you rely on 'cool' somewhere else you could also use a separate boolean var as suggested in the other comment
cool = false
move_and_slide()
func _on_timer_timeout() -> void:
# end 'cool' by resetting the properties, e.g.,
jump = -600
sprite_2d.play("not_cool")
# ...
That being said, I think there are nicer solutions then entering the state in _physics_process. Instead of setting cool directly you could e.g. create a function that is called by the other node or implement a setter for cool that starts the state, …