Shockingly stupid List question

I am running into a problem with something so basic that it’s driving me nuts. I can’t seem to get the right output from a list.

msgList = [msg1, msg2]
msg1 = "Hi. "
msg2 = "Hello. "
displayMsg()
{
    msgList[1];
}

As far as I understand, executing displayMsg() here should print "Hi. ". And yet, it prints “Hello. Hi.” It prints BOTH, and backwards, for some godforsaken reason. I have no idea why it does either. Moreover, I noticed that I can seemingly put any number inside the msgList[*] and it makes no difference. msgList[1], msgList[2] and msgList[56416816516516] result in the exact same output.

¯_(ツ)_/¯

1 Like

This appears to be the double quotes messing things up. When I added that code snip to a game I am working on, I got the same results.

msgList = [msg1, msg2]
msg1 = 'Hi. '
msg2 = 'Hello. '
displayMsg()
{
    say(msgList[1]);  // or "<<msgList[1]>>";
}

This modified version returns the results you were expecting.

Thank you! I’ll be damned if I understand why it’s doing that, but at least that solves that.

While I’m at it, is there some way to quickly retrieve a random element from a list? I could do something like

msgList = [msg1, msg2]
msg1 = 'Hi. '
msg2 = 'Hello. '
displayMsg()
{
           local roll = rand(4);
            if (roll == 0)
                roll += 1;
            if (roll == 3)
                roll -= 1;
            "<<naMsgList[roll]>>";
}

…but I was halfway through typing that when I went “no, wait, this is stupid.”

Double quotes translate to code that calls the say function. They are not strings. This:

"Hi."

is pretty much:

{
    say('Hi.');
}

So

msg1 = "Hi. "

is actually:

msg1 = { say('Hi.'); }

And weird things will happen.

Use single quotes when you want string values. Use double quotes when you want to print text without having to manually call say().

The TADS 3 rand statement will accept a list (or a variable of one). So rand(['Hi. ','Hello. ']) or rand(msgList) will return one the items in the list. TADS 3, when running in debug mode, uses a set seed so the randomization will be the same each run. Which is great for testing.

Thank you very much!