The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+2 votes

Hello,

I'm implementing a game where the player shoot lot of bullets per sec. Those bullets are dynamic (lot of different kinds of bullets) and can become a bit complex (with multiple children). What is the best way to create very quickly those bullets in GDScript? Knowing that there are some conditions to know which kind of bullet to shoot, it wouldn't be performant to go through those conditions every time.
I've seen there's a "duplicate" function in the Node. Is it more or less efficient than instancing every time?

Thanks in advance for your answers.

in Engine by (703 points)

There is a "bullet shower" demo BTW...

Thanks, Bojidar. Alas I already use the trick in this demo, which is to use an Area instead of a RigidBody. What I need is a way to quickly create dozen instances of the same scene without having to reconfigure it every time. For the moment, I guess using a pool of pre-created instances would be the most efficient. I created them in advance and simply add them in the main scene when I need some. And I refill later the pool in a background process.

1 Answer

+1 vote

Have you tried preload instead of load so that the scene is loaded at the start. For my gore system I preload a gore scene which is a RigidBody2D with a script and other nodes attached. Then I spawn between 40 and 120 instantly when my character loses a life or dies. It seems to work fine. So something like:

var goreNode = preload("res://character/gore.tscn")

func spawnGore(amount):
    #spawn gore
    for i in range(amount):
        var gore = goreNode.instance()
        gore.setPos(get_global_pos()+Vector2(randf()*6-3,randf()*6-3))
        objects.add_child(gore)

Decided to run a test and added "spawnGore(1)" into _fixed_process and it only began dropping frames after it reached about 2000 objects. But it should be a lot better for Area2D nodes. Made a low res gif of the results here.

by (855 points)
edited by
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.