ID:172370
 
Is there anyway to check if the user has 5 Items on him for a quest without having to make a var and adding to it everytime someone Picks up one of the 5 items?
I could do a series of ifs but i dont know if this is safe and is there any easier and shorter way to do it?
if(locate(/obj/Items/CupJellyStars/Star1) in usr)
if(locate(/obj/Items/CupJellyStars/Star2) in usr)
if(locate(/obj/Items/CupJellyStars/Star3) in usr)
if(locate(/obj/Items/CupJellyStars/Star4) in usr)
if(locate(/obj/Items/CupJellyStars/Star5) in usr)
usr << "You Completed The CupJelly Quest"
In response to Turles9000
Turles9000 wrote:
I could do a series of ifs but i dont know if this is safe and is there any easier and shorter way to do it?
> if(locate(/obj/Items/CupJellyStars/Star1) in usr)
> if(locate(/obj/Items/CupJellyStars/Star2) in usr)
> if(locate(/obj/Items/CupJellyStars/Star3) in usr)
> if(locate(/obj/Items/CupJellyStars/Star4) in usr)
> if(locate(/obj/Items/CupJellyStars/Star5) in usr)
> usr << "You Completed The CupJelly Quest"


Quick Example:


if(usr.contents == /obj/star && usr.contents == /obj/star1)
usr<<"Quest completed."

In response to SSJ4_Gohan_Majin
Hmm i dont really get how that code works :/ But I found a way that works for me.If you could explain to me how that code works that'd be great. This is what i have.

                                    if(locate(/obj/Items/CupJellyStars/Star1) in usr)
var/obj/Star1=locate(/obj/Items/CupJellyStars/Star1) in usr.contents
if(locate(/obj/Items/CupJellyStars/Star2) in usr)
var/obj/Star2=locate(/obj/Items/CupJellyStars/Star2) in usr.contents
if(locate(/obj/Items/CupJellyStars/Star3) in usr)
var/obj/Star3=locate(/obj/Items/CupJellyStars/Star3) in usr.contents
if(locate(/obj/Items/CupJellyStars/Star4) in usr)
var/obj/Star4=locate(/obj/Items/CupJellyStars/Star4) in usr.contents
if(locate(/obj/Items/CupJellyStars/Star5) in usr)
var/obj/Star5=locate(/obj/Items/CupJellyStars/Star5) in usr.contents
del Star1
del Star2
del Star3
del Star4
del Star5
usr << "You Completed The CupJelly Quest"
In response to Turles9000
SSJ4_Gohan_Majin, that is completely wrong. Turles is much closer to being correct that you are.

Turles, that should work fine; but you're right, there is a much cleaner way. One way to do it would be to get the first five /obj/Items/CupJellyStars that the player has. (But also counting subtypes in that, like Star1, Star2, etc.) If there are less than five, they haven't finished the quest. Otherwise, delete those five stars and finish the quest. A list is perfect for this.

<code>var/list/stars=list() // Make an empty list called "stars" for (var/obj/Items/CupJellyStars/star in usr) // Loop through all of usr's stars stars.Add(star) // Add each star to the stars list if (length(stars)>=5) // If we have enough stars break // Exit the for() loop if (length(stars)<5) // If we have less than five stars usr << "You need at least five cup jelly stars to finish the CupJelly quest!" // Tell them else // We have enough stars, so finish the quest usr << "You finished the CupJelly quest!" // Tell them for (var/obj/star in stars) // Loop through all five stars we found... del star // ...and delete them // You'd probably want to give them some kind of reward here.</code>

The comments make it look a bit messy, but it's more robust than a series of if()s.
In response to Crispy
Or, you can always cut down on those loops, and use associative lists, and check to see if they already have that quest item BEFORE they can pick it up.

Example:
mob
player
var
Quests[0]
Bung
verb
Get_Item()
var/mob/player/P=usr
if(!(P.Quests["Bung Quest"]))
P.Quests["Bung Quest"]=new/list()
var/obj/Items/Quest_One/Q=locate() in P.Quests["Bung Quest"]
if(!(Q))
P.Quests["Bung Quest"]+=new Q
return
else
P<<"<b>I'm afraid you already have this!</b>"
In response to Crispy
Im getting 2 errors here.
                            var/R = input("What do you want to do","[src]")in list("CupJelly","Cancel")
switch(R)
if("CupJelly")
for (var/obj/Items/CupJellyStars/star in usr) // Loop through all of usr's stars
stars.Add(star) // Add each star to the stars list
if (length(stars)>=5) // If we have enough stars
break // Exit the for() loop

if (length(stars)<5) // If we have less than five stars
usr << "You need at least five cup jelly stars to finish the CupJelly quest!" // Tell them
else // We have enough stars, so finish the quest
usr << "You finished the CupJelly quest!" // Tell them
for (var/obj/star in stars) // Loop through all five stars we found...
del star // ...and delete them
if("Cancel")
usr.move=1
usr.action=0
return


The two errors are NPC.dm:319:error:star :duplicate definition
NPC.dm:310:error:star :previous definition.

Also In this line
for (var/obj/Items/CupJellyStars/star in usr)
would that check every star?This is how i the obj tree looks.
obj
Items
CupJellyStars
icon = 'CupJelly.dmi'
Star1
Star2
Star3
Star4
Star5

So should i make it var/obj/Items/CupJellyStars so it would count for all them?
In response to Turles9000
Turles9000 wrote:
The two errors are NPC.dm:319:error:star :duplicate definition
NPC.dm:310:error:star :previous definition.

Whoops! On line 310 and everything below it, replace "star" with another variable name... anything will do. Perhaps "star2".

Also In this line for (var/obj/Items/CupJellyStars/star in usr) would that check every star?

Yes, it will check every star.

So should i make it var/obj/Items/CupJellyStars so it would count for all them?

No. "star" is the name of the variable. (It can be anything, really, I just chose a descriptive name.) If you changed that line to for(var/obj/Items/CupJellyStars), it would make a var called CupJellyStars and loop through all /obj/Items. That's not what you want. =)
In response to Crispy
I make everything under line 310 that is star into star2 but leave line 319 and below alone right?Im getting some weird problems. I get more then 1 "You need at least five cup jelly stars to finish the CupJelly quest!" msg the first time i talk to her even if i have all 5 stars.if i dont get those messages i still dont get the "You finished the CupJelly quest!" msg and it doesnt delete the stars.This is what i have.

                                if("Cup Jelly Quest")
for (var/obj/Items/CupJellyStars/star2 in usr) // Loop through all of usr's stars
stars.Add(star2) // Add each star to the stars list
if (length(stars)>=5) // If we have enough stars
break // Exit the for() loop

if (length(stars)<5) // If we have less than five stars
usr << "You need at least five cup jelly stars to finish the CupJelly quest!" // Tell them
else // We have enough stars, so finish the quest
usr << "You finished the CupJelly quest!" // Tell them
for (var/obj/star in stars) // Loop through all five stars we found...
del star // ...and delete them
In response to Turles9000
Yeah, you did the star/star2 thing right.

I just realised why I didn't notice that I reused the same variable name, though; you've actually indented once too much, on all those lines from "if (length(stars)<5)" onwards (six of them). Unindent those six lines once. The way you have them indented now, they're in the checking-for-stars loop, which would cause all sorts of weird stuff to happen (as you found out).

Once you've fixed that, you can go back through it and replace all references to "star2" with "star" if you like. It really makes no difference, so don't bother if you don't want to, but just thought I'd point out that you can. =)
In response to Crispy
1 More problem sorry about all the questions.It works now but after you finished the quest once you can keep talking to him and get as much as rewards as you want. I could make a var that ask if he completed it or not but I want the user to be able to keep doing the quest as long as he has the requirements.This is what i got right now
                                if("Cup Jelly Quest")
for (var/obj/Items/CupJellyStars/star2 in usr) // Loop through all of usr's stars
stars.Add(star2) // Add each star to the stars list
if (length(stars)>=5) // If we have enough stars
break // Exit the for() loop

if (length(stars)<5) // If we have less than five stars
usr << "You need at least five cup jelly stars to finish the CupJelly quest!" // Tell them
else // We have enough stars, so finish the quest
usr << "You finished the CupJelly quest!" // Tell them
for (var/obj/star in stars) // Loop through all five stars we found...
del star // ...and delete them