0 votes

I am loading BulletFactory.tscn via autoload singleton.

I don't understand why its instancing all the objects contained in the scene in autoload. it instances by itself and then I've got one of every bullet which are instanced, run physics, etc, which is not the behavior I want.

How do I load it globally without it instancing?

my code

in Engine by (36 points)

1 Answer

+1 vote

A scene file is loaded as a PackedSceneclass instance, which inrehits Resource class. When a scene is instanced, any child nodes will be instanced together.

You need to remove any node from an autoload scene if you don't want them to be instanced upon game starting.


Too achieve what you want, you can save all your bullet node as scene files. And use

var bullet_scene :PackedScene = preload("res://bullet01.tscn")
var bullet :Node = bullet_scene.instance()
bullet.rotation_degrees = rot
# etc.

to instantiate a bullet node.


At this point, your BulletFactory becomes an empty scene. So maybe just remove BulletFactory.tscn from AutoLoad and delete the scene file. Then add BulletFactory.gd to AutoLoad, and give it a same name as the previous scene.

A gdscript (which has to inherits Node or its subclass) as an autoload, is the same as a scene as an autoload. Just without those predefined nodes.

by (313 points)

This is really helpful thank you.

I figured out my problem.

In one of the functions, there was a blank line. It caused the interpreter to think that a chunk of code was not inside the function, and it executed it, and that spawned some instances.

It took a really long time to find this stupid simple thing.

:(

Anyway, watch your blank lines and spaces in python like languages!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.