From what I understand, you can load, say, a particular scene only once, then instance it several times if you wish. This means that not every single copy takes up space of the RAM, but rather it is on the CPU or the graphics card to do the job... is that correct? If so, does instancing an object using the drag-and-drop method in the editor have the same effect as via code?

I know this might be a bit deep, but I need to understand this to have better confidence in trying to optimize my game (I'm a beginner btw).

  • ThinKing_2005

    ThinKing_2005 Just one more thing, does instancing take up less RAM space than loading?

    "Loading" something is, itself, the act of moving it from disk to RAM. Remember that, to create a new instance of a scene at runtime, you must first load it in some way (load, preload, etc). This gives you a PackedScene, which carries a scene's resources (code, sprites, etc).

    And as mentioned in other responses above, when you instance something from a PackedScene, you aren't actually copying all of its resources 1:1 into RAM. Rather, the extra space taken up is mostly just the scene's state, which consists of all of its member variables, along with all of its children and their member variables. So, in most cases, the overhead is relatively small, unless you are instancing tens of thousands of nodes or something like that.

I think where you are getting confused is in understanding the separation of roles between the disk, RAM, and the CPU.

The "disk" and RAM are storage devices, while the CPU does computation. Thus, the CPU doesn't really "store" anything - rather, it calculates your game's state by running your code. RAM is what actually remembers your game state between frames, which means it has to store every instance of every scene in your game.

The difference between the disk and RAM is that the disk is for long-term storage (your computer's file system, which is where your game lives), while RAM is for short-term storage. Getting stuff in and out of RAM is much faster than getting it in and out of the disk, which is why the resources in a game are first "loaded" into RAM. This is what happens when you load a scene in GDScript - it gets pulled out of the disk and into RAM, where it can then be instanced. But here's the thing - every instance you create also has to be stored on RAM, because RAM is the only thing that can "remember" your game state between frames (the CPU just performs calculations). So every instance in your game - whether you instantiated it at runtime or if you drag and dropped it in the editor - takes up space in RAM.

While RAM space is something important to keep in mind, you also want to take care not to strain the CPU unnecessarily. This can happen if your code is inefficient - for example, by updating your UI in every frame using _process() rather than only updating it when something changes (this alone won't cause any serious CPU strain, but if dozens/hundreds of nodes abuse _process() in this way, it can cause trouble).

To summarize - having thousands upon thousands of nodes/scenes can overload RAM, while running a ton of inefficient or complex code can overload the CPU.

    That is generally true, but it depends how the game is structured and also how the engine is optimized. For example, if you have 10 enemies with a sprite animation, the sprite graphic PNG does not necessarily need to be loaded 10 times into RAM. If the engine was properly optimized, it would load the image once, and the other instances could use a pointer to reference the same data in memory. Though each instance would need some of it's own memory, because obviously they would be unique (e.g. not all on the same frame of animation) but I doubt the overhead is that high unless we are talking about hundred or thousands of instances. Same is true for scripts, or other resources that can be shared.

      cybereality Thank you very much for the answer!
      Just one more thing though, is Godot Engine properly optimized in this aspect? Does it use a "pointer" to the object to use less RAM space when instanced? Is it the same case when using the drag-and-drop mechanism provided by the engine?

      I hope I'm not asking too many questions at once.

      JoelRummel Thank you very much for the answer!
      Just one more thing, does instancing take up less RAM space than loading? Is it something dependent on the Godot Engine?

        You probably want to save a branch of the tree as a scene (you can right click on the left) that way it will share the items inside. I don't know for sure how it works in memory, but that would be my assumption.

          cybereality My question was basically whether Godot engine actually uses the method of "pointer" you talked about, because...

          cybereality If the engine was properly optimized

          I understand that Godot engine is not necessarily optimized to use less RAM for instancing? If you're not sure, how can I know?

          ThinKing_2005

          ThinKing_2005 Just one more thing, does instancing take up less RAM space than loading?

          "Loading" something is, itself, the act of moving it from disk to RAM. Remember that, to create a new instance of a scene at runtime, you must first load it in some way (load, preload, etc). This gives you a PackedScene, which carries a scene's resources (code, sprites, etc).

          And as mentioned in other responses above, when you instance something from a PackedScene, you aren't actually copying all of its resources 1:1 into RAM. Rather, the extra space taken up is mostly just the scene's state, which consists of all of its member variables, along with all of its children and their member variables. So, in most cases, the overhead is relatively small, unless you are instancing tens of thousands of nodes or something like that.

          cybereality Well, looks like Godot Engine is very optimized (of course, why doubt that)! Thank you very much for your help!

          #This is me explaining how I got the answer...
          "I made a project where when pressing the "load button" the same resource (a scene with 4k pic) is loaded for a different variable, and pressing the "instance button" will instance the first variable created. Aaaand, the ONLY time the "max static memory" or "max dynamic memory" increase in value (shown under monitor tab) is the FIRST time the "load button", other than that, the memory remained basically constant. This means Godot never loads something more than once at the same time. This is kind of reassuring".
          #End of explanation. Link to project (if anyone is interested):