I am working on a project for my computer science class in high school, I have two timers set up, and one is supposed to start after the other and they go back and forth for as long as the game is running and the node containing their code is active.

Here is my code:

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
	#Timer1 start
	if moveActive == true && moveTimer >= 0:
		moveTimer -= delta
		print("moveTimer")
	#timer start
	if moveTimer <= 0:
		stopTimer = maxStopTimer
		stopActive = true
		moveActive = false
		
		
		while stopActive == true && stopTimer >= 0:
			stopTimer -= 1;
			print(stopTimer)
			#hen one is true do it, then turn it off hen you have it reach 0

		if stopTimer <= 0:
			stopActive = false
			moveTimer = maxMoveTimer
			moveActive = true
			
		velocity.x += moveSpeed

The first timer is using an if statement, but in order to work, the second timer needed to be a while loop, and everything works, but the while loop goes to fast for normal numbers to be used as an effective timer, and when you raise the number to one that works, it goes so fast that the game stutters and lags.

It's a little weird but the system I have checks most of the boxes for the project I just need the second timer to work.

  • xyz, Megalomaniak, and DaveTheCoder replied to this.
  • Here's how you should approach it:

    const MOVE_TIMER_MAX = 1.0
    const STOP_TIMER_MAX = 1.0
    var moveTimer = MOVE_TIMER_MAX
    var stopTimer = 0
    
    func _physics_process(delta):
    	if moveTimer > 0:
    		moveTimer -= delta
    		print("MOVE TIMER TICKING ", moveTimer)
    		if moveTimer <= 0:
    			moveTimer = 0
    			stopTimer = STOP_TIMER_MAX
    	if stopTimer > 0:
    		stopTimer -= delta
    		print("STOP TIMER TICKING ", stopTimer)
    		if stopTimer <= 0:
    			stopTimer = 0
    			moveTimer = MOVE_TIMER_MAX

    And here's a generalized solution that indefinitely cycles any number of named timers, with names and durations supplied in an array:

    var timer = [["",0.0]] + [["move", 2.0], ["stop", 2.0], ["think", 1.0 ], ["follow", 1.5 ]]
    
    func _physics_process(delta):
    	timer[0][1] -= delta
    	if timer[0][1] <= 0:
    		timer.pop_front()
    		timer.push_back(timer[0]+[])
    		print("STARTING: ", timer[0][0])

    If this is your homework, don't show the second code snippet to your teacher. They'll know you didn't write it 😉

    Cheesedip You should treat the stopTimer the same way you treat the activeTimer i.e. decrement it by delta each frame. No need for the while loop. The _physics_process() function already works like a giant while loop.

      Cheesedip stopTimer -= 1;

      Try replacing the 1 with delta instead. The _physics_process(delta) keeps track of the time between each tick via the delta variable it provides there.

      xyz I tried that, but the stop timer only decreases once and doesn't decrease anymore, I think it's because it's inside of another if statement, but I'm not sure.

      I think you misunderstand how the Godot engine works. There's already a Main Loop in the engine. _physics_process() is called inside that loop. Adding a while-loop as you're doing is not the right approach.

      Here's how you should approach it:

      const MOVE_TIMER_MAX = 1.0
      const STOP_TIMER_MAX = 1.0
      var moveTimer = MOVE_TIMER_MAX
      var stopTimer = 0
      
      func _physics_process(delta):
      	if moveTimer > 0:
      		moveTimer -= delta
      		print("MOVE TIMER TICKING ", moveTimer)
      		if moveTimer <= 0:
      			moveTimer = 0
      			stopTimer = STOP_TIMER_MAX
      	if stopTimer > 0:
      		stopTimer -= delta
      		print("STOP TIMER TICKING ", stopTimer)
      		if stopTimer <= 0:
      			stopTimer = 0
      			moveTimer = MOVE_TIMER_MAX

      And here's a generalized solution that indefinitely cycles any number of named timers, with names and durations supplied in an array:

      var timer = [["",0.0]] + [["move", 2.0], ["stop", 2.0], ["think", 1.0 ], ["follow", 1.5 ]]
      
      func _physics_process(delta):
      	timer[0][1] -= delta
      	if timer[0][1] <= 0:
      		timer.pop_front()
      		timer.push_back(timer[0]+[])
      		print("STARTING: ", timer[0][0])

      If this is your homework, don't show the second code snippet to your teacher. They'll know you didn't write it 😉

        xyz Thank you so much! The first snippet is what I was initially trying to do, but one thing doesn't work and I try and fix it, and end up making it more complicated than it needs to be. I'll continue to work on this project and post here if I need help, we're allowed to ask for, and get help from peers, in person or online, as long as code not written by yourself is cited as such. Thank you for your help!