Creation and Destruction

One of the powerful tools Spark provides to you is the ability to create and destroy objects while your game is running. With this, you have a way to avoid the prop limit while keeping a high level of detail on your objects. You just need to make sure that items are removed when they are no longer needed, and created again when they are.

=Creation=

Choosing What To Create


The [create] tile allows you to create an object in your game. You use the [create] tile by specifying what you want to create after it.

There are a few options that let you decide what to create after you place the [create] tile, so let's look at them:

in-world picker


The in-world picker allows you to create a copy of something that is in the world. You simply select the object that you want to create.

Remember: The in-world picker references a specific item in your stage. If that item no longer exists when the create tile tries to create it, the creation will fail. Items referenced with the world picker will have an orange world icon.

gallery picker
Selecting with the gallery picker allows you to select any default object in spark, or any saved assembly.



object variable
Specifying an object variable after [create] will create a copy of whatever the object variable was referencing. If the object variable is not referencing anything, [create] won't do anything.



[at position]


By default when you create an object it is placed 1.1 meter in front and 1 meter above its creator's position. However, with the [at position] modifier, you can place the object wherever you would like. [at position] accepts a vector to define the location in the world to create.

[towards]


The towards modifier specifies the location or object that the created object should be facing. If you specify an object here the created object's forward will point towards the specified object's position. Knowing at which point an object tracks its position can help you out a lot here.

Templates
Usually when you use the create tile, you want to be creating from a template. A template is basically an object that exists outside of your game for the sole reason of being created on demand. All items in the object galleries are templates, even your saved assemblies. Objects placed in your game are not templates by default, but can be set up as templates by toggling their setting at Properties-> Brain -> Template.



Note: One of the dangers of not using templates when creating is mentioned above on this page. If the item you selected with the in world picker has been destroyed before you create your new object, creation will fail. However, templates cannot be destroyed.

Advanced Tip: Though templates aren't visible in your game, they are still loaded by Spark. This causes them to use up your object count but it also provides some functionality. Templates actually keep track of variable information, and allow other objects to change that information during the game. You can also use pinned variables on templates to initialize those values. This can be useful for example if you want to create an object with a cost. You could check your template and see if the player has enough money to create the object.

When You Create
Remember that Spark runs at 30 frames a second. If you let the create line run continually, you will quickly get swamped with new items. Usually you will want to only create once, or when a condition happens. Check out the #Timing and Logic section for tiles to help define when you create.

Tracking the Created Object
It is pretty common after creating an object to want to want to update it further, or track it somehow. This can be achieved by assigning the result of the [create] tile to an object variable. For example:



At this point, your object variable will reference the created object, and you can use that variable to set additional information on the object you just created.

Another useful way to track your created objects is by putting them in an object set. You could potentially organize different types of created items into different sets.

Created Objects tile
The [created objects] tile tracks every item that was created by the current object (object running the brain). There is also a more specific, but similar version of this tiled called [projectiles], which tracks the projectiles that this object has shot until they are destroyed.

Creating assemblies
If you find yourself running into problems with the object limit, take a look at creating complex assemblies on demand. If you save an assembly containing for example the interior of a house, you can create that interior from its assembly only when the door to the house is opened, then destroy it when the player exits and the door is closed. By doing this, you can have richly detailed environments while keeping your prop count low.



Mind Your Frame Rate
It is easy to go wild with the create tile, but be careful not to create too many things at once. Spark can handle creating quite a few objects per frame without any performance impact, but it is a reasonably intense operation, so make sure to test any creation heavy stages for frame rate dips.



=Destroy=

The destroy tile is the opposite of the create tile. It is however far simpler in its use. The destroy tile accepts either an object or an object set, and all objects specified will be destroyed.



The above line will destroy all objects that were created by the object who is running that line.

Note: If you do not specify what to destroy after [destroy], you will destroy the object who is running the brain.