[SOLVED]Having trouble spawning mobs
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
[SOLVED]Having trouble spawning mobs
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!
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!
Last edited by Cookie10monster on Tue Jun 30, 2015 4:06 pm, edited 3 times in total.
Re: [HELP]Having trouble spawning mobs
You should share a .love.
Also it might be a good idea to handle the mobs differently:
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
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: [HELP]Having trouble spawning mobs
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!
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: [HELP]Having trouble spawning mobs
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...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!
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
Code: Select all
MobDelete = function (i) table.remove(Mob, i) end
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
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: [HELP]Having trouble spawning mobs
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
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
It will be a lot easier to read and fix
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: [HELP]Having trouble spawning mobs
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 folderpielago 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
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: [HELP]Having trouble spawning mobs
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)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: [HELP]Having trouble spawning mobs
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
let main.lua be just a main and not all of it lol
-
- Prole
- Posts: 21
- Joined: Wed Apr 15, 2015 10:51 pm
Re: [HELP]Having trouble spawning mobs
Whoops! It looks like it didn't upload it correctly or something. My bad. The .love in the post is the new one. Thanks!Positive07 wrote:So... where is the .love?
Who is online
Users browsing this forum: Ahrefs [Bot] and 4 guests