0 votes

I want to make a loop so that when the area collides it changes position immediately where no collisions

while get_overlapping_areas():
            self.global_position.x += 1

how could i do it then?

Godot version 3.2
in Engine by (182 points)

2 Answers

0 votes

while is conditional meaning it needs a bool true or false value but get_overlapping_areas() returns an array. The array type has the size() method which returns an int so an operator (<, >) can be used to return a bool.

while get_overlapping_areas().size() > 0:
by (3,225 points)

but it still doesn't work

while get_overlapping_areas().size() > 0:
        position.x += 1

In the question, by 'when the area collides' did you mean with only other areas? If not you should be using get_overlapping_bodies() if you want to detect static, rigid & kinematicbodies.

while get_overlapping_areas().size() > 0:

will cause an endless-loop (game stops working) if size is not 0.

0 votes
for _i in get_overlapping_areas():
    global_position += 1

or better:

global_position.x += get_overlapping_areas().size()

(both untested)

by (1,442 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.