Please Help Me Understand The Relationship Between Autoload And Main Scene

I just spent a couple hours going nuts. Why? Because a scene I had set as my main scene was also an autoload. Two copies of a node I had in that scene were each running the same function. I was going insane and slamming my fist on a table while my eyes were going buggy because I couldn’t fathom why my god damn function was creating two copies. SO… when I figure what’s going on I say to myself “ah, OK, problem solved. All I have to do is remove my main scene from Autoload.” But guess what? I’ll tell you what! THE MAIN SCENE DOESN’T AUTOLOAD!!! What am I missing here? What is the logic of this? I’m sure I’m missing something here and some kind person will explain it to me. What would cause a scene to not autoload if it’s only a main scene but autoload twice if it’s a main scene and an autoload?

Well, technically you could said the Main Scene itself is sort of a scene autoload. Except is not a global.

Autoloads are scenes and scripts (loaded as a node) that loads when the game starts. They will be siblings of the Main Scene withing the scene tree.

Autoloads are also globals and can easily be accessed trough any script using its name, unlike the main scene.

I made a video about how autoloads can be anything. If you are interested.

2 Likes

Thanks much. You said a main scene isn’t global. I assume this is why a variable in the main scene isn’t being read by other scripts, thus an error is being thrown and the engine stops and the main scene doesn’t get loaded. I’ve worked around this by creating a node called “stupidMainScene” and making that my main scene and making my actual main scene an autoload so it’s variables get read. I’m curious as to why this would be. Why wouldn’t a main scene be global. Maybe “first scene” would be a better descriptor.

2 Likes

Nothing is autoloaded by default because of the performance cost of global variables.

2 Likes

What kind of performance cost are we talking about? If I have this function on an autoload is it a cost worth considering? Is the cost mostly memory or compute or both?

    func calculate_polygon_size(polygon: CollisionPolygon2D) -> Vector2:
    	
            var min_x = polygon.polygon[0].x
	var max_x = polygon.polygon[0].x
	var min_y = polygon.polygon[0].y
	var max_y = polygon.polygon[0].y
	
	for point in polygon.polygon:
		min_x = min(min_x, point.x)
		max_x = max(max_x, point.x)
		min_y = min(min_y, point.y)
		max_y = max(max_y, point.y)
	
	return Vector2(max_x - min_x, max_y - min_y)

So, your main scene is is an empty scene? :rofl: :rofl: :rofl: :joy:
You should consider running queue_free() on “stupidMainScene”. I mean, might as well end its misery (and free whatever resources it’s using to hangout).

2 Likes

I was thinking the same thing.

1 Like