0 votes

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.

in Engine by (436 points)
edited by

1 Answer

0 votes
Best answer

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.

by (436 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.