Does Choicescript have any indexing functions?


#1

For my current project, I have a thing set up so that every time the player checks their phone messages, they might get a random message from a telemarketer or a friend or something. This is all coded using the *rand function, and everything runs smooth so I don’t need help with that.

My problem is that if the *rand function produces the same number twice in one playthrough, it’ll play the same message again, which I don’t want to happen. Obviously I could just make a boolean (true/false) flag for each scene that indicates if it’s already been played, but that will become very clunky very fast as I have over 100 of these things.

So my question is if CS allows us to use indexes for variables? Being able to simply add an already played scene’s numeric value to an index and then check that index instead of one of a hundred booleans would be hella nice, but if CS has that functionality, I don’t know what it is. And so I ask you!


#2

You can use curly braces, as described in the last section of the Advanced ChoiceScript guide. http://www.choiceofgames.com/make-your-own-games/choicescript-advanced/

Like this: (but this coded is untested)


*create message1 "foo"
*create message2 "bar"
*rand number 1 2
*set message {"message" & number}
${message}


#3

Ah, I had forgotten about that. This should be just what I needed. Thank you for your quick help.


#4

Okay so I keep getting an error that I absolutely cannot figure out. My test code is as follows.

*create index “”
{index} \*set index {"{index}"&“1”}
${index}

Which presumably should work unless I have the format wrong. However, I keep getting the error: (line 3: Non-existent variable ‘1’) which makes no sense because that 1 is a string. I’ve also tried putting it as just a number ({"${index}"&1}) but I get the same issue.

I tried changing line 3 to (*set index “{${index}&1}”) but all it returns is {&1} which is silly. Adding spaces around the & similarly has no effect, returning { & 1}.

Also another question: Is there a way to use “wildcards” in the code?


#5

*set index {"${index}"&"1"}

On the third line, the outermost brackets are telling choicescript to set index to the value of whatever variable is in them - in your case, a variable called “1”. Removing those should fix that problem and let “index” be set to the value of index with “1” attached.

*set index ("${index}"&"1")

This will give you the value of index with “1” attached at the end. As far as I know, wildcards (as in “*1”) can’t be used in ChoiceScript. But it would be nice. :slight_smile:


#6

Cool, it’s working now! Thanks for your help. Bummer that there are no wildcards, but thanks anyway.


#7

You can’t use wildcards but you could use loops and clever conditionals to change multiple “indexes” at the same time.


*temp n 0
*temp index_count
*label loop
*set n + 1
*setref "index_"&n true
*if (n > index_count)
 *finish
*else
 *goto loop

I didn’t test that code but it should set index_1 through to index_10 to true.
You could easily change that to any other prefix, a different set value or amount of indexes - and with some tweaking emulate other wildcard(ish) behaviour :slight_smile: