Spawner2D

Inherits: Node2D < CanvasItem < Node < Object

Automatically instantiate nodes from scenes in 2D.

Description

Spawner allows to conveniently instantiate nodes from scenes and scripts automatically into the SceneTree. In order to use it, you need to set resource first. Once Spawner2D is added to the SceneTree, it will start to create new instances immediately.

The speed and number of nodes instantiated can be controlled by modifying rate, time step, and limit. For example, if rate = 10, then Spawner2D will instantiate 10 nodes per second, or you could modify time step and instantiate 10 nodes per minute if step = 60. If you only need to instantiate 10 nodes and stop, then you’d need to set limit = 10. The delay can be configured to create spawn waves and sequences.

The modify_* properties allow to set transform of spawned nodes according to Spawner2D’s own transform. Depending on the value of local, either local or global transform is modified of the nodes that are spawned by Spawner2D.

Methods

Node spawn ( )

Signals

  • finished ( )

Emitted when the current number of spawned nodes equals to limit.


  • node_spawned ( Node node )

Emitted when a new node is spawned automatically when enabled is set to true. The signal is not emitted when spawn is called manually via code.

Enumerations

enum ProcessMode:

  • PROCESS_PHYSICS = 0 — Update the spawner during the physics step at each frame (fixed framerate processing).
  • PROCESS_IDLE = 1 — Update the spawner during the idle time at each frame.

Property Descriptions

Default 0.0
Setter set_delay(value)
Getter get_delay()

If delay > 0, then spawning is postponed, otherwise instantiates nodes immediately when setting enabled to true. Measured in seconds.


Default true
Setter set_enabled(value)
Getter is_enabled()

Starts or stops the spawn process. The state (number of currently spawned objects, spawn time etc.) is reset at all times.


Default 0
Setter set_limit(value)
Getter get_limit()

Limits the number of nodes instantiated. If limit == 0, the number of nodes instantiated is unlimited.


Default false
Setter set_local(value)
Getter is_local()

If true, spawned nodes will inherit transform from parent node, otherwise transform will not be pushed from parent by setting the spawned node as a top-level using CanvasItem.set_as_toplevel. Disabled by default, as it may cause physics simulation errors if spawned nodes inherit from RigidBody2D.


  • bool modify_position
Default true
Setter set_modify_position(value)
Getter is_modifying_position()

If true, modifies the spawned node’s position upon spawning from Spawner2D.


  • bool modify_rotation
Default true
Setter set_modify_rotation(value)
Getter is_modifying_rotation()

If true, modifies the spawned node’s rotation upon spawning from Spawner2D.


Default true
Setter set_modify_scale(value)
Getter is_modifying_scale()

If true, modifies the spawned node’s scale upon spawning from Spawner2D.


Default 1
Setter set_process_mode(value)
Getter get_process_mode()

Processing mode allows to configure whether the spawn method is called during idle or physics frame, only applies when enabled is set to true. See ProcessMode.


Default 1
Setter set_rate(value)
Getter get_rate()

The number of nodes instantiated per time step.


Setter set_resource(value)
Getter get_resource()

The resource from which a new node is instantiated. Accepts PackedScene and Script only. If the resource is a Script, make sure that the constructor does not have any required parameters, otherwise the instantiation may fail.


Default NodePath(".")
Setter set_spawn_path(value)
Getter get_spawn_path()

The nodes are spawned directly as a child of Spawner2D by default. Setting this property allows to customize the destination by specifying a different parent node.


Default 1.0
Setter set_step(value)
Getter get_step()

The rate number of nodes will be spawned each time step. Measured in seconds.

Method Descriptions

Forces an instantiation of a new Node from a resource, even when enabled is set to false. By default, the new node is added as a child to this one, unless spawn_path is customized.

Note: the node_spawned won’t be emitted by calling this function, so if you need to spawn multiple instances from the same resource, you can do so safely during signal emission. If you do need to notify other objects about this event, you can emit the node_spawned signal manually.