Page 1 of 1

Help: Spawning multiple enemies at set locations

Posted: Mon Mar 13, 2017 8:29 pm
by reynne
Hello.

I am trying to learn love2d, and decided to follow a youtube tutorial (https://www.youtube.com/watch?v=FeLljv5clnw&t=689s). However, I cannot get multiple enemies to spawn, and I am pretty sure I am following the instructions correctly. Please take a look at my code and let me know what I am doing wrong. Thank you.

Code: Select all

enemy {}
enemies_controller = {}
enemies_controller.enemies = {}

function enemies_controller:spawnEnemy(x, y)
  enemy = {}
  enemy.x = x
  enemy.y = y
  enemy.bullets = {}
  enemy.cooldown = 0
  enemy.speed = 6
  table.insert(self.enemies, enemy)
end

enemy = {}
enemies_controller:spawnEnemy(0, 0)
enemies_controller:spawnEnemy(100, 100)

function love.update(dt)
  for _,e in pairs (enemies_controller.enemies) do
    e.y = e.y + 1
  end
end

function love.draw()
  --draw enemy
  love.graphics.setColor(255, 0 , 0)
  for _,e in pairs(enemies_controller.enemies) do
    love.graphics.rectangle("fill", e.x, e.y, 80, 20)
  end
end 

Re: Help: Spawning multiple enemies at set locations

Posted: Tue Mar 14, 2017 2:01 am
by mtdev
Line 1 looks like it is missing the =

Code: Select all

enemy = {}
then it worked for me.

Re: Help: Spawning multiple enemies at set locations

Posted: Tue Mar 14, 2017 12:42 pm
by reynne
That was a typo from retyping the code a few times. However, I put in two calls to the spawnEnemy, but it is still not spawning more than one. I am thinking that it is just drawing it with the draw function, but somehow the logic for the spawnEnemy function is not working. I can get one to spawn, but I can't get the second one to appear.

Re: Help: Spawning multiple enemies at set locations

Posted: Tue Mar 14, 2017 12:48 pm
by reynne
Oh, sorry. I figured it out I think. It had something to do with my directories in my ide. It was running a different version of my main.lua which did not have any of the changes to spawn the enemies. Thank you for the help.

Re: Help: Spawning multiple enemies at set locations

Posted: Tue Mar 14, 2017 6:36 pm
by DanielPower
I'm glad you got it working, but I would like to add that you should not be using a global variable for enemy.

Code: Select all

-- enemy = {} -- Not necessary
enemies_controller = {}
enemies_controller.enemies = {}

function enemies_controller:spawnEnemy(x, y)
  local enemy = {} -- This variable can be local since we can access it from `enemies_controller.enemies` later
  enemy.x = x
  enemy.y = y
  enemy.bullets = {}
  enemy.cooldown = 0
  enemy.speed = 6
  table.insert(self.enemies, enemy)
end

-- enemy = {} -- I guess you're using this to clear the enemy variable after you added things to it in spawnEnemy.
-- This is no longer necessary since enemy is now local, and disappears as soon as you exit the spawnEnemy function.

enemies_controller:spawnEnemy(0, 0)
enemies_controller:spawnEnemy(100, 100)

function love.update(dt)
  for _,e in pairs (enemies_controller.enemies) do
    e.y = e.y + 1
  end
end

function love.draw()
  --draw enemy
  love.graphics.setColor(255, 0 , 0)
  for _,e in pairs(enemies_controller.enemies) do
    love.graphics.rectangle("fill", e.x, e.y, 80, 20)
  end
end 
By using a local variable for enemy, the variable will only exist when you actually need it to, and won't get in the way of other places where you might want to use that name. For example, later on you may want to have more than one type of enemy. It's just a lot cleaner to use local variables when appropriate.

There is also a performance boost when using local variables, but it is rather small, and not worth worrying about unless your project is quite large.