Cira RB(CiraRB)

Preload scene before add_line()?

I noticed we're preloading the scene everytime we call add_line() function. Doesn't that defeat the purpose of preloading? Or is it handled by the preload() function itself? Could you explain a bit more how the preload() function works?

  • Nathan Lovato replied

    We briefly mention it in the lesson:

    It’s a special function that tells Godot to load the scene once upon launching the game and later keep it in memory. That way, we can quickly create new instances of it.

    The loaded scene is kept in memory, so it's not like you're loading it every time the code runs. You can think of the call to preload() as being "replaced" in your code. The first time the GDScript code compiler reads it, it's going to load the scene, then it keeps the loaded scene around, and every time this line executes, Godot has direct access to the loaded scene.

    I hope it makes sense. If not, please let me know!

    2 loves