ID:1270126
 
(See the best response by Stephen001.)
Hello,

It seems when I use while statements it tend to lag ALOT and my game becomes unplayable alot of the time.

Is there a way to reduce lag or am I doing something wrong?

For example if I write this, It works but the game is very laggy, but if I remove it the game runs well.

mob/proc/attackAI()
while(src)
for(var/mob/M in oview(5))
if(M.Enemy == 1)
walk_to(src,M,1,2)
M.HP -= 1
oview() << "A Fire Fight Has Ensued!"
if(M.HP <= 0)
oview() << "Unit [M] Destroyed."
del(M)
sleep(10)


I am sure I am just missing something simple but I thought I would ask.

I have used while statements in other games without lag but I can't see why sometime it lags and other times I don't have a problem.

Any Help is appreciated!

Thanks in advance!
Bloodocean7.
Can you show us where you are calling this procedure?

How many AI controlled mobs are there in the world, roughly?
Your sleep(10) isn't indented correctly, that's why it lags.
mob/Grunt
icon = 'icons.dmi'
icon_state = "Grunt"
New()
src.attackAI()


I call it like that usually and this is just a new project to try out a concept so its the only AI in game so far which is what is strange.

and as for the sleep(10) I have tried adding it in other areas and it still lags pretty badly. :P

Easy mistake to make.

Walk_to: Move Ref on a path to Trg continuously, taking obstacles into account. Each step will be preceded by Lag time of inactivity. If Ref is within Min steps of Trg, no action will be taken. This is also true if the target is too far away (more than twice world.view steps).

Prime word: "continuously". You're in essence calling a loop in a loop, sort of. Not to mention you're trying to make the AI go to everyone at once "continuously" if it didn't lag your AI would be having a stroke.


Instead of doing this you will have to set a "target" reference for the AI. If target in view walk_to. Pause the searching of a new target until current target is dead or out of view, etc.

This can give you extra options in your code as when selecting a target you can supply different behaviours like what kind of target they prefer.

Also you should indent that sleep to line up under the while() (in alignment with the for())


Also, in the case of AIs I prefer to replace the walk_to with step_to.

Good luck.
Best response
Shouldn't you be spawn()ing that call to attackAI()?
In response to Stephen001
This. You're not allowing New() to return, which isn't good. By using spawn() you allow New() to return.
So I need to indent sleep() and use step_to and use Spawn() when I call the AI.

Will do :) Thanks guys I'll post if that helps :)
mob/proc/attackAI()
while(src)
for(var/mob/M in oview(5))
if(M.Enemy == 1)
step_to(src,M,1,2)
M.HP -= 1
oview() << "A Fire Fight Has Ensued!"
if(M.HP <= 0)
oview() << "Unit [M] Destroyed."
del(M)
sleep(10)




mob/Grunt
icon = 'icons.dmi'
icon_state = "Grunt"
Click()
walk_to(src,usr)
New()
spawn(10) src.attackAI()


I did as you guys suggested and It doesn't lag now!

I am guessing the Spawn and badly indented sleep was the main issue.

Thanks a million guys this will be sure to help me in future projects :)
Your problem was that "sleep(10)" was nested INSIDE the for() loop. So you were essentially pausing after every mob found in the loop.

But if no mobs were found, there was no sleep() command. So if the NPC had no mobs nearby, it was endlessly looping without any sort of delay.. and that's where your lag was originating.
Ah makes sense!

Its funny how one line of code in the wrong place can cause so much trouble!

Thanks again guys!
Unfortunately that's all it takes sometimes. One line indented wrong can cause a whole program to not run properly.
In response to Stephen001
Stephen001 wrote:
Shouldn't you be spawn()ing that call to attackAI()?

MAN! you're a GOD!
I had the same doubt for months lol

Thanks so much for that enlightment =D