🧰 [TOOL] CSLIB - ChoiceScript Library

You’re very welcome. I’ve stuck a draft of a “sort” method for arrays up on the CSLIB repository: Add sort method to cslib_array by CareyJWilliams · Pull Request #51 · ChoicescriptIDE/cslib · GitHub. Any feedback would be welcome.

You can try it out by:

  • copying and pasting the new code added to cslib_array into your own project
  • Making sure you have a global var called cslib_ret (as CSLIB always requires)

And then:

*create my_array 5 5 4 3 2 1
*gosub_scene cslib_array sort "my_array"
${my_array[1]} = 1
${my_array[2]} = 2
...
2 Likes

@CJW I haven’t gotten to this yet, but I will!

In the meantime, to anyone: are we able to combine *hide_reuse & *selectable_if? I want the option to only be selectable if a condition is met but, once selected, it can’t be selected again.

You can’t do that with a double inline command. But instead of *hide_reuse, create some sort of tickbox variable to confirm whether the option has been selected, like below:

*if tick = 0
  *selectable_if(condition) #Option
    *set tick 1

That should have the same effect, using only slightly more code.

2 Likes

Yes, I use true/false rather than integer (unless I need to keep track of multiple possible states for something), but this is what I do all the time. It’s the only way to combine *hide_reuse and *selectable_if functionality.

2 Likes

@Havenstone @Chris_Conley

Thanks! I ended up with the following:

*selectable_if ((hallclock >= 0) and exhibit1) #The Whales.

Hallclock starts at 3. With each choice made it ticks down one. When it hits 0, this option (and those like it) become unselectable and, in their place, the “exit the hall” options become selectable. Exhibit1 is a boolean - set to true for all exhibits - but once selected is set to false. That way you can choose exhibits in any order you want, can’t select the same exhibit twice and once you have picked three your only options are to leave the hall.

This appears to be working. I wish there were a more elegant solution.

1 Like

@CJW Okay, here come my questions!

So you gave me a link to two files. I recreated each of those files, with the same code. The first one starts with *return - shall I include that as well?

Then I need to add in my story " ```
*create my_array 5 5 4 3 2 1"
So those numbers are the values of the things I want to sort. So should that be?
*create my_array {Anthropology} {Astronomy} {Biology} {Entomology} {Geology} {Paleontology} {History}
?
And then I add:
${my_array[1]} = 1
${my_array[2]} = 2
…
${my_array[7]} = 7
?

And if that all works, I can then add code like this?
*temp lowest_value {p_array_ref&“1”}
*temp highest_value {p_array_ref&p_array_ref&“count”}
The lowest value is ${lowest_value}.
The highest value is ${highest_value}.

which will allow me to do checks against lowest_value and highest_value to determine if something is at the top or at the bottom.

Hiya,

You can ignore the test file, that’s just there to ensure future updates to cslib don’t break the functionality.

All you need is the new contents of the other file from line 363 to 392 (inclusive).

Stick it anywhere you like in your project and call it like so:

*gosub_scene the_scene_you_put_it_in sort "my_array"

Where my_array is an array created like you’ve suggested above. You’ll also need to *create a variable called “cslib_ret”.

You don’t likely want to mess with the implementation of this (so don’t go using p_array_ref). It’s a library function, so the intent is to treat it like an opaque box, feed data in and get data out.

All that said, if you want to know which stat/variable is the highest. This isn’t what you want. This is great for dumping an ordered scoreboard or something, but it can’t link you to which stat each value belonged to.

There’s already a function called max_stat for that: cslib/scenes/cslib_number.txt at 45af9d6c221fbe85f4a857ade2d6974c127b62e4 · ChoicescriptIDE/cslib · GitHub

*gosub_scene cslib_math max_stat "biology" "astrology" "..."
${cslib_ret} is the largest stat

There’s a min_stat as well.

1 Like

Oh! Yeah, that’s all I care about - the max highest and the min lowest. So should I just tackled instead the one we explored above?