+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.

asked Mar 24, 2016 in Engine by Gokudomatic2 (689 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.

answered Mar 26, 2016 by batmanasb (858 points)
edited Mar 26, 2016 by batmanasb
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 How to use this Q&A? before posting your first questions.