ID:2415034
 
(See the best response by Nadrew.)
I want to know why my code snippet causes my game to crash. Is there a better way to accomplish what I am trying to do?

Guard_Behavior()
while(src)
var/mob/Life_Form/L = locate()
if(L) world<<"Hi"
spawn(1) Guard_Behavior()
Best response
You're essentially crashing the stack, you call the proc over and over without it ever ending in the first place. If you use while() you don't need to call the proc again until the condition allows the loop to end. You'll also want to use sleep() instead of spawn() and set waitfor to 0 on the proc, so you can call it without it blocking (cleaner than spawning off the proc call).

Guard_Behavior()
set waitfor = 0
while(src)
// Do stuff
sleep(10) // 1 is probably gonna eat your resources like crazy.


However, you seem to have this proc defined for all mobs, you definitely do not want a loop running all the time on every mob of that type in the game. You'd run out of resources pretty rapidly as you added more. You'd likely want to have a single global loop running that runs over a list of guard NPCs and calls a behavior proc (that's not a loop).

Or you can simply trigger the NPCs as a player comes into view by keeping track of them in a list and going over said list when a player enters an area.
In response to Nadrew
I wasn't even aware of set wait for. Thanks for the help, I am now able to make the code run properly now.

Login to reply.