ID:1826119
 
(See the best response by Avidanimefan.)
Problem description:

So I have talk verb. I want an if condition that checks for a certain item in this persons inventory. In this case it's an obj/item/scroll/letterforgruwald. How do I incorporate that into an if statement?
Best response
var/obj/item/S=locate(/obj/item/scroll/letterforgruwald) in usr.contents
if(!isnull(S))//then you found it
//carry on
Thank you very much!
In response to Nowatimean
Alternatively, you can cast more specifically and use locate()'s documented shorthand (and also name your variables better):
var obj/item/scroll/letterforgruwald/letter = locate() in usr
if(letter)
// letter found
That makes sense. I get screwed up with the conditions a lot. Thank you guys, though. Much appreciated!
Or you can turn it into a one liner, and prevent useless scopes...

if(!(locate(/obj/item/scroll/letterforgruwald) in src)) return
//letter found, continue
In response to Suicide Shifter
I have a feeling this letter is going to be deleted or something, so a reference to it will be used. Otherwise, yeah, useless or one-time-use variables should be avoided unless it makes the code clearer for the reader.

It looks to me like a delivered quest item, but that's a guess.
I never really enjoyed the use of locate() other than for grabbing locations on the map, although I know there's a lot more functionality for it than that given Kaiochao's suggestions above. I think I would probably do it this way:
for (var/obj/item/scroll/letterforgruwald/a in src.contents)
// Ah we found one. Let's break the for loop.
// This for loop will not run if none are found.
world << "we found the [a]!"
break
In response to Mr_Goober
Mr_Goober wrote:
I never really enjoyed the use of locate() other than for grabbing locations on the map, although I know there's a lot more functionality for it than that given Kaiochao's suggestions above. I think I would probably do it this way:
> for (var/obj/item/scroll/letterforgruwald/a in src.contents)
> // Ah we found one. Let's break the for loop.
> // This for loop will not run if none are found.
> world << "we found the [a]!"
> break
>


But then you're creating an unnecessary loop through all of the mob's contents which isn't exactly efficient especially if you've got ten people all doing the same quest or whatever runs the process at the same time.
Yeah it was for a quest item, as Kaiochao said. The item gets deleted once you "deliver" it.