The deal's quite simple, every mob/player in the world have a obj/items/list var that, obviously, acts as the inventory for players. What I want to do is to have some interaction if a certain item is found in the inventory (such as a door opening if the player has the proper key, a quest finishing if the player has the proper item, security check for some verbs, etc.). This is the syntax that I use...
To check if the item is in the inventory (whereas mob/M is the player):
if(M.items.Find(locate(/obj/items/common/herb)))
[coding stuff]
And now, if the item's not in the inventory...
if(!M.items.Find(locate(/obj/items/common/herb)))
[coding stuff]
However, I read in that help file that when the item is not found, then the value 0 is returned... so I just wanna make sure about something: what precisely is returned in both cases? And if you see that something is wrong in the above pieces of code, please do tell.
Thanks in advance...
ID:147783
Dec 5 2003, 11:34 am
|
|
In response to DerDragon
|
|
DerDragon wrote:
The number returned is the position the element has been found in the list. > var/list/L = list("one","two","three","four") L.Find("three") would return 3 So I guess my way of verifying if the item was NOT a part of the list was wrong... I should have used instead: if(M.items.Find(locate(/obj/items/common/herb)) == 0) [coding stuff] right? |
In response to Mart2J
|
|
Actually, in your case, you don't want to use Find() at all.
if(locate(/obj/common/herb) in M.items) That is true if there IS an /obj/common/herb in M's items list. Believe it or not, I only just recently learned to use locate() like this... fortunately I wasn't too far into my main code to have to change a whole lot. Btw, if you want to test if there is not.. if(!(locate(/obj/common/herb) in M.items)) I'm not sure if the second set of parentheses is really necessary, but I put them in my code. |
In response to DerDragon
|
|
DerDragon wrote:
Actually, in your case, you don't want to use Find() at all. > if(locate(/obj/common/herb) in M.items) That is true if there IS an /obj/common/herb in M's items list. Believe it or not, I only just recently learned to use locate() like this... fortunately I wasn't too far into my main code to have to change a whole lot. Btw, if you want to test if there is not.. Great! Exactly what I needed! Thanks a million! |
In response to Mart2J
|
|
So I guess my way of verifying if the item was NOT a part of the list was wrong... I should have used instead: Nope, it works just fine. Look at the help file and you'll see that almost every proc() returns 0 if the thing you're trying to do fails. 0 or null, both of which are read as an if statement as being "FALSE". Any if statement is executed if the condition in () is "TRUE". if (!"FALSE") reads as being true, so it's executed. If the fact that it "returns 0" invalidated ! as a method, then ! would be worthless in almost every case. |
In response to DerDragon
|
|
DerDragon wrote:
if(!(locate(/obj/common/herb) in M.items)) I'm not sure if the second set of parentheses is really necessary, but I put them in my code. The extra parentheses are necessary, unfortunately. For some strange reason, the "in" operator has a higher precedence than the ! operator. If you leave out the parentheses, it's effectively testing <code>if(null in M.items)</code>. |
i.e.
L.Find("three") would return 3