0 votes

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)
in Engine by (230 points)

2 Answers

+2 votes
Best answer

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)
by (649 points)
selected by
+1 vote

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.

by (5,163 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories