Page 1 of 1

Bug trying to spawn bullets from a specific enemy position

Posted: Tue Jan 31, 2017 1:24 am
by demvive
My issue is in enemy.lua and I have commented where the bug is.

I have enemies in an arrangement, a timer counts down and it picks a random enemy to spawn the bullet from.
Global bullet timer, random enemy each time.

Seemingly at random, it crashes the game. Spawning minions might make crashes more frequent, but they happen regardless and the minion's bullet system is separate entirely right now.

I think that it is choosing an enemy that doesn't exist, but I don't know how. it chooses between 1 and the length of the enemy_spawned table.
SpaceInvadersDefence.love
(55.25 KiB) Downloaded 139 times

Re: Bug trying to spawn bullets from a specific enemy position

Posted: Tue Jan 31, 2017 9:56 am
by 0x72
demvive wrote:it chooses between 1 and the length of the enemy_spawned table.
and
demvive wrote:

Code: Select all

enemy.bullet_position = math.random(1,table.getn(bullet.spawned))
No it doesn't. It chooses between 1 and #bullet.spawned.

If #bullet.spawned > #enemy.spawned then there is a chance you'll get nil.

:)

-- edit:

But even then you could end up with nil if your table is empty! In any case you should probably check if the choice is there:

Code: Select all

local choice = enemy.spawned[enemy.bullet_position]
if choice ~= nil then
  bullet_spawn(choice.x,choice.y,0,100,1)
end
--

Not directly related to the bug but I don't see why you need store this enemy.bullet_position at all.
Maybe use a function for getting random item from a table so you never end up with a bug like that?:

Code: Select all

function sample(t) 
  local pos = love.math.random(#t)
  return t[pos], pos 
end

Re: Bug trying to spawn bullets from a specific enemy position

Posted: Tue Jan 31, 2017 5:26 pm
by demvive
Wow thanks! I don't know if I would have ever noticed that...

I'm not really concerned with optimization/good programming techniques for this game, so I've sort of just been making spaghetti code. That's why nothing is ideal/looks good haha. Thanks!

Re: Bug trying to spawn bullets from a specific enemy position

Posted: Wed Feb 01, 2017 7:26 am
by skyHights
TL;DR: You don't need the ~= nil
0x72 wrote:
demvive wrote:But even then you could end up with nil if your table is empty! In any case, you should probably check if the choice is there:

Code: Select all

local choice = enemy.spawned[enemy.bullet_position]
if choice ~= nil then
  bullet_spawn(choice.x,choice.y,0,100,1)
end
Or you could do:

Code: Select all

local choice = enemy.spawned[enemy.bullet_position]
if choice then
  bullet_spawn(choice.x,choice.y,0,100,1)
end
as if the variable choice has anything in it, it will return true if used as a boolean, for example

Code: Select all

text = "text in here"
if text then
  return true
end
while that won't actually do anything interesting if you run it, the code inside the if statement will be run, however, if you run this:

Code: Select all

text = elephant
if text then
  return true
end
the variable elephant doesn't exist so text will be nil, and the code in the if statement will not be run.