Referencing an attribute of 'result'

scrimshaw04
05 Feb 2017, 12:41

For various reasons, instead of using the 'display a menu of objects' checkbox in the use tab of an enemy in my game, I have to use a script to grab a list of objects from the player's inventory that can be used. I've got that almost working fine, here's the code I have

game.menuitem = this
string_list = NewStringList()
foreach (obj, ScopeInventory()) {
  list add (string_list, "BACK")
  if (DoesInherit(obj, "object_usable")) {
    list add (string_list, GetDisplayName(obj))
  }
}
ShowMenu ("Pick an item", string_list, true) {
  foreach (obj, ScopeInventory()) {
    if (result = GetDisplayName(obj)) {
      this.integer = this.integer + result.integer
      msg ("You use the " + GetDisplayName(obj) + ".")
      destroy (GetDisplayName(obj))
    }
    if (result = "BACK") {
    }
  }
  on ready {
    do (game.menuitem, "script_fight")
  }
}

The thing I'm not certain about is the 'this.integer = this.integer + result.integer' line. Because I'm cloning objects, and the player could have any number of them, I can't refer to a specific object in this case. What I want to do is to reference an integer attribute of the item that's used, and then add that to an integer attribute of the object that the item is being used upon.

So how do I reference a specific attribute of 'result' without referring to it by name?


Jay Nabonne
05 Feb 2017, 13:19

Short and simple answer: wouldn't it just be "obj.integer", since you found the object you wanted? If not, then I'm not sure what you're asking.

Longer, different answer (assuming I know what your asking):
ShowMenu can take a dictionary instead of a list. Here is a quick example:

      options = NewStringDictionary()
      dictionary add(options, "Result1", "Choice 1")
      dictionary add(options, "Result2", "Choice 2")
      dictionary add(options, "Result3", "Choice 3")
      dictionary add(options, "Result4", "Choice 4")
      ShowMenu("Pick an item", options, true) {
      	msg("Result = " + result)
      }

The menu will show the "value" part of what's added (the "Choice X" strings), but when a choice is made, the "key" part (the "ResultX" string) will be set into result.

For your choice, change the string_list to a string dictionary - maybe give it a semantic name like "options". In your foreach, change your add to :

dictionary add(options, obj.name, GetDisplayName(obj))

This will set the key to the object's name and the value (the displayed part) to the display name.

Then to get the obj from the result, just use:

obj = GetObject(result)

No need to use a loop to look up (and if the player has multiple objects with the same display name, it will use the one they picked, not the first with the matching display name).


Jay Nabonne
05 Feb 2017, 13:21

Also, keep in mind that with the foreach loop, if you destroy the object inside the loop, you will likely get a runtime error message,as the list will get modified when you delete the object, and Quest doesn't like lists being modified when it's iterating over them. (Not confirmed this will happen, but a suspicion.)