Display inventory in menu

Sir_Numybuns
Hey pplz, quick noob question, when using the display a menu command, how can i get it to display all the items the player is currently carrying? Thank you in advance for any responses

The Pixie
First of all, I hope you are happy using code. This link will tell you how to paste it into your game if not.
viewtopic.php?f=18&t=4771

// Lines starting with two slashes are comments so you know what is happening!
// Get a list of the names of the objects held
// Start from an empty list
string_list = NewStringList()
// Loop through each item held, and add its name to the list
foreach (obj, ScopeInventory()) {
// Add the name or alias to the list
list add (string_list, GetDisplayName(obj))
}
// Show the menu
ShowMenu ("Pick an item", string_list, true) {
// The selection goes in a variable called "result"
// We have to search the inventory to find the object with that name
// so again we loop through it
foreach (obj, ScopeInventory()) {
// Does the name or alias match?
if (result = GetDisplayName(obj)) {
// Now we know the player picked whatever is in the obj variable
// we can do something with it
msg ("You selected the " + GetDisplayName(obj))
}
}
}

jaynabonne
You can also use the dictionary form of ShowMenu (mapping object to display name), which eliminates the need to find the object from the display name once chosen (and will work properly if multiple objects have the same display name, which shouldn't happen, but...). If you use the dictionary form, then the result will just be the desired object.

Sir_Numybuns
Thanks, i got it working! my only problem is that it occasionally prints the same message twice after picking an option, but minor details...

The Pixie
jaynabonne wrote:You can also use the dictionary form of ShowMenu (mapping object to display name), which eliminates the need to find the object from the display name once chosen (and will work properly if multiple objects have the same display name, which shouldn't happen, but...). If you use the dictionary form, then the result will just be the desired object.

Good point, that is better:
// Lines starting with two slashes are comments so you know what is happening!
// Get a dictionary of the names of the objects held
// Start from an empty dictionary
string_dict = NewStringDictionary()
// Loop through each item held, and add its name and display name to the dictionary
foreach (obj, ScopeInventory()) {
// Add to the dictionary
// the name is the key, the display name is the value
dictionary add (string_dict, obj.name, GetDisplayName(obj))
}
// Show the menu (shows the values from the dictionary)
ShowMenu ("Pick an item", string_dict, true) {
// The selection key goes in a variable called "result"
obj = GetObject(result)
// Do stuff weith it
msg ("You selected the " + GetDisplayName(obj))
}

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums