Is there a way to check for multible objects in a players inventory

I need to have a player leave a room with many different objects, I want to have the exit be locked until the player has them. Is there any way to do this?


You could do it just using the and operator. For example:

if (ListContains (ScopeInventory(), brush) and ListContains (ScopeInventory(), spoon) and ListContains (ScopeInventory(), weasel dentures)) {

however, this is really inefficient.

A better way to do it is to make an attribute somewhere to contain a list of the objects you need. For example, if you have a door that the player can't pass through until they have all those objects, you could give the door an attribute named objects_needed that is an objectlist containing all the needed objects.

I think in the desktop version of Quest you can do this on the door's 'Attributes' tab. (I've not tested this as I don't have the desktop version). In the online version, you'd have to give it an initialisation script instead. Something like:

this.objects_needed = NewObjectList()
list add (this.objects_needed, knife)
list add (this.objects_needed, spork)
list add (this.objects_needed, spoon)
list add (this.objects_needed, dentures)

You could put the attribute on whichever object you want; whether it's a door, a room, or whichever object has the script that needs to check for the presence of these items.

Then when you want to check if they have all the objects, you'd do:

objects_missing = ListExclude (this.objects_needed, ScopeInventory())
if (ListCount (objects_missing) = 0) {
  msg ("You have all the objects!")
}
else {
  msg ("You can't go through! You haven't got " + FormatList (objects_needed, ", ", ", or", "") + ".")
}

(obviously, either or both sides of the if statement would do something more than display a message; I'm just showing you how to test if the player has them)

This works because ListExclude (this.objects_needed, ScopeInventory()) gets a list of all objects that are in the objects_needed attribute but are not in the inventory. So if ListCount for that list is zero, they have them all.

Hope that makes sense; let me know if you need any more explanation.


So for an exit how would I check it so it unlocks the exit, so the exit stays locked until the player has the exit?


Using this to unlock an exit would probably be unnecessarily complex. I think you'd have to put the check on a turnscript, and put the function to unlock the exit inside the if block.

What most people would do, I think, is instead make the exit so that it checks the items when the player tries to go through it. In this case, you wouldn't lock the exit (as locked exits can't run scripts), but you'd give the exit a script to run when the player goes through it:

objects_missing = ListExclude (this.objects_needed, ScopeInventory())
if (ListCount (objects_missing) = 0) {
  msg ("You go through the door.")
  game.pov.parent = this.to
}
else {
  msg ("You can't go through, the door seems to be locked.")
}

You could do this with or without the FormatList line to tell the player what items they need.

The line game.pov.parent = this.to means that the player is allowed through the exit.
You could also add the line this.script = null if you want to make it stop checking after the player has gone through the exit once.


Would the object list be the same as a string list, that's the closest I can find besides object on the attributes tab.


Unfortunately not. Although you might be able to create it as a stringlist (containing the names of the objects) and then go to code view and change type="stringlist" to type="objectlist".

(it's a little frustrating sometimes that the Quest editor doesn't always provide an easy way to set things up)


Log in to post a reply.

Support

Forums