Spawning multiple objects and characters

How to create multiple objects or characters in your game using kode

Although you can place all the characters and objects you need in your world from the start, it is not always advisable. The number of objects that are active in your world at any one time will affect the games processing needs, and can slow the game down, which is called lag. Also, if you placed lots of attacking enemies all at the same time your player could be inundated and unable to fight them all off, so instead you create your enemies in waves.

Creating multiple objects is called spawning. This tutorial will show you different methods of doing this.

Method 1: Duration timers

To create 1 object you create a single frame WHEN side, like this:

WHEN [once] DO [create][goblin]

To create multiple objects you can increase the amount of frames the line runs, each frame will create one object like this.

WHEN [duration timer][3][in frames] DO [create goblin]

This will create 3 goblins.

Method 2: For each of

WHEN [once] DO
…WHEN [for each of][3] DO [create][goblin]

Don’t forget the single frame WHEN side  (eg. once, or started to)- or it will create 3 goblins continuously without stopping. Here is a different example:

WHEN [numvar:goblin] [<] [3]
…WHEN [for each of][1]
…/… DO [create] [goblin]
…/… [numvar:goblin] [incremented by] [1]

Specify in the first line how many objects you need and it will stop when it has made that number.

Enemy Spawners

Recommend you keep the amount of active enemies to under 30 as it can affect processing power at levels higher than that.

Simple spawner – 1-30 enemies, single wave

This will create 1 group of enemies, bunched together, that will attack in unison.

Place a logic cube where you want your enemy to spawn (spawn cube) and turn the brain is active to false in Edit/Properties/Brain.
Place another logic cube with a trigger zone where your player can set off the enemy spawning.

In the trigger zone cube:

WHEN [started to][in trigger zone][Player] DO [IWP:spawn cube][brain is active][=][true]

In the spawn cube:

WHEN [duration timer][5][in frames] DO [create][goblin][at position][position]

Multiple wave spawner
This will create a wave of enemies every time all the enemies of the previous wave are dead

In the spawn cube:

WHEN [global][numvar:enemies][=][0]
…WHEN [duration timer][5][in frames] DO [create][IWP: goblin][at position][position]

In the enemy brain
WHEN [Once] DO [global][numvar:enemies][incremented by][1]
WHEN [started to][is dead] DO [global][numvar:enemies][decremented by 1]

If you want to make this a finite number of waves change to this:

WHEN [numvar:wave][<][4]
…WHEN [global][numvar:enemies][=][0]
…/…WHEN [duration timer][5](the number of enemies you want)[in frames] DO [create][IWP: goblin](or other enemy)[at position][position]
…/…/…WHEN DO [numvar:wave][incremented by][1]

This will make 4 waves only

Spawner that makes sure there are always the same number of enemies in the world

enemy brain as above

In the spawn cube:

WHEN [global][numvar:enemies][<][10]
…WHEN [duration timer][1] [in frames] DO [create][IWP: goblin](or other enemy)[at position][position]

This will create a goblin only if there are less than 10 goblins in the world. So as your player kills a goblin, another will be spawned.

Multiple enemy types wave spawner

Enemy brains have Kode as above

Create a random number generator on page 2 of your spawn cube

WHEN [page entered] DO [numvar:enemytype][=][random number][1][to][3][as integer]
WHEN [numvar:enemytype][=][1] DO [objvar:enemy][=][Goblin]
WHEN [numvar:enemytype][=][2] DO [objvar:enemy][=][Zombie]
WHEN [numvar:enemytype][=][3] DO [objvar:enemy][=][Wolf]

page 1
WHEN [numvar:wave][<][4]
…WHEN [global][numvar:enemies][=][0]
…/…WHEN DO [call page][2]
…/…WHEN [duration timer][5](the number of enemies you want)[in frames] DO [create][objvar:enemy][at position][position]
…/…/…WHEN DO [numvar:wave][incremented by][1]

This will create 4 waves of enemies which will be random types.

Comments are closed.

Website Powered by WordPress.com.

Up ↑

%d bloggers like this: