Issues with power up thing.(solved)

godot 4.3

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.

it is not below the same function i have checked and Changed what functions its in

Please repost your code and use markdown to preserve indentation (i.e place three backticks (```) before and after your code.)

If the code after the print statement falls outside the scope of a function, then this script won’t even run.

thank you for informing me, i have changed it and shown where the if cool is more logically

I don’t see anything in that code that ever sets cool to true.

Edit: Ah, ok, you note it’s coming from another script.

Presumably that other script is setting up the timer as well? If so, how is it referencing _on_timer_timeout(), since that’s local?

Edit Edit:

Presumably where you timer.start() you should be hooking up _on_timer_timeout(). The timer won’t fire that unless it’s told to.

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)

i would like to say that it worked before i implemented the timer but this is the activating script

extends CharacterBody2D

@onready var guy: CharacterBody2D = $"../guy"




func _on_area_2d_body_entered(body: Node2D) -> void:
	guy.velocity.y = -500
	guy.cool = true
	queue_free()

(guy is the player)

So, cool gets set from outside, but:

  • 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...

1 Like

It may be connected in-editor

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.


@gooberdev

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?

Sort of; it’s hammering the animation in every update as well, so I’d expect it to be stuck on the first frame.

Unlike Timers, AnimatedSprite2D does not restart animations on subsequent .play()s, I’m not sure on setting .animation directly though.

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.

1 Like

the only issue is that cool dosent stop, nothing else rlly happens just that cool dosent end

i think the issue is that it is not receiving the on_timer_timeout or timer.start correctly

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, …

Also what are you trying to do here?

if velocity.x < 0:
  Sprite2D
1 Like

thank you for explaining it in a way i would understand, im a beginner and also the bit at the bottom was a piece of messed up code i forgot to delete

OMG TYSM I FIGURED IT OUT BC OF YOU YIPPIE. yes godot forums this is a complete sentence.