+1 vote

I am doing something similar to the "Navigation Polygon (2D)" example, except have more than one item to move along the path. As a result, I didn't want to repeat the same while loop in _process twice and so wanted a separate function. All attempts wouldn't work. One example is below:

func _process(delta):
    if (path.size() > 1):
        while(to_walk > 0 and path.size() >= 2):
            some_function()

I also tried a simple test with the while loop only in "some_function()" as well:

    func _process(delta):
        if (path.size() > 1):
          some_function()

func some_function():
    while 1<2:
        print ("a never ending while")

Is there a way of doing this or a reason this can't be done?

in Engine by (32 points)

Are you sure you can't just do for node in nodes_to_move, and put the while loop inside?

I think that would work too yes, thanks!

1 Answer

0 votes

I found a solution - the separate function needs to return something, eg:

func _process(delta):
 if (path.size() > 1):
        while(to_walk > 0 and path.size() >= 2):
           someVariable =  some_function(someVariable)
by (32 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 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.