ID:154052
 
Unless I'm mistaken, Lummox (and perhaps others) have said they prefer associative lists over long switch statements. For the AI that I'm working on I'm currently using two switch statments (per mob) each AI cycle. The first is based upon the task it's assigned (gather, nurse, attack, etc.) and the second is the mode within those tasks (return, enter colony, exit colony, search, etc.).
AI_Main()
switch(task)
if(GATHER) Gather_AI()
if(NURSE) Nurse_AI()
//and so on
Gather_AI()
switch(mode)
if(SEARCH) Gather_Search()
if(ENTER) Gather_Enter()
//and so on

This is very organized, neat, and easy to follow (at least the way my mind works) but I realized it has a lot of function calls and some if statements I could avoid. Before I do an overhaul of the system (just a bit tedious, not too bad though) I wanted to get some advice on whether this would make much of a difference (or have negative side effects). Possibly hundreds of mobs would be doing this each second or so.

var/list/proclist = list("GS" = /mob/proc/Gather_Search, "GE" = /mob/proc/Gather_Enter)

AI_Main()
call(proclist[task])()

I could use numbers instead of strings but that is just to get the basic point across (which I suppose would just be an array).
English wrote:
This is very organized, neat, and easy to follow (at least the way my mind works) but I realized it has a lot of function calls and some if statements I could avoid. Before I do an overhaul of the system (just a bit tedious, not too bad though) I wanted to get some advice on whether this would make much of a difference (or have negative side effects). Possibly hundreds of mobs would be doing this each second or so.

Switch() will definitely be less efficient than a list, if there's any kind of complexity involved. If you're just switching between two or three states, switch() or an if/else might be the way to go.

var/list/proclist = list("GS" = /mob/proc/Gather_Search, "GE" = /mob/proc/Gather_Enter)

AI_Main()
call(proclist[task])()

I could use numbers instead of strings but that is just to get the basic point across (which I suppose would just be an array).

One other thing you can do is use the fact that call() can use strings as proc names. How efficient this is I'm not sure, although it strikes me that type paths are probably better suited to that anyway. Just something to keep in mind.

With lists, numbers will be more efficient than associations, plus you can use the associations for other things like determining what "weight" to give a certain possible state choice.

Lummox JR
In response to Lummox JR
Thanks for the input, I think I'll stick with the first version of call that uses the path, it also lets me avoid using strings besides the uncertainty of how efficient it is.

[Edit]
Wow, it's amazing how much difference eliminating 1300 function calls every half a second can make (give or take a few hundred). I did other things that helped but this was one of the larger ones.
[/Edit]