Code Design help needed

hegemonkhan
11 Jul 2016, 21:39

@Jay, Pixie, Pertex, etc/or whoever else is a good programmer:

Right now I got massive ugly code redundency Functions, and would like to be able to consolidate them into a single scripting that can dynamically handle those slightly different Functions I have way to many of in redundency.

what I need:

1A. able to construct a 'call scripting' line for dynamic-ness of it // this doesn't seem to be possible of with a 'call Function' scripting line.
1B. able to take/use/get arguments/parameters (for both: the dynamic construction of 'call scripting' and for any normal VARIABLES needed by/within the scripting)

  1. able to call that scripting again (loop)

Can this be done with Functions?
or
Can this be done with Objects and Delegates?
or
Is there anotehr way this can be done? (I hope this is somehow possible to do... as I really need to wipe out this code redundency of mine, lol)


for an example of what I need:

(hopefully this demonstrates what I mean, as in terms of what I need for the dynamic-ness)

// list arguments/parameters:
<attr name="sex_stringlist_attribute" type="simplestringlist">male;female</attr>
<attr name="race_stringlist_attribute" type="simplestringlist">european;asian;arabian;african;american</attr>
<attr name="class_scripting_attribute" type="simplestringlist">knight;berserker;thief;wizard;cleric</attr>
<attr name="specialization_scripting_attribute" type="simplestringlist">combat;magic;stealth;diplomacy;science</attr>
<etc etc etc>

// name_of_scripting_argument/parameter: the individual scriptings that I'd like to be able to combine (put into a dynamic scripting that can handle them):
<sex_scripting>
<race_scripting>
<class_scripting>
<specialization_scripting>
<etc etc etc>

// character_argument/parameter:
<game.pov>
<orc_1>
<etc etc etc>

// attribute_argument/parameter:
<sex_string_attribute>
<race_string_attribute>
<etc etc etc>

// name_of_dynamic_scripting_argument/parameter:
dynamic_scripting (list_argument, name_of_scripting_argument, name_of_dynamic_scripting_argument, character_argument, attribute_argument)

<dynamic_scripting parameters="list_parameter, name_of_scripting_parameter, name_of_dynamic_scripting_parameter, character_parameter, attribute_parameter">
  menu (list_parameter)
  if (result = value1) {
    name_of_scripting_parameter = value1
  } else if (result = value2) {
    name_of_scripting_parameter = value2
  // etc 'else ifs' as needed
  } else {
    call (name_of_dynamic_scripting_parameter (list_parameter, name_of_scripting_parameter, name_of_dynamic_scripting_parameter, character_parameter, attribute_parameter))
  }
  call (name_of_scripting_parameter (character_parameter, attribute_parameter))
<dynamic_scripting>

as right now, I got individual Functions for each of these things, which is massive code redundency/inefficiency, and thus I'd like a single scripting that can dynmaically handle all/any of (what are currently) these individual Functions (or whatever else, such as Object+Delegate, or whatever other means there are/is)


hegemonkhan
11 Jul 2016, 22:15

err... let me give some (near) actual examples (as my made-up example is probably not fully accurate in demonstrating what I need):

how can I craft a single scripting (Function/Object+Delegate/whatever other means/method) to dynamically handle both of examples (I currently created Functions, but I can hopefully change these into some thing else, like Objects+Delegates or whatver else is available)?

(I have many many more than just two Functions/Scriptings, and thus why I need a scripting that can dynamically handle them, to reduce this massively ugly/horrendous code redundency)

<game name="example">
  <attr name="sex_stringlist_attribute" type="simplestringlist">male;female</attr>
  <attr name="age_stringlist_attribute" type="simplestringlist">zygote;embryo;baby;child;teen;adult;corpse</attr>
  <attr name="start" type="script">
    sex_string_function (player)
    on ready {
      age_string_function (player)
    }
  </attr>
</game>

<object name="player">
  <attr name="sex_string_attribute" type="string">unknown</attr>
  <attr name="age_string_attribute" type="string">unknown</attr>
</object>

<function name="sex_string_function" parameters="character_parameter"><![CDATA[
  msg ("Sex?")
  numbering_integer_variable  = 0
  msg (numbering_integer_variable + ". random selection")
  foreach (option_string_variable, game.sex_stringlist_attribute) {
    numbering_integer_variable = numbering_integer_variable + 1
    msg (numbering_integer_variable + ". " + option_string_variable)
  }
  get input {
    ClearScreen
    if (IsInt (result)) {
      if (ToInt (result) > 0 and ToInt (result) < ListCount (game.sex_stringlist_attribute)) {
        character_parameter.sex_string_attribute = StringListItem (game.sex_stringlist_attribute, ToInt (result) - 1)
      } else if (ToInt (result) = 0) {
        character_parameter.sex_string_attribute = StringListItem (game.sex_stringlist_attribute, GetRandomInt (0, ListCount (game.sex_stringlist_attribute) - 1)
      } else {
        sex_string_function (character_parameter)
      }
    } else {
      sex_string_function (character_parameter)
    }
  }
]]></function>

<function name="age_string_function" parameters="character_parameter"><![CDATA[
  msg ("Age?")
  numbering_integer_variable  = 0
  msg (numbering_integer_variable + ". random selection")
  foreach (option_string_variable, game.age_stringlist_attribute) {
    numbering_integer_variable = numbering_integer_variable + 1
    msg (numbering_integer_variable + ". " + option_string_variable)
  }
  get input {
    ClearScreen
    if (IsInt (result)) {
      if (ToInt (result) > 0 and ToInt (result) < ListCount (game.age_stringlist_attribute)) {
        character_parameter.age_string_attribute = StringListItem (game.age_stringlist_attribute, ToInt (result) - 1)
      } else if (ToInt (result) = 0) {
        character_parameter.age_string_attribute = StringListItem (game.age_stringlist_attribute, GetRandomInt (0, ListCount (game.age_stringlist_attribute) - 1)
      } else {
        age_string_function (character_parameter)
      }
    } else {
      age_string_function (character_parameter)
    }
  }
]]></function>

P.S.

I'm aware that my custom menu code lines, could be put inside of a Function or an Object+Delegate or whatever else, and I probably will do so, as I'm very likely to be using this in other totally different places/scriptings. Though, if this wasn't the case, maybe it'd be better to leave the code lines as they are, instead of putting them into a Function/whatever, at least this is my guess at it being better/more efficient (no "overhead" that would come with the extra Function for putting these code lines inside of).


The Pixie
12 Jul 2016, 07:15

I would suggest using ShowMenu rather than get input. That will guarantee that result will have a valid value; no need to check if it is valid and code for it not. I would also not bother to send the player object to your functions, because they will only ever do this with the player object, and it will be easier to read if it says player, rather than character_parameter.

Here is the code refactored into one function:

<!--Saved by Quest 5.6.5783.24153-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="example">
    <sex_stringlist_attribute type="stringlist">
      <value>male</value>
      <value>female</value>
    </sex_stringlist_attribute>
    <age_stringlist_attribute type="stringlist">
      <value>zygote</value>
      <value>embryo</value>
      <value>baby</value>
      <value>child</value>
      <value>teen</value>
      <value>adult</value>
      <value>corpse</value>
    </age_stringlist_attribute>
    <start type="script">
      string_function ("Sex?", "sex_string_attribute", game.sex_stringlist_attribute)
      on ready {
        string_function ("Age?", "age_string_attribute", game.age_stringlist_attribute)
      }
    </start>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <object name="player">
      <attr name="sex_string_attribute">unknown</attr>
      <attr name="age_string_attribute">unknown</attr>
    </object>
  </object>
  <function name="string_function" parameters="prompt, att, lst"><![CDATA[
    msg (prompt)
    numbering_integer_variable = 0
    msg (numbering_integer_variable + ". random selection")
    foreach (option_string_variable, lst) {
      numbering_integer_variable = numbering_integer_variable + 1
      msg (numbering_integer_variable + ". " + option_string_variable)
    }
    get input {
      ClearScreen
      if (IsInt (result)) {
        if (ToInt (result) > 0 and ToInt (result) <= ListCount (lst)) {
          set(player, att, StringListItem (lst, ToInt (result) - 1))
        }
        else if (ToInt (result) = 0) {
          set(player, att, StringListItem (lst, GetRandomInt (0, ListCount (lst) - 1)))
        }
        else {
          string_function (prompt, att, lst)
        }
      }
      else {
        string_function (prompt, att, lst)
      }
    }
  ]]></function>
</asl>

However, you could also take a look at this, which I did for Anonynn who has a similar need:
http://textadventures.co.uk/forum/samples/topic/5492/character-creation-questions-again

Each question is set up as an object, and the function iteratyes recursively through them. You can attach a script to the object, so later questions can change, depending on previous replies (in her case, if you choose to be dragon-descended you will get asked what sort of tail you want).


hegemonkhan
12 Jul 2016, 08:41

Thank you Pixie!

Geez... was I blind... I didn't realize that by making it into a single scripting, I wouldn't have to worry about how to construct the 'call scripting', as there's only it, no need for any dynamic construction of the correct 'call scripting'... unless I have/need the scripting calling upon another scripting...hmm... How would this done? I was also blind at how simple/easy the arguments/parameters were... not sure why I was so confused... grrr.

I'm now getting the idea that I can have this scripting inside of a 'foreach'.... HK grins evilly, I was having a 'coding-writer's block', thanks for helping me get out of it, Pixie!

And I definately have to study your library to see how you've set it all up! Definately will be something useful for me, as I try to transition from Function/procedural to Objects~Delegates/object-oriented. I still don't really have the object-oriented mindset/thought process, sighs. It's still a bit alien to me to think/work in this way, compared to procedural and its use of Functions.