Match result to objects

Gng

Heyall

So in a shop, I was wondering if there's a way to find obj.name from a string.

options = NewStringList()
productlist = NewObjectList()
foreach (x, GetDirectChildren (aiditems)){
    if (IsRegexMatch ("pharmashop", x.selltype)){
        list add (productlist, x)
        x = x.alias + " ( " + x.price + ")"
        list add (options, x)
    }
}
list add (options, "exit interaction")

First, I added the needed objects using a filter. I didn't use the filterbyattribute because the object can be bought in several places. I created the "selltype" string attribute, then added "pharmashop, medics". So it will be selected in both places for buying.
I also added the price to the brackets.
Then:

ShowMenu ("What product would you like to buy?", options, false) {
    foreach (x, productlist){
        if (IsRegexMatch (result, x)){
            BuyObject(x)
        }
    }
    msg ("Thank you for your purchase! Would you like another product?")
}

This is where I'm stuck. In ShowMenu, how can I get the object from a string chosen by the player so that I can Clone it and add it to the player?


Gng

Solved: This is how I managed to solve it:

options = NewStringList()
productlist = NewObjectList()
foreach (x, GetDirectChildren (aiditems)){
    if (x.available = true){
        if (IsRegexMatch ("pharmashop", x.selltype)){
            list add (productlist, x)
            x = x.alias + " (" + x.price + ")"
            list add (options, x)
        }
    }
}
list add (options, "exit interaction")

ShowMenu ("What product would you like to buy? You have {color:gold:" + player.money + "} currency.", options, false) {
    z = result
    n = Instr (z, "(") - 1
    z = Replace(z, Mid (z, n), "")
    o = GetObject(z)
    BuyObject(o)
}

It's a long time since I played with this. But if I remember correctly there's a simpler solution. If the options list passed to ShowMenu is a stringdictionary, you can have different values for the text displayed on the menu, and the value of result when it is chosen.

So you could do it like this:

options = NewStringDictionary()
foreach (x, GetDirectChildren (aiditems)){
    if (x.available = true) {
        if (IsRegexMatch ("pharmashop", x.selltype)){
            description = x.alias + " (" + x.price + ")"
            dictionary add (options, x.name, description)
        }
    }
}
dictionary add (options, "exit interaction", "")

ShowMenu ("What product would you like to buy? You have {color:gold:" + player.money + "} currency.", options, false) {
    o = GetObject(result)
    BuyObject(o)
}
```


Gng

Interesting. I thought of using a dictionary, but I didn't know how it would help and it didn't seem very easy to me, but your solution is simple and works. Thank you

For anyone wondering, I also added a condition for exit interaction:

options = NewStringDictionary()
foreach (x, GetDirectChildren (aiditems)) {
  if (x.available = true) {
    if (IsRegexMatch ("pharmashop", x.selltype)) {
      description = x.alias + " (" + x.price + ")"
      dictionary add (options, x.name, description)
    }
  }
}
dictionary add (options, "exit interaction", "exit interaction")
ShowMenu ("What product would you like to buy? You have {color:gold:" + player.money + "} currency.", options, false) {
   if (not result = "exit interaction") {
    o = GetObject(result)
    BuyObject (o)
  }
}

For anyone wondering, I also added a condition for exit interaction:

Normally I would have checked for that case; but the initial code you posted didn't, so I assumed that your BuyObject function already dealt with being given null.


Log in to post a reply.

Support

Forums