In response to Green Lime
Green Lime wrote:
Hmm couldn't you just do a 100mobs : 1 loop O.o

Exactly. That's the more efficient way to do it.
In response to SSJ2GohanDBGT
Ok, Sorry I jumped at you then.
In response to Crispy
But each Enemy I have coded does something different >_>..
Also, to make all the mobs do 1 loop instead...would I just run the code of the base mob?
mob
Enemy
New()
..()
spawn(10) src.Search()
proc
Search()
for(var/mob/Player/M in oview(10))
if(istype(src,/mob/Enemy/Monster_1))
//Do some stuff.
if(istype(src,/mob/Enemy/Monster_2))
//Do some other stuff.
Monster_1
Monster_2
In response to ITG Master
Meh Bump

In response to ITG Master
You've got the wrong end of the stick, I'm afraid. =) That will still produce 1 loop per mob. Also, you can still have each mob behaving differently.

In its simplest (and crappiest) form, a global mob AI loop generally looks something like this:

world/New() // When the game starts
..() // This line is important. =)
spawn() mobAIloop()

proc/mobAIloop()
for (var/mob/Enemy/E in world)
E.AI()
spawn(10) mobAIloop()

mob/Enemy/proc/AI()
// Do stuff

mob/Enemy/Monster_1/AI()
// Do stuff specific to Monster_1

mob/Enemy/Monster_2/AI()
// Do stuff specific to Monster_2


Problems with the above snippet:
  • It loops through the entire world looking for mobs every second. This is bad. A better way is to keep a list of all /mob/Enemy objects, and loop through that instead. The most efficient way to do so is to make a global variable to hold the list, add src to it in mob/Enemy/New(), and remove src from it in mob/Enemy/Del(). Remember to call ..() in both New() and Del(); also, any lines after ..() in Del() will not be executed, so remove src from the list before the ..() line.
  • It moves all mobs at once. You may or may not like this; regardless, this means you'll get CPU usage spikes once every second, as opposed to keeping it relatively constant, which is arguably better. To fix this, you could wait for less time (say, 1/10 of a second) and only trigger a proportion of the mobs each time you do so (say, 1/10 of the mobs).
I'll leave solving these problems as exercises for you. ;-)
In response to Crispy
It loops through the entire world looking for mobs every second. This is bad. A better way is to keep a list of all /mob/Enemy objects, and loop through that instead. The most efficient way to do so is to make a global variable to hold the list, add src to it in mob/Enemy/New(), and remove src from it in mob/Enemy/Del(). Remember to call ..() in both New() and Del(); also, any lines after ..() in Del() will not be executed, so remove src from the list before the ..() line.

Better yet rather than in New() and Del() only do it when a mob needs to be active like when a player enters the area or gets close. No point in having a mob do stuff if no one is around to see or be affected.
In response to ITG Master
ITG Master wrote:
I just have never had to deal with loops before.

Amazing... Really, never? I've rarely coded anything that didn't involve some sort of a loop.

Absolute Zer0
Page: 1 2