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.
Bug trying to spawn bullets from a specific enemy position
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Bug trying to spawn bullets from a specific enemy position
anddemvive wrote:it chooses between 1 and the length of the enemy_spawned table.
No it doesn't. It chooses between 1 and #bullet.spawned.demvive wrote:Code: Select all
enemy.bullet_position = math.random(1,table.getn(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
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!
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
TL;DR: You don't need the ~= nil
as if the variable choice has anything in it, it will return true if used as a boolean, for example
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:
the variable elephant doesn't exist so text will be nil, and the code in the if statement will not be run.
Or you could do: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
Code: Select all
local choice = enemy.spawned[enemy.bullet_position]
if choice then
bullet_spawn(choice.x,choice.y,0,100,1)
end
Code: Select all
text = "text in here"
if text then
return true
end
Code: Select all
text = elephant
if text then
return true
end
Learning to Löve.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 5 guests