Bug trying to spawn bullets from a specific enemy position

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
demvive
Prole
Posts: 3
Joined: Tue Jan 31, 2017 12:18 am

Bug trying to spawn bullets from a specific enemy position

Post 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 132 times
User avatar
0x72
Citizen
Posts: 55
Joined: Thu Jun 18, 2015 9:02 am

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

Post 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
demvive
Prole
Posts: 3
Joined: Tue Jan 31, 2017 12:18 am

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

Post 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!
User avatar
skyHights
Citizen
Posts: 78
Joined: Mon Aug 25, 2014 12:14 pm

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

Post 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.
Learning to Löve.
Post Reply

Who is online

Users browsing this forum: No registered users and 141 guests