Hey, sorry to bother you guys again, but this seems to be my last big problem :)
Is there anyway to have a function thats called every X (1/10) of a second, or whatever? I basically need to do that so I can have my NPC mobs wander around mindlessly unless it sees a PC to attack :)
ID:179377
Jan 10 2002, 7:03 pm
|
|
In response to Vortezz
|
|
Thanks, what you said did help :) And in case anyone else needs to know how, here's what I did:
<font size=2> proc/NPC_Check() while(1) //inifinite ticker loop sleep(50) var/tmp/mob/M for(M in view()) if (!M.client) walk_towards(M,usr,3) </font> I then called NPC_Check() when the player logs in. The NPCs will only chase as long as you are in their view! Now if I could just figure out how to get BYOND to do my homework ^_^ |
Dreq wrote:
Hey, sorry to bother you guys again, but this seems to be my last big problem :) I always recommend against using the spawn/sleep approach for this...it leads to a number of problems down the road. Recently I had one single sleep() loop like that in my code and it caused no end of race condition bugs. Better to use an event loop...I have an EventLoop library, and some demos to show how to use it: byond://Deadron.WalkingClicking byond://Deadron.CombatSystem |
In response to Deadron
|
|
What does that mean?
I do have it set up to move certain "races" at certain speeds, if that's what you mean. |
In response to Dreq
|
|
Dreq wrote:
What does that mean? A race condition is a bug that occurs based on the timing of two or more things messing each other up. By using an event loop, you remove this possibility. If you use spawn() or sleep() to say "do this in 2 seconds", then something else happens and you say "do that in 1 second"...and both of those timers go off and the mob is told to do two different things in the same second...it may do both (when it's only supposed to be able to do one) or it stop in the middle of one and start the other...etc. Basically, confusion reigns. If you use an event loop, you can ensure that only one thing is going on at a time. |
In response to Deadron
|
|
Well I sorta want it to do that. I want it to go at the closest character at all times. If one gets too far away, I want it to break persute and chase a closer PC (if any is there), so wouldn't that be find then?
|
Dreq wrote:
Well, you'll want to use sleep() or spawn() here. Look em up, I'm not thinking straightly enough to give a good answer.
About your wandering around mindlessly bit, think about this for a minute:
<code> 100 NPC mobs + 100 NPC mob actions every second = 200 NPCs moving every second 200 NPCs moving every second * 60 seconds = 12,000 walking actions per minute </code>
That's a LOT for the CPU to handle. Might wanna do your AI only if someone is in view(), cause it'll be easier on yer CPU :)