Referencing Items in a String List

Shadecerule
29 Jul 2018, 04:46

Is there a way to reference an item in a string list?

For instance, stringList has "hi", "hello", and "bye". What can I use to reference "hi"? Like when I want to ask a question and check if(result = stringList[0]) or something.


mrangel
29 Jul 2018, 09:40

It looks like you've got the answer already

if(result = stringList[0]) {

Looks fine to me.
You could also use if (result = ListItem (stringList, 0)) {

If this isn't working, could it be a scope issue?
You mention result, so I'm assuming this is a script that uses GetInput, Ask, ShowMenu, or similar. Remember that for those functions, the script that handles the result has access to a single variable, result. It does not have access to any variables created outside of that code block.

So if you're creating a stringList and then displaying a menu, you will need to make the list an attribute of some object so that you can still reach it.


Shadecerule
29 Jul 2018, 12:39

I guess the reason I asked is because if(result = stringList[0]) doesn't work. :P

That explains why it doesn't, though. Is there a way that I can use a dictionary for this, to cut the code I have to write when using ShowMenu?


mrangel
29 Jul 2018, 14:00

I think the question would be what you're trying to accomplish. Like, where does this stringlist come from, and why do you want to check if the user's selection is the first item in the list?


The Pixie
29 Jul 2018, 14:31

Getting things from lists can be a little odd occasionally because Quest does not know what the thing is at first. You might find this works:

s = stringList[0])
if(result = s)

Assigning it to a variable forces Quest to esablish it is a string. Or use StringListItem, which again forces the issue:

if(result = StringListItem(stringList, 0))

Or it could be something else entirely...


Shadecerule
29 Jul 2018, 17:21

As an example, this is essentially what I am trying to do:

  hiList = NewStringList()
  list add (hiList, "\"Hi.\"")
  list add (hiList, "\"Bye.\"")
  ShowMenu ("", hiList, false) {
    if (result = hiList[0]) {
      msg ("\"Hi,\" you say.")
    }
    else {
      msg ("\"Bye,\" you say.")
    }

I'm trying to check this way because if (result = "\"Hello\"") doesn't work; Quest doesn't like it when you put \" inside of an if check. I could technically do something like if (result = "Say hello."), but this isn't preferable because it doesn't allow for more complex dialogue choices. Lastly, the reason I'm using ShowMenu is because I need to block the player from taking any actions while the dialogue is occurring.


mrangel
29 Jul 2018, 19:46

Quest doesn't like it when you put " inside of an if check.

I've never come across that problem. But in that case, how about…

Silly way of doing this
ShowMenu ("", Split ("\"Hi.\";\"Bye.\""), false) {
  switch (Replace (result, "\"", "")) {
    case ("Hi.") {
      msg ("\"Hi,\" you say.")
    }
    case ("Bye.") {
      msg ("\"Bye,\" you say.")
    }
  }
}

Or put the string in a variable so it's not directly inside the 'if' statement:

ShowMenu ("", Split ("\"Hi.\";\"Bye.\""), false) {
  affirmative_answer = "\"Hi.\""
  if (result = affirmative_answer) {
    msg ("\"Hi,\" you say.")
  }
  else {
    msg ("\"Bye,\" you say.")
  }
}

Or use the dictionary form of ShowMenu:

  options = NewStringDictionary()
  dictionary add (options, "hi", "\"Hi.\"")
  dictionary add (options, "bye", "\"Bye.\"")
  ShowMenu ("", options, false) {
    if (result = "hi") {
      msg ("\"Hi,\" you say.")
    }
    else {
      msg ("\"Bye,\" you say.")
    }
  }

(Or do what I usually do: use the 'curly quotes' and within a string. They don't have any special meaning to Quest, to the XML parser, or to your web browser. So you don't need to escape them or anything)


Shadecerule
30 Jul 2018, 02:34

Weird... Maybe I've been encountering a bug, then. But I'll give those a try, thanks!