ID:147890
 
//For some reason this freezes when called
mob/proc/attacktest(mob/M)
var/mob/A = src
if(!M.key)
npcattacks
M.dir = get_dir(M,src)
if(src in get_step(M,M.dir))
spawn(M.attackdelay)
goto npcattacks
if(src in oview(3,M))
walk_towards(M,src)
goto npcattacks
whats the point of var/mob/A
Xallius wrote:
//For some reason this freezes when called
mob/proc/attacktest(mob/M)
var/mob/A = src
if(!M.key)
npcattacks
M.dir = get_dir(M,src)
if(src in get_step(M,M.dir))
spawn(M.attackdelay)
goto npcattacks
if(src in oview(3,M))
walk_towards(M,src)
goto npcattacks

When it gets to the line "goto npcattacks" it goes back to npcattacks and will end up executing the same code again, getting to "goto npcattacks" and doing it all over again. I don't use the goto commands in byond myself, they are very rarely needed and I have not come accross a situation where I had to have them. In fact, most people that do use it are doing so when not needed and usually innapropriately.

I am going to assume that you want M to walk towards the source when farther than 1 tile away and stand there attacking while next to the source. I would suggest you call the function with M as the src and pass in the thing to be walked to/attacked as the M. I will show you how to achieve what you want doing it this way anyway though, as it would apply to the changed function as well.

> mob/proc/attacktest(mob/M)
> var/mob/A = src>
if(!M.key)
while(src)//I personally prefer recursion, but I am showing you how to fix it the way you have it set up.
> M.dir = get_dir(M,src)
> if(src in get_step(M,M.dir))
> //Put in your attacking code here, or call your attack function.
> else if(src in oview(3,M))
> walk_towards(M,src)
sleep(M.attackdelay)//I'm assuming you want this to happen every M.attackdelay 10ths of a second.
In response to Loduwijk
//This is the coding you sent me, properly indented, wiped
//of the comments, and with a test reply >> world.

mob/proc/attacktest(mob/M)
if(!M.key)
while(src)
M.dir = get_dir(M,src)
if(src in get_step(M,M.dir))
world << "[M] attacks [src]"
else if(src in oview(3,M))
walk_towards(M,src)
sleep(M.attackdelay)


//Unfortunately, I am still recieving the following error
//when this proc is called:

//Infinite loop suspected--switching proc to background.
If it is not an infinite loop, either do 'set background=1' or set world.loop_checks=0.
In response to Xallius
Xallius wrote:
//This is the coding you sent me, properly indented, wiped
//of the comments, and with a test reply >> world.

mob/proc/attacktest(mob/M)
if(!M.key)
while(src)
M.dir = get_dir(M,src)
if(src in get_step(M,M.dir))
world << "[M] attacks [src]"
else if(src in oview(3,M))
walk_towards(M,src)
sleep(M.attackdelay)


//Unfortunately, I am still recieving the following error
//when this proc is called:

//Infinite loop suspected--switching proc to background.
If it is not an infinite loop, either do 'set background=1' or set world.loop_checks=0.

No, that is not properly indented. You have everything after the while and before the sleep indented one-line too few. The way I had it was correctly indented, if it did not appear so to you then it must show differently on your browser. Change that like so:

> mob/proc/attacktest(mob/M)
> if(!M.key)
> while(src)
> M.dir = get_dir(M,src)
> if(src in get_step(M,M.dir))
> world << "[M] attacks [src]"
> else if(src in oview(3,M))
> walk_towards(M,src)
> sleep(M.attackdelay)


I would also suggest that you use step_towards instead of walk_towards since this is a loop and it will keep executing that line.
In response to Loduwijk
//I copied that coding and replaced walk with step...

mob/proc/attacktest(mob/M)
if(!M.key)
while(src)
M.dir = get_dir(M,src)
if(src in get_step(M,M.dir))
world << "[M] attacks [src]"
else if(src in oview(3,M))
step_towards(M,src)
sleep(M.attackdelay)
//now it freezes...
//could you please test it so I know that the problem is isolated?
In response to Xallius
The previous example of fixing the indentation didn't go far enough; the sleep() needs to be inside your while() loop.

Lummox JR
In response to Lummox JR
Oops, I did make an indentation error there, sorry about that. And thanks for the catch, Lummox.