0
\$\begingroup\$

The reason I address this question is because of the limited documentation and support for starting devs on Godot. I do not know what is wrong with this one if statement, but after I added it, my player was unusable and invisible.

var collision = move_and_collide(vel * delta)

if collision.collider.name == "Spikes Tilemap":
    print("collided")

Spikes Tilemap is a tilemap I set custom to collision detection for spikes.

If you need more information, ask me. Thank you for your patience.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Well, the if statement doesn't seem to do any harm... are you sure you didn't change anything else? \$\endgroup\$
    – Arian_ki
    Mar 28, 2022 at 1:26
  • \$\begingroup\$ @ArianKeshvari I turned the if statement, and the if statement only, into a comment, and it ran just fine. \$\endgroup\$
    – Athsmooth
    Mar 28, 2022 at 1:37
  • \$\begingroup\$ Do you see any error messages getting printed out? Can you share more of the script than the 3 lines you have so far? What script is that? The player? Is it a KinematicBody2D? \$\endgroup\$
    – yoozer8
    Mar 31, 2022 at 2:40
  • \$\begingroup\$ @yoozer8 It is a kinematic body 2d. the error is "Invalid get index 'collider' (on base: 'null instance') \$\endgroup\$
    – Athsmooth
    Apr 2, 2022 at 2:01

1 Answer 1

2
\$\begingroup\$

Based on the limited information you've provided, my guess is a null pointer exception in the script.


From the KinematicBody2D docs (assuming your player, and/or the script your showing us, is a KinematicBody2D):

The body will stop if it collides. Returns a KinematicCollision2D, which contains information about the collision when stopped, or when touching another body along the motion.

From the KinematicCollision2D docs (discussing move_and_collide results):

If a collision is detected, a KinematicCollision2D object is returned.

It seems move_and_collide doesn't always return a value, and will only actually give you an object if there is a collision. You should add a check that you actually got an object before trying to access that object's properties. If you got no result, assume there is no collision.


Based on your comment providing the error output, this is exactly the issue.

It is a kinematic body 2d. the error is "Invalid get index 'collider' (on base: 'null instance')

The move_and_collide function only returns an object when there is a collision. Otherwise it returns null. It is called every frame, and there is not always a collision. You need to add a null check to verify it actually returned something before checking what kind of collision it was.


To fix avoid this particular error, change your if condition to the following, in order to make sure you only attempt to access properties on the collision object if it actually exists.

if collision != null:
    if collision.collider.name == "Spikes Tilemap":
        print("collided")

    # any other code that relies on using collision
\$\endgroup\$
7
  • \$\begingroup\$ I did have a node called spikes tilemap \$\endgroup\$
    – Athsmooth
    Apr 1, 2022 at 22:09
  • \$\begingroup\$ I added a second spike on my tileset that has tilemap but it did nothing \$\endgroup\$
    – Athsmooth
    Apr 1, 2022 at 22:14
  • \$\begingroup\$ @Athsmooth the "Spikes Tilemap" bit actually doesn't matter here (at least not for this particular bug). The problem is that you are trying to access the property of something that does not exist. Since no collision happens, move_and_collide returns null, so your variable collision doesn't actually have a value. So collision.collider fails, because collision is not an object with a property called collider (because it is not any object at all). \$\endgroup\$
    – yoozer8
    Apr 3, 2022 at 1:15
  • \$\begingroup\$ @Athsmooth I've added an example for how you can resolve this error. Hopefully that will help you \$\endgroup\$
    – yoozer8
    Apr 7, 2022 at 1:46
  • \$\begingroup\$ I'm sorry. It seems like I didn't fully understand what you were saying at the time. This does seem to work. Thanks! \$\endgroup\$
    – Athsmooth
    Apr 7, 2022 at 23:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .