ID:154323
 
I'm not QUITE sure this belongs in design philosophy, but it's not really a code problem, I'm only looking for ways to improve the current code. Here's the basics:
proc/Told()
mob/tell(mob/M as mob in view(), msg as text)
usr << "You tell [M]: [msg]"
M.Told(msg)

mob/DemoGuy
Asked(msg)
if(findtext(msg,"hello")) src.tell(usr,"Hello.")
else if(findtext(msg,"hey")) src.tell(usr,"Hey to you too.")
else if(findtext(msg,"hola")) src.tell(usr,"Buenas Dias.")
else if(findtext(msg,"monkey")) src.tell(usr,"Mmm...Monkeys.")
else if(findtext(msg,"armedillo")) src.tell(usr,"In Montana!")
else if(findtext(msg,"dice")) src.tell(usr,"Go eat some chicken you freak!")
else if(findtext(msg,"DM")) src.tell(usr,"Hey, that's what I'm programmed in!")

Now, I'm not really happy with having a whole lot of if, else if, else if, else if's in this code. I tried the switch only to discover that it only likes "constant expressions", so that's not going to work. I was wondering if anyone has some suggestions for how to improve this?
Try an associative list...
var/list/phrases = list ("hey" = "Hey to you, too.","etc" = "And so forth.")

Then, in your proc:

for (var/t in src.phrases)
if (findtext(t,msg)) usr << "[phrases[t]]"

I don't know for sure that the src/usrs match what you were doing, but you should get the idea.
In response to Lesbian Assassin
Lesbian Assassin wrote:
Try an associative list...

That Lummox!

Thanks to his article that was my first thought too...
In response to Deadron
I haven't read it... associative lists are my answer to everything.
In response to Lesbian Assassin
Lesbian Assassin wrote:
I haven't read it... associative lists are my answer to everything.

Even to the question "What's the square root of 5?"
In response to Lesbian Assassin
Ha! That's wonderful! (Never knew about those before).
In response to Leftley
var/list/square_roots = list ("5" = "2.23606797749978969640917366873128...")
In response to Foomer
They're a really powerful tool for making things like conversation starters, especially since they allow you to do away with a lot of hard-coding.
In response to Lesbian Assassin
Lesbian Assassin wrote:
They're a really powerful tool for making things like conversation starters, especially since they allow you to do away with a lot of hard-coding.

Yeah...imagine writing them to a savefile or textfile, then editing the file externally whenever you want without having to reupload the game...

Would be nice in general for responding to predetermined words...however I always find myself adding all sorts of extra logic to conversation trees.
In response to Foomer
Foomer wrote:
Ha! That's wonderful! (Never knew about those before).

I found out about them very soon after starting with BYOND... just didn't understand a thing about 'em for the next several months thereafter. Ever since I figured out how to use them, though, I've been using them compulsively.
In response to Leftley
Leftley wrote:
Foomer wrote:
Ha! That's wonderful! (Never knew about those before).

I found out about them very soon after starting with BYOND... just didn't understand a thing about 'em for the next several months thereafter. Ever since I figured out how to use them, though, I've been using them compulsively.

When I got to BYOND they didn't have associated lists, so in typical fashion I added them to the Deadron Library, as Dictionary objects.

Much nicer to have them integrated into the language, though.
In response to Deadron
I never did quite figure out how to work an associative list, but it would help in a lot of the things I'm doing. I have used tons of lists lately, though.
In response to Lord of Water
Lord of Water wrote:
I never did quite figure out how to work an associative list, but it would help in a lot of the things I'm doing. I have used tons of lists lately, though.

ive been using a combination of constants and lists alot lately as well. I think im going to be doing the skills by associative lists.

FIREking