The only way is to pass a string with the name of the array and construct the reference you want. Which you have already discovered.
Everything is a string. Arrays, numbers, strings… everything. You cannot pass a reference to an array because whatever you pass is a string anyway…
The choice script engine runs on JavaScript which parses the textfile to run operations, this means it has a lot of ‘quirks’ to put it politely
In your example, we could condense the code to:
Code
utils.txt
==============================
*label print_n_item_in_array
*params array n
>Entered subroutine
${{array&(“_”&n)}}
*return
But afaik, this is the only way it will work.
create_array is global but temp_array is scoped to the scene. So once a temp_array has been defined calling gosub will mean it is still in scope, but calling gosub_scene or calling return to a different scene will lose it. But remember, everything is a string, so if you store any values in a global variable they won’t lose reference.
Hope this helps!? If you have a particularly complicated use case just let us know and I’m sure we can figure something out!
There is no real array in ChoiceScript. Behind the screen, it just creates a bunch of variables with a suffix. So there really isn’t any variable called genders. But as you figured, you can passa the name of the array as string.
It’s a bit of a shame that there isn’t a more elegant way of doing it but ah well haha, I guess ChoiceScript wasn’t really designed for it.
Appreciate the pointers! Unfortunately I don’t think it will really work for me, I wanted to create some util functions that would take in temp arrays, so I wouldn’t end up with 100 lines of *create_array in my startup file. Guess I will just have to be more organised
You can have a working array. You copy your temp array into the global working one and “send” it to the util scene. Also, I think you’ll appreciate CS_Lib, take a look!