Hi, I’m having trouble with the parameter commands! I was trying to adapt this code to fit with the use of create array as an experiment and/or to familiarize myself with the code.
The exact error I’m getting is Non-existent variable 'array_1'
The idea is to present a few possible paths forward in a forest, having them be randomized by the array, so that you are presented with a choice of two (as a simple proof of concept). Because of that, this is all in my startup.txt, and I’m not sure if that’s the problem?
The error occurs on the first line in which I use the “array” param- it parses array_length fine, it seems, but doesn’t read the paramater that was supposed to be pushed to “array”, so it just reads “array_X” instead of “startingpath_X” as intended.
I’ve read the param documentation, and checked on here for other people with this issue, but the one I found was a problem with using camelcase, which I am not doing, so I’m lost.
In an attempt to do some basic troubleshooting, I tried working with params on something very basic:
*gosub visit "Dracula" "garlic"
*label visit
*params person gift
You go visit ${person} and you bring ${gift} with you.
*return
*choice
#yes
#no
*finish
This doesn’t work. It tells me that no parameters were passed. I used the example in the documentation about how to work with Param and Gosub, so I’m kind of at a loss as to what I’m doing wrong.
I’m not sure why the debug test isn’t working, but I think there’s an issue with this line: “startingpath” is in quotation marks, so you’re not passing the variable, you’re passing a string.
Be careful where you place your subroutines. ChoiceScript reads top to bottom. Subroutines are just labels with a *return statement. ChoiceScript doesn’t distinguish normal labels from subroutine labels, so it will read every line of the file and when it hits the *param command it will thow this error. Move all subroutines either to a different file or to the end of the file after a *finish statement.
Actually, passing it as string it the right way.
That’s because everything in ChoiceScript is a string. There’s no real array as you may understand it from other programing language. It’s just syntax sugar to creating many variables with a prefix. When you use array syntax, it just concatenates the names with an underscore. So array[1] is the same as array_1. It’s not going to parse array as a variable pointing to an array in memory (even because the array doesn’t exist).
To do what you want, you can’t use array syntax with reference variables, but instead do the concatenation manually (or use interpolation as below).
Grah, this just keeps tripping me up. (But isn’t it passing the value of the variable to the subroutine? What good does giving it the variable name as a string do?)
Yes, you’re passing the values. In this case there’s no variable called startingpath because *create_array will create many variables with the prefix but no variable actually called “startingpath” (only startingpath_1, startingpath_2, etc). You want to pass it as a string so you can concatenate inside the subroutine.