ID:156516
 
Alright, so I have a code that checks if an item exists in the user's contents:
if(locate(obj/cookedshrimp)in usr.contents)

But is there any way so it checks it twice, instead of writing that code twice? By this, I mean it checks if there are two cooked shrimps in the user's inventory.
You could use a for loop to cycle through the players inventory. Something like this:
var/shrimpFound=0
for(var/obj/cookedshrimp/O in usr.contents)
shrimpFound++


Then all you do is compare to that shrimpFound value to see how many the player has.
In response to Gunbuddy13
Seems like a lot of unnecessary variables =/ I mean actual code, such as:
if(locate(/obj/cookedshrimp*2)in usr.contents)
In response to Colin1011
I think that IS "real code". Correct me if I'm wrong but that is as basic as it gets. There are no unnecessary variables; where else would you store the number of objects of a certain type in your inventory?
In response to Duelmaster409
I consider it a big waste of time to make a seperate variable for every single object. >.> It complicates everything. And by real code, I mean a function that checks how many total 'cookedshrimp' are in the user's inventory.
In response to Colin1011
DM's syntax can't include an easy way to do everything you'd ever want to do. That's why you can define your own functions. If you think it's messy to use a for loop and variable to add up the number of cooked shrimp, throw the code in its own proc.

mob/proc/get_shrimp_count()
var/shrimpFound=0
for(var/obj/cookedshrimp/O in src.contents)
shrimpFound++
return shrimpFound

// When you need to check how many shrimp the player has:
if(src.get_shrimp_count() >= 2)
src << "You have at least two cooked shrimp!"


You could make the function more generic by having it take a type path and checking how many objects of that type are in the player's inventory.
In response to Forum_account
That makes it seem a lot easier ;D I'll just make a seperate code file for those procs... thanks alot.
In response to Forum_account
Just wondering, though... Would it be possible to do this?
if(locate(/obj/cookedshrimp)in usr.contents)
if(locate(/obj/cookedshrimp)in usr.contents)

Would that check for two, or would it find the same ones all over again?
In response to Colin1011
It would find the same one twice. If you want to use locate you'd need to find a shrimp, remove it, then try to locate another. Adding/removing things from the player's contents can cause problems. The code will also look messier and won't scale well (what happens when you want to look for 3 or 4 cooked shrimp?).