Page 1 of 2

[SOLVED]Having trouble spawning mobs

Posted: Fri Jun 12, 2015 8:01 pm
by Cookie10monster
Hi, I am working on a game similar to terraria and am working on mob spawning. When I set the mobs positions during the load function, they work fine, but when I run this
if love.math.random(1,1000) == 1 then
NumberOfMobs = NumberOfMobs+1
Mobs.posX[NumberOfMobs] = 1000
Mobs.posY[NumberOfMobs] = 300
end
It creates the mob, which is drawn, takes damage when you hit it, and falls. However, it does not collide with blocks, or, if it does, it collides with them several blocks lower than it should. I have done some testing, and found that I am able to spawn in mobs with other ways. For example, if I run this:

if NumberOfMobs<=10 then
NumberOfMobs = NumberOfMobs+1
Mobs.posX[NumberOfMobs] = 1000
Mobs.posY[NumberOfMobs] = 300
end
everything works fine. Any ideas on why this is? Thank you!


Floating Islands 0.3.2.love
Here is the .love
(509.17 KiB) Downloaded 213 times

Re: [HELP]Having trouble spawning mobs

Posted: Fri Jun 12, 2015 11:13 pm
by s-ol
You should share a .love.

Also it might be a good idea to handle the mobs differently:

Code: Select all

-- spawn new mob:
table.insert(mobs, {posX = 1000, posY = 300})

-- draw mob
for _,mob in ipairs(mobs) do
  drawMobAt(mob.posX, mob.posY)
end

Re: [HELP]Having trouble spawning mobs

Posted: Fri Jun 12, 2015 11:25 pm
by Cookie10monster
S0lll0s wrote:You should share a .love.

Also it might be a good idea to handle the mobs differently:

Code: Select all

-- spawn new mob:
table.insert(mobs, {posX = 1000, posY = 300})

-- draw mob
for _,mob in ipairs(mobs) do
  drawMobAt(mob.posX, mob.posY)
end

Shared the .love, and you're right, I probably should handle the mobs a little bit more efficiently. I should probably know this by now, but what is the difference between doing this

for i = 1#Mobs.posX do
DrawMob(Mobs.posX,Mobs.posY)
end

and using ipairs? Is ipairs more efficent? Thanks!

Re: [HELP]Having trouble spawning mobs

Posted: Sat Jun 13, 2015 3:29 am
by Positive07
Cookie10monster wrote: Shared the .love, and you're right, I probably should handle the mobs a little bit more efficiently. I should probably know this by now, but what is the difference between doing this

and using ipairs? Is ipairs more efficent? Thanks!
Well deleting the monster is way more complicated, you have to delete all the data from the different tables... and tracking all that stuff is not nice really... You could for example delete posX and forget to delete posY and get all weird positions (and hell that is a difficult bug to track), or you could delete a mob you didnt intend to delete...

For example deleting a mob as you have now will be something like

Code: Select all

MobDelete = function (i)
    table.remove(Mob.posX, i)
    table.remove(Mob.posY, i)
    --...
end
Instead if you had a mob table with mobs you could do

Code: Select all

MobDelete = function (i) table.remove(Mob, i) end
And this deletes ALL the properties for that Mob

Also creation is simpler since you can define all the properties in a single table instead of in different ones

Code: Select all

--Now
MobNew = function (x, y)
    local i = #Mob.posX + 1
    Mob.posX[i] = x
    Mob.posX[i] = y
    --etc
end
--With the proposed change
MobNew = function (x, y)
    table.insert(Mob, {posX = x, posY = y})
end
Still this is code design and personal preferences but by expirience we can tell you this is better, and cleaner

Hope this was helpful

Re: [HELP]Having trouble spawning mobs

Posted: Sun Jun 14, 2015 12:28 am
by Cookie10monster
Thanks for the replies, the info is good to know. However, this doesn't solve the original problem. Any ideas?

Re: [HELP]Having trouble spawning mobs

Posted: Sun Jun 14, 2015 4:58 am
by pielago
can you make different files .lua inside your main folder example: main.lua , player.lua, enemy.lua, map.lua..etc
It will be a lot easier to read and fix

Re: [HELP]Having trouble spawning mobs

Posted: Sun Jun 14, 2015 4:32 pm
by Cookie10monster
pielago wrote:can you make different files .lua inside your main folder example: main.lua , player.lua, enemy.lua, map.lua..etc
It will be a lot easier to read and fix
You're right, its a lot more organized with different things in different files. Not everything is in its own .lua, but I hope this helps. The code for the mob is in the mobs.lua folder. The two original mobs are created in lines 32-52 of the variables.lua folder

Re: [HELP]Having trouble spawning mobs

Posted: Mon Jun 15, 2015 3:00 am
by Positive07
So... where is the .love?

Re: [HELP]Having trouble spawning mobs

Posted: Mon Jun 15, 2015 4:55 am
by pielago
just make others ( .lua) files than upload the (.love) than community can help you solve it
let main.lua be just a main and not all of it lol

Re: [HELP]Having trouble spawning mobs

Posted: Mon Jun 15, 2015 7:29 pm
by Cookie10monster
Positive07 wrote:So... where is the .love?
Whoops! It looks like it didn't upload it correctly or something. My bad. The .love in the post is the new one. Thanks!