I have this function called start(Delta) in which a variable "is_active" has to return true, I have a timer system like this: timer += Delta. I want to make a function called end() executed when a certain time passes in which is_active becomes false. I want the start function to call a function "Hurt_box.create_hit_effect()" but I want it to be executed only once, not all the time (since "start()" works the same as a procces, except that I can call it whenever I want ) and also, when you call the start() function again once end() has been executed, the hurtbox function can be called again.

This is what I tried to do:

var hurt_timer = 0.0

var one_time_effect = false

func start(Delta):

`if one_time == false:`

	`Hurt_box.create_hit_effect()`

	`is_active = true`

	`one_time = true`



`if is_active:`

	`hurt_timer += Delta`

	`if hurt_timer >= 0.2:`

		`hurt_timer = 0.0`

		`one_time_effect = false`

		`end()`

	

	`Hit_box.collision_shape_2d.disabled = true`
func end():

`emit_signal("end_state")`

`is_active = false`

`one_time = false`

` :
If you need more information, you can ask, I would be very grateful if someone could help me :)

    DaPOPs Use a timer to call end() after a certain time. All you need is one line of code:

    get_tree().create_timer(.2).connect("timeout", end)

      DaPOPs

      var hurt_timer = 0.0
      var one_time_effect = false
      
      func start(Delta):
      	if one_time == false:
      		Hurt_box.create_hit_effect()
      		is_active = true
      		one_time = true
      	if is_active:
      		hurt_timer += Delta
      		if hurt_timer >= 0.2:
      			hurt_timer = 0.0
      			one_time_effect = false
      			end()
      		Hit_box.collision_shape_2d.disabled = true
      
      func end():
      	emit_signal("end_state")
      	is_active = false
      	one_time = false

      1 - you don't need so many bools.

      DaPOPs call a function "Hurt_box.create_hit_effect()" but I want it to be executed only once

      do you want the thing to run every time the timer ends or just once and start over when pressing a button?

      run every tick: just run it with end.

      if hurt_timer >= 0.2:
      	hurt_timer = 0.0
      	one_time_effect = false
      	Hurt_box.create_hit_effect()
      	end()

      run once: don't reset the variable after setting it to true, set it back to false from OUTSIDE THE SCRIPT

      func end():
      	emit_signal("end_state")
      	is_active = false
      	#one_time = false

      myScript.one_time = false

      DaPOPs when you call the start() function again once end() has been executed, the hurtbox function can be called again.

      but you are calling this thing every frame... it's doing what it's supposed to.
      try putting the timer OUTSIDE the function and having it call the function instead:

      if hurt_timer >= 0.2:
      	hurt_timer = 0.0
      	start()

      edit:

      xyz DaPOPs Use a timer to call end() after a certain time. All you need is one line of code:
      get_tree().create_timer(.2).connect("timeout", end)

      the timer has to run continuously, so use a timer node instead.

      • xyz replied to this.

        Jesusemora the timer has to run continuously, so use a timer node instead.

        From the code OP posted it looked like they want to run it on demand i.e. when start() is called.