Returning a value from a subroutine

I have the following scenario:

Scene A can go to Scene C
Scene B can go to Scene C

Scene C needs to return control to A or B, but things happen there that scenes A and B need to know about. I don’t want to use any global variables to track this state (that’s the worst-case scenario I will fall back to if there isn’t any other solution for this).

I know you can’t use something like

*set x 3
*return x

But I think that maybe there’s a way to use pointer black magic to send a param to the subroutine, then change its reference inside the subroutine to something else so that it retains the value that it points to on the other side.

Is this possible? And if it is, will the code be acceptable when publishing to HG?

I think that the way you’re trying to do it is precisely how it doesn’t work. Like temp variables and such are wiped out when you move from scenes. At least I don’t know another way around than global variables to do that, and less than all that work in a way that allows you to get published. Either way, is that too much of a hassle to code? Aren’t global variables supposed to do just what you’re intending to do?

I mean, I don’t know programming but I bet there are ways to do what you want, but you’ll be playing with internal code, so forget to get it published.

4 Likes

That’s kind of what I’m afraid. My hope was that if another variable has a reference to that memory address, then it won’t be deleted. I saw some other posts where people were playing with pointers, but I can’t find anything about it in the official documentation about it. Granted, they were using pointer shenanigans in the same scene.

Yes and no. In my particular case, the global variable would be used just for passing data between scenes, so that means it will clutter the startup file.

1 Like

Unfortunately as @Loudbeat suggests, this isn’t possible in ChoiceScript without global variables (or some hacky usage of script). Scene to scene data transfer via gosub_scene is one directional.

You might want to look at 🧰 [TOOL] CSLIB - ChoiceScript Library for inspiration.

That uses two patterns:

  1. Defines a reused “cslib_ret” to hold all ‘returned’ values
  2. Some methods take the name of a global variable and refer to it, as you mention, like a pointer or reference

Unfortunately, again, neither approach works with temp vars.

4 Likes

I ended up using a different approach. The reason why I needed the state in the first scenes from the transition ones was to know which label to redirect the player. After reading through the goto scene documentation, I discovered that navigation is possible to a label inside a scene

*goto_scene [scene file] [label]

The solution that I used was sending a param to scene C and then branching on that param to determine to which label to navigate in scenes A or B. Using this approach, no additional global variables need to be declared.

3 Likes

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