'body->get_space()' error sometimes when I start my game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Yeldham

Sometimes, when I start my game, at the very beginning, this error pops up:

0:00:00:0484 - Condition ' !body->get_space() ' is true. returned: false
----------
Type:Error
Description: 
Time: 0:00:00:0484
C Error: Condition ' !body->get_space() ' is true. returned: false
C Source: servers/physics_2d/physics_2d_server_sw.cpp:969
C Function: body_test_motion

I don’t know if it’s a bug or something I’m doing wrong. Here is the segment of code where the error points out, in the move_and_slide function calling:

func _physics_process(delta):
direction = 0
if Input.is_action_pressed("ui_left"):
	direction -= 1
if Input.is_action_pressed("ui_right"):
	direction += 1

speed_current.x = lerp(speed_current.x, WALK_MAX_SPEED * direction,
		SPEED_STEP)

if is_on_floor() and Input.is_action_just_pressed("ui_up"):
	speed_current.y = JUMP_FORCE

speed_current.y += GRAVITY * delta

speed_remaining = move_and_slide(speed_current, FLOOR_NORMAL)
speed_current.y = speed_remaining.y
if not is_on_floor():
	speed_current.x = speed_remaining.x

if is_on_floor() and position.y < camera.get_camera_position().y:
	camera.drag_margin_top = 0
elif camera.drag_margin_top == 0:
	camera.drag_margin_top = CAMERA_TOP_MARGIN

Ever figure out what the issue was?

Diet Estus | 2018-12-10 00:28

Hell, I stopped working on the project that this code was from since a good time. I actually don’t know.

Yeldham | 2018-12-13 22:11

I just got this error myself. AFAI can tell, it’s from trying to spawn a physics body in a place where it can’t be spawned, i.e colliding.

The reason i think this is because I got the error from spawning a large amount of kinematic 2d bodies into a relatively small space. The game crashed, and like I say, I think its from them not having room to be created without colliding with what’s alreay there.

ka0s420 | 2019-09-19 05:40

:bust_in_silhouette: Reply From: boruok

i’ve got this error when restarting current scene few times. Still don’t know how fix this.

:bust_in_silhouette: Reply From: 763484204

i got the same problem when i try to add a kinemoditybody2d to my scene, the error happend when the kinemoditybody2d call the move_and_slide and the linear_velocity that passed in is zero(only once),i updated the code to call the move_and_slide only when the velocity is not zero and the error solved
sorry my english is not good

I think i know why this error pops up. I am also happy to share this, because i solved it a bit by trail and error. I had the same error and it happen when my player (being a characterbody2d) was trying to walk up a stair. my game is a top down game and the player can collide with a wall and something else.

I only got the error when my player was colliding with the wall, and it was climbing the stairs. if you climb the stairs the player node is moved to another parent, subportview and given a new position. it is moved away from the collision, because the object is placed in another area where it does not collide anymore.

So what i think that happens in the cycle is this (although this is a guess):

  1. the collision is detected before the move and slide is done:
  2. after all collision objects are known for collision, the object is moved by my code during execution of my code.
  3. it wants to use the collision object in move_and_slide, but it is not colliding any more with that body, resulting in the “Body->get_space()” error.

How to fix it:
i made a piece of code that checks first if it is climbing the stairs. If so, do not execute move_and_slide this frame:

if !CheckEnterStairs():
move_and_slide()

So the conclusion is that the move and slide does not like it if you change the surroundings of your collisions. so it could happen in many forms. think of stuff like:

  1. spawning a player in a new environment and still calling move_and_slide (i think it is possible that the scenario of @Yeldham could be this, as my own story)
  2. spawning a collision possibility on or right next to to the player during runtime. (like the scenario of @ka0s420 or maybe @Yeldham )

I think it is also wise to look at the collision layers and masks. But bare in mind that changing the collision situation on runtime can give the error.