Need help with my function not having pauses

I am making a game where if you do anything in the game the hunger goes up, but if you stand in place then static electricity will build up and then you get struck by lightning and die. But when i stand still the thing just builds up immediatly with no pauses in between, how do i fix it?

Here’s the code of my player:

extends CharacterBody3D

@onready var CameraController = $Cameracontroller
const Gravity = 3
var Speed = 2
var idle = true
var pickingup = false
var sticks = 0
var static_electricity = 0
func _process(delta: float) -> void:
	
	if not is_on_floor():
		velocity.y += -Gravity * delta #gravity
		
		

	
	
	if Input.is_action_just_pressed("CameraLeft"): #turn camera left
		CameraController.rotate_y(0.5)
	if Input.is_action_just_pressed("CameraRight"): #turn camera right (i think)
		CameraController.rotate_y(-0.5)
		
	
	var input_dir = Input.get_vector("Move_Left", "Move_Right", "Move_Forward", "Move_Backwards")
	var direction = (CameraController.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	
	
	if direction:
		velocity.x = direction.x * Speed
		velocity.z = direction.z * Speed
		
	else:
		velocity.x = move_toward(velocity.x, 0, Speed)
		velocity.z = move_toward(velocity.z, 0, Speed)
		
	
	
	if Input.is_action_just_pressed("Move_Left"):
		$Sprite3D.flip_h = true
		$Sprite3D.offset.x = -9.0
		$Hitbox/CollisionShape3D.position.x = absf($Hitbox/CollisionShape3D.position.x) * signf(velocity.x)
	if Input.is_action_just_pressed("Move_Right"):
		$Sprite3D.flip_h = false
		$Sprite3D.offset.x = 9.0
		
	if velocity == Vector3.ZERO:
		idle = true
		if idle == true and pickingup == false:
			$AnimationPlayer.play("Idle_Unarmed")
		static_electricityfunc() #function call
	else:
		idle = false
		if idle == false and pickingup == false:
			$AnimationPlayer.play("Walk_Unarmed")
		
	if idle == true and pickingup == true or idle == false and pickingup == true:
		$AnimationPlayer.play("PickUp_Unarmed")
		await get_tree().create_timer(1.0).timeout
		velocity = Vector3.ZERO
		pickingup = false
	
	$Cameracontroller/CameraManager/Camera3D/CanvasLayer/Label.text = str("Sticks =", sticks)
	$Cameracontroller/CameraManager/Camera3D/CanvasLayer/Sprite2D3.frame = static_electricity
	move_and_slide()
	

func static_electricityfunc(): #this function 
	static_electricity += 1
	await get_tree().create_timer(1.0).timeout

The lightning will be added later

Hi,

await will trigger a time break inside the function itself before going to further instructions.
In your current implementation:

func static_electricityfunc(): #this function 
    static_electricity += 1
    await get_tree().create_timer(1.0).timeout
    # Nothing here!

Nothing happens after the await, so it’s basically useless.

In your process, you call static_electricityfunc() every frame, which will increment static_electricity by one and start the useless await call, explaining why your delay does not work.


Here’s one thing you could do: declare a new variable that will act as your timer for static electricity. While player is not moving, increment that timer over time (by adding delta to it), and if is has exceeded 1 (meaning it’s been running for one second or more), then you can increment static_electricity. Last thing to do is reset the timer to 0 when moving.
Something like this:

var static_electricity_timer: float = 0

func _process(delta: float) -> void:
    if velocity == Vector3.ZERO:
        static_electricity_timer += delta
        if static_electricity_timer > 1:
            static_electricityfunc()
    else:
        static_electricity_timer = 0

In such a code, you don’t need your await line anymore.
Let me know if that works.

It kinda worked. I needed to put the static_electricity_timer reset to the static_electricity function. Thank you!

Glad it worked!

I needed to put the static_electricity_timer reset to the static_electricity function.

Seems strange to me tbh but hey, if you have a working code, that’s fine I guess :smile:

1 Like

Oooh because you want to trigger the static electricity and then reset the timer, right? That would make sense in that case.

1 Like