ID:1664416
 
(See the best response by Ter13.)
Code:

Ussually when i do AI i do something like this

while(src)
for(var/mob/m in view(2))
do this
sleep(2)

it seems kinda a waste of resources for eevery mob in the game to be searching through a while loop every 20 mili seconds to see if their is a player nearby. Any ideas that would be more efficient
Problem description:

It is. Lummox has an old article on some good programming practices. He mentions this.

http://www.byond.com/forum/?post=39699

Basically, one option is to use areas which trigger an AI loop of everything in that area and "wake them up" in a sense. The loop is only good as long as the thing is awake. I'd suggest giving that whole article a thorough read.
This way is made so the AI is only active if a player is within view (7 tiles in this case) Threw this together real fast for ya.

mob/AI
var
active = FALSE
mob/target = null
proc
Activate()
active = TRUE
for()
sleep(10)
if(!target)
for(var/mob/player/P in ohearers(7,src))
target = P
break
if(!target)
break
else
if(get_dist(src,target)>7)
target = null
else
step_towards(src,target)
active = FALSE

mob/player/Move()
..()
for(var/mob/AI/A in ohearers(7,src))
if(!A.active)
A.Activate()
That wouldn't be any better. He's going for efficiency here, and you are calling a for loop every time each player tries to move. Lummox's example is a good option, and I suggest both of you check out that article because it's very helpful.
In response to Fugsnarf
Fugsnarf wrote:
That wouldn't be any better. He's going for efficiency here, and you are calling a for loop every time each player tries to move. Lummox's example is a good option, and I suggest both of you check out that article because it's very helpful.

My way is also efficient, it's for tile-based movement of coarse.
In response to Kozuma3
You're dropping the return value in Move.
In response to Kozuma3
yes youd have to agree its alot more efficient then mine as it only activates when a player moves(which there should be less of then enemies) while mine happened infintiley rather if something was moving and also effected a greater number of mobs.

I also looked at your article, fugsnarf. I tried to copy and paste the code to fool around with it so i could learn. But all that did was bring about errors. When i fixed the errors all it did was do nothing.
Have a turf trigger the AI once it's crossed.
The code wasn't there to be copied and pasted. It's there to learn from and duplicate on your own. I guess that's too much to expect.
In response to Fugsnarf
Fugsnarf wrote:
The code wasn't there to be copied and pasted. It's there to learn from and duplicate on your own. I guess that's too much to expect.

We're sorry, oh great one.
I didn't intend to come off as arrogant or pretentious, so I apologize if I did.

To the OP: the best way to learn these concepts is to make attempts at it yourself, like any other skill. I highly suggest that over just copying and pasting, but in the end it's up to you.
Best response
Take a look at this post for a complete AI solution that doesn't use a mob-search loop for every enemy. Instead, I search for enemies within range every time a player moves.

In theory, there will be less players than AI on a map at a given time, thus the savings should be significant. On top of that, the most a player should be moving is one tile at most per frame, so you can pretty well depend on this scaling linear with your player count.

http://www.byond.com/forum/?post=1447399#comment7868507
In response to Fugsnarf
the only problem i have is i have a really hard time following his code in particular just because everything is being cross referenced. If i had build that, no problem. The problem is i didnt. ANd unless i can see clearly what its doing ill just ignore it. But i came up with my own solution