Getting the Highest of a Set of Attributes?

Niekitty
18 Oct 2016, 01:00

Yet another coding question... sooner or later I'm either going to understand this stuff fully, or you'll all be sick of me and boot me out. :D

I have a list of stats on the player character, including a couple of (very) basic personality traits (player choices create variations in NPC response and protagonist dialog, etc.). My question is: is there way to sort through, say, five player.attributes and pick out the highest one, or lowest one, or the like, without building the Leaning Tower of If?

So... quick check to see if player.psAggr, player.psCurio, or player.psCompa is higher, and then perform a different action (that part would take an If, i know) based on the result?


hegemonkhan
18 Oct 2016, 06:59

the trick is to put your Attribute names into a String List Attribute, which then allows you to iterate through all of them, checking which is highest, lowest, etc.

a slightly more complex example (to see more on this type of stuff and for more versatility/utility/usage):

(hopefully, the code design is correct, with no syntax or other errors/issues)

<object name="room">
</object>

<object name="player">
  <attr name="parent" type="object">room</attr>
  <attr name="strength_integer_attribute" type="int">70</attr>
  <attr name="endurance_integer_attribute" type="int">60</attr>
  <attr name="dexterity_integer_attribute" type="int">50</attr>
  <attr name="agility_integer_attribute" type="int">40</attr>
  <attr name="speed_integer_attribute" type="int">30</attr>
</object>

<object name="global_data_object">
  <attr name="primary_attribute_stringlist_attribute" type="simplestringlist">strength; endurance; dexterity; agility; speed</attr>
</object>

// the one issue is if you have Attributes tied for max value, the function below will only use/return the list-first-occuring Attribute:

<function name="max_attribute_function" parameters="object_parameter, list_object_parameter, list_attribute_parameter, string_ending_parameter" type="string">
  max_string_variable = StringListItem (GetAttribute (list_object_parameter, list_attribute_parameter), 0)
  foreach (attribute_variable, GetAttribute (list_object_parameter, list_attribute_parameter)) {
    if (GetInt (object_parameter, attribute_variable + string_ending_parameter) > GetInt (object_parameter, max_string_variable + ending_string_parameter)) {
      max_string_variable = attribute_variable
    }
  }
  return (max_string_variable)
</function>

// using ifs/switch isn't the most efficient method if you got lots of Attributes (using a Script Dictionary would be more efficient, but it's a bit more complex/advanced than using ifs/switch), but this is just for/as an example of doing actions/events with it:

<function name="max_attribute_event_function" parameters="object_parameter, max_string_parameter, string_ending_parameter">
  switch (max_string_parameter) {
    case ("strength") {
      msg (object_parameter.name + " " + max_string_parameter + ": " + GetInt (object_parameter, max_string_parameter + string_ending_parameter))
    }
    case ("endurance") {
      msg (object_parameter.name + " " + max_string_parameter + ": " + GetInt (object_parameter, max_string_parameter + string_ending_parameter))
    }
    case ("dexterity") {
      msg (object_parameter.name + " " + max_string_parameter + ": " + GetInt (object_parameter, max_string_parameter + string_ending_parameter))
    }
    case ("agility") {
      msg (object_parameter.name + " " + max_string_parameter + ": " + GetInt (object_parameter, max_string_parameter + string_ending_parameter))
    }
    case ("speed") {
      msg (object_parameter.name + " " + max_string_parameter + ": " + GetInt (object_parameter, max_string_parameter + string_ending_parameter))
    }
  }
</function>

// the one issue is if you have Attributes tied for min value, the function below will only use/return the list-first-occuring Attribute:

<function name="min_attribute_function" parameters="object_parameter, list_object_parameter, list_attribute_parameter, string_ending_parameter" type="string">
  min_string_variable = StringListItem (GetAttribute (list_object_parameter, list_attribute_parameter), 0)
  foreach (attribute_variable, GetAttribute (list_object_parameter, list_attribute_parameter)) {
    if (GetInt (object_parameter, attribute_variable + string_ending_parameter) < GetInt (object_parameter, min_string_variable + ending_string_parameter)) {
      min_string_variable = attribute_variable
    }
  }
  return (min_string_variable)
</function>

// using ifs/switch isn't the most efficient method if you got lots of Attributes (using a Script Dictionary would be more efficient, but it's a bit more complex/advanced than using ifs/switch), but this is just for/as an example of doing actions/events with it:

<function name="min_attribute_event_function" parameters="object_parameter, min_string_parameter, string_ending_parameter">
  switch (min_string_parameter) {
    case ("strength") {
      msg (object_parameter.name + " " + min_string_parameter + ": " + GetInt (object_parameter, min_string_parameter + string_ending_parameter))
    }
    case ("endurance") {
      msg (object_parameter.name + " " + min_string_parameter + ": " + GetInt (object_parameter, min_string_parameter + string_ending_parameter))
    }
    case ("dexterity") {
      msg (object_parameter.name + " " + min_string_parameter + ": " + GetInt (object_parameter, min_string_parameter + string_ending_parameter))
    }
    case ("agility") {
      msg (object_parameter.name + " " + min_string_parameter + ": " + GetInt (object_parameter, min_string_parameter + string_ending_parameter))
    }
    case ("speed") {
      msg (object_parameter.name + " " + min_string_parameter + ": " + GetInt (object_parameter, min_string_parameter + string_ending_parameter))
    }
  }
</function>

// an example of using (calling) it (the Functions):

<game name="example">
  <attr name="start" type="script">

    max_string_variable = max_attribute_function (player, global_data_object, primary_attribute_stringlist_attribute, "_integer_attribute")

    max_attribute_event_function (player, max_string_variable, "_integer_attribute")

    min_string_variable = min_attribute_function (player, global_data_object, primary_attribute_stringlist_attribute, "_integer_attribute")

    min_attribute_event_function (player, min_string_variable, "_integer_attribute")

  </attr>
</game>

The Pixie
18 Oct 2016, 07:03

I would start with a string list that has all but one of their names, which you could set up as an attribute itself, or include in the code. Use the remaining attribute to initialise, then iterate through the list:

l = Split("psCurio;psCompa ;psSomethingelse", ";")
highest_att = "psAggr"
highest_value = player.psAggr
foreach (s, l) {
  if (GetInt(player, s) > highest_value) {
    highest_att = s
    highest_value = GetInt(player, s)
  }
}
switch (highest_att) {
  case ("psAggr") {
    ...

Niekitty
19 Oct 2016, 22:31

...........................................
Okay... XD
Thank you both. I think this will all help once I can translate it into a shape in my head. :D
I'm not familiar with all of the switch and case stuff yet, so I think my learning curve just asked for climbing gear again.