Problems with Raycast2Ds retaining their is_colliding() value

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

I’m running into a problem when I try to change my current level node, where the game crashes because the player’s Raycast iscolliding() still thinks it’s colliding with something, tries to use getcollider(), and crashes when there’s no object there to return.

The node structure is

World
 - Player
 - currentLevel

The level change script is located in World, and is

func instance_scene(scenepath):
    call_deferred("deferred_instance_scene", scenepath)

func deferred_instance_scene(scenepath):
    #clear currentLevel
    for child in $currentLevel.get_children():
        $currentLevel.remove_child(child)
	    child.queue_free()

    #load and instance the scene
    var scene  = load(scenepath)
    var scene_instance = scene.instance()
    scene_instance.set_name(scene_instance.name)
    $currentLevel.add_child(scene_instance)

and the raycast script is located in Player and is:

var bottomRaycasts = [
$CollisionShape2D/rcBotLeft,
$CollisionShape2D/rcBotRight
]

for raycast in bottomRaycasts:
	if raycast.is_colliding():
		print(raycast.get_collider())
		var object = raycast.get_collider()
		if object.get_collision_layer() == 16384:
			colFloor = true
		elif object.get_collision_layer() == 32768:
			colFloor = true
			floorBounce = true

That print spits out one line of Object:null before the crash. My understanding is that it shouldn’t be printing anything, because there’s nothing for it to be colliding with.

EDIT: Interestingly, if I pause the Player node with gettree().paused, then change the scene, then unpause the player after the scene has changed, it still crashes.

:bust_in_silhouette: Reply From: denxi

Did a little more digging about raycasts and found they hold that information by design. I fixed the problem by adding a function that forces all my raycasts to update, to flush out the collision data from the old scene.`

func updateRaycasts():
    for raycast in $CollisionShape2D.get_children():
	    raycast.force_raycast_update()

Then ran it during the scene change.