Question about a Stringlist and an If-Script/Switch Script [S-S-SOLVED]

Anonynn
24 Aug 2018, 17:49

Hello!

I just had a question regarding a stringlist and using it with an if-script/switch script. I have an NPC that can change it's gender depending on what the player requests for them to be: male or female. I know that you reference a stringlist in a script like this...

You can see a "+ npc.newgender +" blah blah...

but how would I reference that stringlist in an if-script or a switch script?

Like this?

if (npc.newgender="male") {
}
else if (npc.newgender="female") {
}

or

switch (npc.newgender) {
  case ("male") {
    msg ("Blah blah description here.")
  }

Thanks in advance for the help!

Anonynn.


mrangel
24 Aug 2018, 18:52

Not sure I understand the question. I don't see a stringlist there.

I'm assuming the newgender attribute is a string which is either "male" or "female". In which case your if and switch examples are exactly the same (although the switch version is more efficient).

Is this attribute actually a stringlist? I'm not sure what use that would be.


Anonynn
24 Aug 2018, 21:22

Hello!

Sorry for not clarifying. Let's say a stringlist like this...

"male"
"female"
"other"

which the player can set.

How could I properly display that stringlist attribute in an if-script/switch script?

Anonynn.


mrangel
24 Aug 2018, 23:38

I still don't understand what you're trying to do.

Normally if you choose the gender of a character (whether the player character or an NPC), you would choose one option from the list. And then the chosen option would be stored in a string attribute. If it's a stringlist, does that mean you're allowing the player to choose multiple options?

Or do you mean that the player is picking one option from a stringlist? In that case, the value they've picked is a string; the fact that it was chosen from a list shouldn't affect the way you treat the attribute.

I'm probably missing something obvious here; hopefully someone can understand a little better.


Anonynn
25 Aug 2018, 00:14

Hey!

I think I figured it out. I just made a switch script with

 npc.newgender="genderhere"

for each of the descriptions and that seems to have worked. Sorry that I wasn't very clear about all that. @_@

Anonynn.


hegemonkhan
25 Aug 2018, 07:30

to use String Lists, a simple example:


stringlist_variable = Split ("red;blue;yellow", ";")

player.favorite_color_string_attribute = StringListItem (stringlist_variable, 0)
// player.favorite_color_string_attribute = "red"

player.favorite_color_string_attribute = StringListItem (stringlist_variable, 1)
// player.favorite_color_string_attribute = "blue"

player.favorite_color_string_attribute = StringListItem (stringlist_variable, 2)
// player.favorite_color_string_attribute = "yellow"

player.favorite_color_string_attribute = StringListItem (stringlist_variable, 3)
// ERROR! There is NO 4th item!

player.favorite_color_string_attribute = StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1))
// player.favorite_color_string_attribute = [random selection: "red"/"blue"/"yellow"]


how to then use the selected item, in an if/switch script:

(continuing in using my example above for this example)

// using if:

stringlist_variable = Split ("red;blue;yellow", ";")

player.favorite_color_string_attribute = StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1))

if (player.favorite_color_string_attribute = "red") {
  msg ("RED")
} else if (player.favorite_color_string_attribute = "blue") {
  msg ("BLUE")
} else if (player.favorite_color_string_attribute = "yellow") {
  msg ("YELLOW")
}

// or (but this is really inefficient - don't do this method with the 'if' Script, and... it re-randomly selects the value each time! Unless, you do want it to re-randomly select the value each time, lol. But otherwise, it's better to store the randomly selected value into a VARIABLE, and then you use that VARIABLE in the 'if' script, as that way it's only doing the random selection operation/s once, and not each time, which is why it's really inefficient), just to see how it works with the 'if' script, here's an example:

if (StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1)) = "red") {
  msg ("RED")
} else if (StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1)) = "blue") {
  msg ("BLUE")
} else if (StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1)) = "yellow") {
  msg ("YELLOW")
} else {
  msg ("FAILED to have any matches, for each re-selection and 'if/else-if' check)
}

// ----------------------------------------------------------------------

// using switch:

stringlist_variable = Split ("red;blue;yellow", ";")

player.favorite_color_string_attribute = StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1))

switch (player.favorite_color_string_attribute) {
  case ("red") {
    msg ("RED")
  }
  case ("blue") {
    msg ("BLUE")
  }
  case ("yellow") {
    msg ("YELLOW")
  }
}

// or (since the 'switch' already separates it out, this is NOT inefficient and bad to do like it is with the 'if' Script -- unless you wanted it to re-randomly select each time of course. With the 'switch' Script you can't as easily code for it to re-randomly select, as you can / as it does, with the 'if' script):

switch (StringListItem (stringlist_variable, GetRandomInt (0, ListCount (stringlist_variable) - 1))) {
  case ("red") {
    msg ("RED")
  }
  case ("blue") {
    msg ("BLUE")
  }
  case ("yellow") {
    msg ("YELLOW")
  }
}


here's some links on using lists and dictionaries:

http://docs.textadventures.co.uk/quest/using_lists.html
http://docs.textadventures.co.uk/quest/using_dictionaries.html

http://textadventures.co.uk/forum/samples/topic/5137/list-and-dictionary-extensive-guide-by-hk

http://textadventures.co.uk/forum/quest/topic/qrlgh2op_eiwnfheoe3vng/any-way-to-make-a-limit-for-an-attribute-so-that-it-wouldnt-rise-fall-after-a-ce#ae2d6e65-4999-4320-8828-d1edb27d63dd (scroll down to my 'Conceptually About' (I again explain about Lists and Dictionaries here --- might be more clear than my older post in the 'hk extensive list and dictionary guide' link above)


hegemonkhan
25 Aug 2018, 08:00

P.S.

I believe with Pixie's new quest versions, there's the:

'PickOne' Script/Function, which will randomly select an item from the list


HK edit:

err... almost... PickOneString, PickOneObject, etc...

http://docs.textadventures.co.uk/quest/functions/index_allfunctions.html (scroll down to the 'P' section/category)


otherwise, if you want to select a certain/specific item, you just (there's a bit more steps/understanding to doing it though - ask for help if you can't figure out how to) use these Scripts/Functions:

ListItem, StringListItem, or ObjectListItem

DictionaryItem, StringDictionaryItem, ObjectDictionaryItem, or ScriptDictionaryItem


hegemonkhan
25 Aug 2018, 08:12

P.S.S.

you can also use a menu ('show menu / ShowMenu') with a list, and then have your 'if/switch':

stringlist_variable = Split ("red;blue;yellow", ";")

// using 'switch':

show menu ("Color?", stringlist_variable, false) {
  switch (result) {
    case ("red") {
      msg ("RED")
    }
    case ("blue") {
      msg ("BLUE")
    }
    case ("yellow") {
      msg ("YELLOW")
    }
  }
}

// using 'if':

// using 'switch':

show menu ("Color?", stringlist_variable, false) {
  if (result ="red") {
    msg ("RED")
  } else if (result = "blue") {
    msg ("BLUE")
  } else if (result ="yellow") {
    msg ("YELLOW")
  }
}

a 'get input' example (there's many designs/variations, so this is just one example):

stringlist_variable = Split ("red;blue;yellow", ";")

// using 'if':

list_count_integer_variable = ListCount (stringlist_variable)

msg ("Pick a number: 1 to " + list_count_integer_variable)

get input {
  if (IsInt (result)) {
    input_integer_variable = ToInt (result)
    if (input_integer_variable > 0 and input_integer_variable <= list_count_integer_variable) {
      switch (StringListItem (stringlist_variable, input_integer_variable - 1)) {
        case ("red") {
          msg ("RED")
        }
        case ("blue") {
          msg ("BLUE")
        }
        case ("yellow") {
          msg ("yellow")
        }
      }
    }
  }
}