Menu with Container Item List

Ok. So I have a container called "Weapons" that holds a players weapons. I want them to pick a weapon to attack with using a menu (instead of having to type "attack with blah"). So I tried the following and it doesn't work. What am I doing wrong?

menulist = NewStringList()
foreach (obj, GetDirectChildren(Weapons)) {
weapon = obj + "(" + obj.melee_dmg + " dmg)"
list add (menulist, weapon)
}

I wanted to add it's damage to the description, but I even tried this and it didn't work.

menulist = NewStringList()
foreach (obj, GetDirectChildren(Weapons)) {
list add (menulist, obj)
}

The game just freezes.


I'm not sure why that would freeze. But the most obvious issue is the expression obj + "(". You're adding an object to a string, which probably isn't what you want. I suspect that code should be:

menulist = NewStringList()
foreach (obj, GetDirectChildren(Weapons)) {
  weapon = GetDisplayAlias (obj) + "(" + obj.melee_dmg + " dmg)"
  list add (menulist, weapon)
}

so that you're displaying the alias of the object, rather than the object itself.

However, this still isn't perfect. Once you've done the ShowMenu, your result variable will be something like "knife (15 dmg)", which will be hard for your code to deal with. This is why the ShowMenu function allows dictionaries. You could use something like:

menudict = NewStringDictionary()
foreach (obj, GetDirectChildren(Weapons)) {
  weapon = GetDisplayAlias (obj) + "(" + obj.melee_dmg + " dmg)"
  dictionary add (menudict, obj.name, weapon)
}

in that case, if you pass menudict to the ShowMenu function as a list of options, it will show the alias and the damage, result will just be the name of the weapon. So in the code within the menu, you could do something like weapon = GetObject (result) and then you have an object variable you can use to examine that weapon's attributes.


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

Support

Forums