Functions with parameters and return values in choice script?

Hi,
I was wondering if there’s any way to create functions with peramitors and return values in choice script? Like, for example, you could have a function to display a preset list of colors and also an option to enter a color yourself, and it could then return a string containing the preset color you chose or the color you entered. You could then set like, the eye color of the player to the return value of that function. I know gosub can sort of do this, but there’s no way as far as I know to specify peramitors and a return value with gosub unless you get creative with variables.

As far as I know, Choice Script doesn’t allow you to create functions with parameters or return values, exactly. There’s also not really variable scoping like that would imply, either. You can, however, create subroutines that will change global state. Check out the *gosub command in the docs. I hope that helps.

You can’t declare parameters or have return values in ChoiceScript. At least not exactly. You can do something like that, however. You may have already thought of doing it this way, since you mentioned getting creative with variables. This is sort of what I’ve been doing. First, make sure to declare some variables in your startup.

*set parameter1 0
*set parameter2 0
*set util_return 0

Then we can establish our parameters and call a subroutine somewhere:

*set parameter1 5
*set parameter2 3
*gosub utilities math_function
The number is ${util_return}.

So I’ve set two parameters I’m using for my function. Then I’m going to the label math_function in the scene utilities. It’s easier to create a separate scene for functions you’ll be calling in multiple scenes, so your function will be in one place. Inside my utilities scene, I have something like this:

*label math_function
*set util_return ( parameter1 + parameter2 )
*return

And that’s it!

1 Like

Yeah, that’s what I meant about getting creative with variables and it looks like that’s what I’ll have to do. Although I was looking through the choice of robots code a little earlier to see how he did the stat increase thing where it would say, for example, (++Grace) or something like that and it looks like he was able to do parameters with gosub, but I’m not sure how.

Choice of Robots uses different JavaScript that’s been customized from the standard ChoiceScript. It’s been changed to allow arguments for gosub, like with the bump subroutines.

You can now pass parameters to a subroutine, just use the *params statement in the subroutine itself to access them. The subroutine can’t return a value though so you still need to use variables to track any changes the subroutine makes. I use subroutines a lot.

1 Like