I created a Flappy Bird clone in Godot 4 using a tutorial on YouTube. However, today I encountered a problem with optimizing the game. On mobile devices, it runs with a significant delay because when the pipes go out of view, they don't disappear and continue to exist, putting a load on the device. I know I need to use the VisibleOnScreenNotifier2D node, but I can't just add this node and the check in the script for the pipes because it instantly triggers an error: "Invalid access to property or key 'position' on a base object of type 'previously freed'." and the game crashes. How can I fix this and improve the optimization of my game? Please help me in this difficult situation! Thank you very much in advance!
link tutorial -

    Scel I can't just add this node and the check in the script for the pipes because it instantly triggers an error: "Invalid access to property or key 'position' on a base object of type 'previously freed'.

    There's no need for a check in the script. Connect the VisibleOnScreenNotifier2D's screen_exited signal to a function that queue-frees the pipe.

    The Dodge the Creeps tutorial has an example:

    The last piece is to make the mobs delete themselves when they leave the screen. Connect the screen_exited() signal of the VisibleOnScreenNotifier2D node to the Mob and add this code: ...

    https://docs.godotengine.org/en/4.3/getting_started/first_2d_game/04.creating_the_enemy.html#enemy-script

    If there's a possibility that the pipe may be queue-freed by other code before it leaves the screen, use is_instance_valid to check that the pipe still exists before queue-freeing it.

    this method is not working, because when you add the VisibleOnScreenNotifier2D node and connect the screen_exited signal for pipes, this causes the project to crash and an error:
    Invalid access to property or key 'position' on a base object of type 'previously freed'.

      Scel this causes the project to crash

      Does the crash occur when a pipe moves off screen and is queue-freed by the screen_exited signal handler?

      If so, my guess is that there's some code that's still trying to access the deleted pipe's position property. You need to locate that code and stop it from doing that. Does the error message tell you the location of the code that causes the error?

      I don't know what your code looks like, but is_instance_valid is a common solution for that kind of problem.

      • Scel replied to this.

        DaveTheCoder thanks for trying to help, but I was able to find my own way to solve my problem, which helps to clear the array of pipes after the pipe went out of the field of view of the player's camera

          Scel is your "screen_exited" signal attached to your main scene or to the pipes themselves? The pipes will give errors if something else is handling them and they suddenly get freed. They should free themselves when they go offscreen, just connect the screen_exited() signal in the pipes themselves. Then at ready(), have those pipes connect their signal to whatever code you are using to clear your array.