This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
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
end while

if I write this without any "wait" operations rotation happens immediately...:

while get_rotd() <= 90:
    var ARot = get_rotd()
    ARot+= 2 * delta
in Engine by (228 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
by (688 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

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,286 points)

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)

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 Frequently asked questions and 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 [email protected] with your username.