Passing *create_array/*temp_array variables as input params

Does anyone know if it’s possible to pass arrays (made using *create_array or *temp_array) as subroutine input params?

Seems like it won’t even enter the subroutine, it immediately displays an error:

show code
startup.txt
================================================
*title scratchpad
*author Dioti

*scene_list
	startup

*create implicit_control_flow true
*create_array genders 3 "M" "F" "NB"

*gosub_scene utils print_n_item_in_array genders 2

*finish
utils.txt
================================================
*label print_n_item_in_array
*params array n

>Entered subroutine
*line_break
${array[n]}

*return

The only way I’ve been able to get it to work is by passing the name of the array variable and deriving the individual variable names for each item.

show code
startup.txt
================================================
*title scratchpad
*author Dioti

*scene_list
	startup

*create implicit_control_flow true
*create_array genders 3 "M" "F" "NB"

*gosub_scene utils print_n_item_in_array "genders" 2

*finish
utils.txt
================================================
*label print_n_item_in_array
*params array_name n

>Entered subroutine
*line_break
*temp item_name (array_name&"_")&n
item_name: ${item_name}
*line_break
item_value: ${{item_name}}

*return

The example above is pretty simple but if the subroutine does something more complicated, it can get pretty messy.

Additionally, it means you can only use global arrays (created using *create_array) but not temp arrays, which is a pretty big limitation.

Is there a proper way of doing this?

because there is no variable “genders”

so you have to pass the variable as “genders_1”, “genders_2” or “genders_3”

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 :slight_smile:

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!

1 Like

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.

Thanks for the help everyone!!

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! :slight_smile: 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 :laughing:

1 Like

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!

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.