While loop and wait function

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By websterek
:warning: Old Version Published before Godot 3 was released.

Hi is there any options similar to wait for while loop?
I need to write script similar to this one:

int max_speed = 10
int target_angle = 30

while (target_angle != object.Rotation) do
  int delta = target_angle - object.Rotation
  delta = max(min(delta / 5, max_speed), -max_speed) + max(min(delta, 1), -1)
  object.Rotation = object.Rotation + delta
  wait(.05)
end while

if I write this without any “wait” operations rotation happens immediately…:

while get_rotd() <= 90:
	var ARot = get_rotd()
	ARot+= 2 * delta
	set_rotd(ARot)
:bust_in_silhouette: Reply From: bruteforce

The while loop is unnecessary!

Try something like this:

func _fixed_process(delta):
	if(get_rotd() <= 90):
		var ARot = get_rotd()
		ARot+= 2 * delta
		set_rotd(ARot)
:bust_in_silhouette: Reply From: avencherus

As bruteforce mentioned, the while loop is not needed, and not recommended. You want to work with the delta time provided by the game loop itself.

For your problem, you want to control the speed by time. As an example say you wanted something to rotate 360 degrees over 5 seconds, this is the general idea for achieving that.

var seconds = 5
var rot_d_per_second = 360 / seconds
	
func _fixed_process(delta):
	var angle = get_rotd()
	angle += rot_d_per_second * delta
	set_rotd(angle)

On a side note, you might want to be careful when doing accumulations on angles. Floating point inaccuracy issues can creep into that.

excuse me i’m trying to achieve the same result with opacity but i don’t understand the concept of the script that you provided. For example i don’t know what delta is. (sorry, i’m new to godot)

imazariuz | 2021-07-02 01:41