[SOLVED]Having trouble spawning mobs

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.
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

[SOLVED]Having trouble spawning mobs

Post 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 212 times
Last edited by Cookie10monster on Tue Jun 30, 2015 4:06 pm, edited 3 times in total.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: [HELP]Having trouble spawning mobs

Post 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

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: [HELP]Having trouble spawning mobs

Post 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!
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: [HELP]Having trouble spawning mobs

Post 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
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: [HELP]Having trouble spawning mobs

Post by Cookie10monster »

Thanks for the replies, the info is good to know. However, this doesn't solve the original problem. Any ideas?
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: [HELP]Having trouble spawning mobs

Post 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
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: [HELP]Having trouble spawning mobs

Post 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
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: [HELP]Having trouble spawning mobs

Post by Positive07 »

So... where is the .love?
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: [HELP]Having trouble spawning mobs

Post 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
Cookie10monster
Prole
Posts: 21
Joined: Wed Apr 15, 2015 10:51 pm

Re: [HELP]Having trouble spawning mobs

Post 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!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 10 guests