What is cached when I load a scene?

Godot Version

4.4

Question

This is my first ever experience with godot, and it is a painful one. I’m trying to create a missile command clone. Basically I have these scenes with some scripts for each: Game, Missile, Explosion, Crosshair.

I’m instantiating the explosion scene from the missile scene. This happens when the missile reaches it’s target or detects another explosion. The only line I need to change for the project to WORK AS INTENDED is this one:

from:
var explosion = load("res://Explosion.tscn").instantiate() as Node2D
to
var explosion = ResourceLoader.load("res://Explosion.tscn", "", ResourceLoader.CACHE_MODE_IGNORE_DEEP).instantiate() as Node2D
What exactly is the difference between these two? What is cached? If I use the first one it looks like I have multiple references to the same node in memory.

The full source code is here: GitHub - kudorgyozo/missile-command-godot(luckily it’s just a hobby project)

here’s a video: https://www.youtube.com/watch?v=gGfr_b1ZxdU

What is cached?

That’s easy: Everything.

Well, all resources, that is. Nodes in the scene tree aren’t resources, and separate instances of them will be created when calling .instantiate().

But all of the resources that are referenced through the instantiated nodes will be cached, and future calls to .instantiate() will just use the cached resources.

You can force a resource to be duplicated into a scene by using its resource_local_to_scene property.

Specifically, in your case:

It looks like Explosion.tscn has a CollisionShape2D node, which, in its shape field, references a CircleShape2D resource.

That CircleShape2D is what is getting cached and shared.

So, setting the shape’s Local to Scene property in the inspector should fix your problem. (Again, to clarify: this property is on the CircleShape2D resource nested in the shape proeprty, not on the CollisionShape2D itself.

(edited to add a screenshot:)

1 Like

Thank you very much. This is exactly what I was looking for.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.