## Random Enemies Spawning

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Pa0DeF0rma
Prole
Posts: 5
Joined: Fri Nov 15, 2019 8:48 pm

### Random Enemies Spawning

Hello guys, i've got a project of a topdown-shooter game that I'm developing, but I dont know how to spawn random enemies in random places of the screen. I have seen some other topics about spawning objects but none of these helped me. Anybody could help me out? Thanks for reading

pgimeno
Party member
Posts: 1943
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

### Re: Random Enemies Spawning

Hi Pa0DeF0rma, welcome to the forums. You can use an array of enemies. Each element in the array would contain information about the corresponding enemy, including the coordinates and maybe some other stuff that you need to keep track of, like the remaining HP or whatever. In love.draw, you would draw every entry in the array.

To spawn an enemy, you would just add an element to the array. To delete an enemy, you would remove it from the array.

Deletion is not as simple as it looks. You better check which ones you need to delete in reverse order, otherwise you could skip enemies.

Example of a spawning function:

Code: Select all

local enemies = {}  -- empty to start with

local function spawnEnemy(x, y)
-- Create an enemy
local enemy = {x = x, y = y}

-- Add it to the array
enemies[#enemies + 1] = enemy
end

function love.draw()
for i = 1, #enemies do
local enemy = enemies[i]
love.graphics.draw(enemyImage, enemy.x, enemy.y)
end
end

Now, just use the function spawnEnemy, passing it a random position.

This solves the spawning part. With bullets, you could do the same. For deletion, typically you need to check a collision between every bullet and every enemy.

Pa0DeF0rma
Prole
Posts: 5
Joined: Fri Nov 15, 2019 8:48 pm

### Re: Random Enemies Spawning

Thanks, pgimeno! I understanded about spawning in random places, but if I want to spawn random enemies, could I just add a ID for each one and in the hour of calling the function spawnEnemy check if the random number between the ID interval is the same as one ID of a random enemy. Does that sound right?

pgimeno
Party member
Posts: 1943
Joined: Sun Oct 18, 2015 2:58 pm
Location: Valencia, ES

### Re: Random Enemies Spawning

I'm not sure I get what you mean. Do you mean random types of enemies? If so, we're delving into object orientation territory now, but if you want to keep it simple, you can have an array with template values for the different types of enemies (the structure I mentioned above should then include info about the type of enemy in question). To create a new enemy, you would select a random type from 1 to the length of the array, then copy the structure into a new enemy. Don't just assign it, that is, don't just do 'newEnemy = enemyTemplates[love.math.random(1, #enemyTemplates)]', because that creates a reference to the original, and every enemy would have the same properties (viz. the same position, etc.). Make a copy instead; use for example 'deepcopy' in http://lua-users.org/wiki/CopyTable like this: 'newEnemy = deepcopy(enemyTemplates[love.math.random(1, #enemyTemplates)])',

With an object oriented paradigm, you could have an array of classes, each implementing one type of enemy, then instantiate a class of a random type (which you would choose by selecting a random number as above) every time you want to spawn one.

If I have misunderstood, please explain what you're trying to accomplish.

Pa0DeF0rma
Prole
Posts: 5
Joined: Fri Nov 15, 2019 8:48 pm

### Re: Random Enemies Spawning

You don't have misundertood,it is exactly what I'm trying to say it. I get it the ideia. Thanks for your help

### Who is online

Users browsing this forum: Google [Bot] and 6 guests