show menu use alias while getting object from result

Avantar
I hope some-one clever might come up with an idea to my limited coding knowledge and logic thinking. I do not have a problem per se, but rather an annoyance - let me try and explain:

I sell weapons in a shop - Example: a Dagger (the alias and name will be Dagger)
When I buy Daggers from the shop, the names will go Dagger1, Dagger2....
There will be situations where I will need to equip a weapon whilst in combat (This script loops until either enemy or game.pov dies)
On your combat round turn, a menu pops up using 'show menu' of possible choices and these choices outcome is defined by a switch and using cases
One of the menu options is: Equip Weapon

A for loop will loop through weapons in your inventory and add them to a list called 'weaponlist' and the actual object's name (Dagger1, Dagger2....) will be added to the list.
The next 'show menu' will display this available weapons for you to use and from the result, it will run an attribute script for this weapon.
The annoyance is - I do not want the name displayed, but rather the alias - If I do that, it does not find the actual object belonging to the alias (obviously) and I can not think of a work around.

Here is the code for you to follow - might clear up the question (One of the cases for the switch):
game.pov.weapon_number = 0
weaponlist = NewStringList()
foreach (object, ScopeInventory()) {
if (DoesInherit(object, "weapon") and object.location = "single") {
game.pov.weapon_number = game.pov.weapon_number + 1
msg (object + " is a single handed weapon")
list add (weaponlist, object.name)
}
else if (DoesInherit(object, "weapon") and object.location = "dhand") {
game.pov.weapon_number = game.pov.weapon_number + 1
msg (object + " is a two handed weapon")
list add (weaponlist, object.name)
}
}
if (game.pov.weapon_number > 0) {
show menu ("Which weapon do you want to equip:", weaponlist, true) {
if (result <> null) {
object_x = GetObject(result)
if (object_x<>null and object_x.location="dhand") {
msg ("You have chosen to equip your " + result)
do (object_x, "equip")
}
else if (object_x<>null and object_x.location="single") {
show menu ("In which hand do you want to equip " + result + "?", split("Left hand;Right hand", ";"), false) {
switch (result) {
case ("Left hand") {
do (object_x, "equipl")
}
case ("Right hand") {
do (object_x, "equipr")
}
}
}
}
}
else {
msg ("You have chosen to press cancel")
}
}
}
else {
msg ("You do not have any weapon in your inventory to equip!")
}


Thank you in advance - Even if you just say that it is not possible.

jaynabonne
Thankfully, it's an easy one! :) It seems not many people know about the dictionary abilities of show menu and ShowMenu.

You can use a string dictionary instead of a string list with show menu (or ShowMenu). The *value* for each entry is what's shown in the menu, and the *key* for the entry is what's returned in result. In other words,the value is the "menu display string" for the key.

As an example, if you do this:

options = NewStringDictionary()
dictionary add(options, "male", "I am male")
dictionary add(options, "female", "I am female")
showmenu ("Are you male or female", options, false) {
msg("result = " result)
}

then the menu will show:

I am male
I am female


and if you pick the first, result will come back "male" and if the second, "female".

So for your case, change this:

weaponlist = NewStringList()

to this:

weaponlist = NewStringDictionary()

and change:

list add (weaponlist, object.name)

to

dictionary add (weaponlist, object.name, GetDisplayAlias(object))

and it should do what you want.

Edit: Note the GetDisplayAlias(object) can be whatever you want. You could even use something like:

GetDisplayAlias(object) + " (single-handed)"

to add more description into the menu text.

The Pixie
Here is a function that will search though the player inventory for an object withthe given alias, and return the first one found (or null if not found):

  <function name="FindByAlias" parameters="value" type="object">
foreach (o, ScopeInventory ()) {
if (HasString(o, "alias")) {
if (o.alias = value) {
return (o)
}
}
}
return (null)
</function>

Avantar
Yes...yes...yes!

Like I said: I will leave this for the clever ones.

@jaynabonne: You know my questions are usually easy. :D
I do not think that I have seen a full blown game from you yet. With your knowledge it should be promising....Working on something?

Thank you both!

jaynabonne
Avantar wrote:@jaynabonne: You know my questions are usually easy. :D
I do not think that I have seen a full blown game from you yet. With your knowledge it should be promising....Working on something?


Funny you should ask: viewtopic.php?f=5&t=4659
:)

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

Support

Forums