Page 1 of 1

Random Enemies Spawning

Posted: Fri Nov 15, 2019 8:56 pm
by Pa0DeF0rma
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 :awesome:

Re: Random Enemies Spawning

Posted: Sat Nov 16, 2019 11:53 am
by pgimeno
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

function love.draw()
  for i = 1, #enemies do
    local enemy = enemies[i], enemy.x, enemy.y)
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.

Re: Random Enemies Spawning

Posted: Sat Nov 16, 2019 1:06 pm
by Pa0DeF0rma
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?

Re: Random Enemies Spawning

Posted: Sat Nov 16, 2019 1:47 pm
by pgimeno
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 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.

Re: Random Enemies Spawning

Posted: Sat Nov 16, 2019 2:33 pm
by Pa0DeF0rma
You don't have misundertood,it is exactly what I'm trying to say it. I get it the ideia. Thanks for your help