ID:174045
 
How do you pass multiple mobs into a proc?
Xallius wrote:
How do you pass multiple mobs into a proc?

How you would want to do that depends on what you are going to do with them, but I will show you two ways you might want to pass multiple mobs as arguments into a procedure.

The first, and probably easier, way is to simply add more arguments to your procedure. If you want to pass both an attacker and a defender into a procedure then put in a variable for each as arguments.

example:
proc/duel(mob/attacker,mob/defender)

Then when you call that procedure in your code and want to pass in both mobs.

example:

//setting up a duel
duel(src,M)

The second way, and arguably the more versatile, allows you to pass in any number of mobs into your procedure. make the argument for your procedure accept a list, and pass a list of the mobs into it.

example:
proc/sight_effect(list/mobs_in_sight)

Then when you call the proc add in a list of all mobs, like so:

example:
var/list/mobs=list()
for(var/mob/M)
mobs.Add(M)
sight_effect(mobs)


Hope that cleared it up. If you have any further questions about it, just explain what you need cleared up.
In response to Loduwijk
//here's my coding... I am not sure what is wrong...

mob/proc/attacktest(mob/M)
if(!M.key)
npcattacks(src,M)

mob/proc/npcattacks(mob/M)
if(src in get_step(M,M.dir))
npcattacks(src,M)
else if(get_dist(M,src) <= 10)
walk_towards(M,src)
npcattacks(src,M)

//Thank you for your time and effort ^_^!
In response to Xallius
//It gives an infinite loop error
In response to Xallius
Xallius wrote:
//here's my coding... I am not sure what is wrong...

mob/proc/attacktest(mob/M)
if(!M.key)
npcattacks(src,M)

mob/proc/npcattacks(mob/M)
if(src in get_step(M,M.dir))
npcattacks(src,M)
else if(get_dist(M,src) <= 10)
walk_towards(M,src)
npcattacks(src,M)

//Thank you for your time and effort ^_^!

Your npcattacks has only one argument, yet you pass in two. By the looks of it, you don't need npcattacks(src,M), just npcattacks(M).
In response to Xallius
You have your npcattacks() calling itself each time. Put a spawn in there.

npcattacks(mob/M)
//other code
spawn()
npcattacks(M)