ID:270184
 
Well, I've finally decided to mess around with associative lists, I do indeed find them extremely handy. What I'm trying to achive is pulling a value from an element of a list. I have a value, from an element, of a list.

Stats=list("Attack"=1,"Defense"=1)


What I'm trying to do is alter a players stats by pulling the value from the element. I've tried, but with no prevail.

            if(prob(Prob_Hit)) for(var/S in Contents)
if(S["Attack"])
text+="ATTACK "
if(M.Atkloss==6) {P.Battle_Text("[copytext(uppertext(M.name),1,12)]'s ATTACK cannot be raised anymore!",1);return}
if(M.Atkloss==-6) {P.Battle_Text("[copytext(uppertext(M.name),1,12)]'s ATTACK cannot be lowered anymore!",1);return}
M.Atkloss=min(6,max(M.Atkloss+S["Attack"],-6))
if(abs(S["Attack"] / 2) == 1) text+="sharply "
if(S["Attack"] > 0) text+="rose!"
else text+="fell!"


The above outputs a bad index runtime error.
The problem is kinda common for those who just started using lists (I remember this happening to me -_-')

Anyways, the problem is that you have S["Attack"]

The reason that's the problem is that it's suppose to be Stats["Attack"] to get that value (I will show at the end of why this is...in one way)

I am not sure what you are doing with this "for(var/S in Contents)" but if the Stats list is in obj's or something, you can always do S.Stats["Attack"]

Here's the thing about list value recall:
mob/var/Stat = list("Health"=1,"MaxH"=122)
//The above makes all mob's have a list variable called Stat
//which contains the value of Health and MaxH

mob/verb/Return()//... guess what this is ;/
world<<list2params(src.Stat)//This is completely unneeded
//unless you are debugging a list for any sort of errors
world<<"Your current health is [src.Stat["Health"]]!"


Here's the simple recall command:
List_Variable["Sub_Var value to be recalled"]

so if the var was: S = list("A" = 12,"B")

than
S["A"] will return 12 and S["B"] returns null

I think I messed up something there so if ya do find it, please correct me :)

- GhostAnime
How did you define the list?
Did you define it like:
mob/var/list/Stats=list("Attack=1",etc)?
In response to GhostAnime
Ah, okay. I see what you did, and saw my problem of using the for() statement. I switched it around and it works now! Here's my changed snippet:

            if(prob(Prob_Hit))
if(Stats["Attack"])
text+="ATTACK "
if(M.Atkloss==6) {P.Battle_Text("[copytext(uppertext(M.name),1,12)]'s ATTACK cannot be raised anymore!",1);return}
if(M.Atkloss==-6) {P.Battle_Text("[copytext(uppertext(M.name),1,12)]'s ATTACK cannot be lowered anymore!",1);return}
M.Atkloss=min(-6,max(M.Atkloss+Stats["Attack"],6))
if(abs(Stats["Attack"] / 2) == 1) text+="sharply "
if(Stats["Attack"] > 0) text+="rose!"
else text+="fell!"