# Arrays and loops and choices, oh my!

**URL:** <https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860>\
**Category:** ChoiceScript Help\
**Created:** [January 6, 2020, 9:28pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860 "2020-01-06T21:28:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![fishbait](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fishbait](https://forum.choiceofgames.com/u/fishbait)\
**Post date:** [January 6, 2020, 9:28pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/1 "2020-01-06T21:28:20Z")

</div>

Hi there!

I’m feeling a bit ambitious these days and have decided to create a kind of “proper” inventory system for my game, one that is based on arrays and allows actions such as equipping, unequipping and using items from within the stats screen. I’m going for a more old-school granulated experience, basically to see how close I can get a CYOA type game to feel like a classical puzzle based text adventure.

Anyways, I’m stumped with this bit of code:

> Which item do you want to equip?  
> \*temp a 1  
> \*choice  
> …\*label loopstart1  
> …\*if (it\_loc[a] = 1) #Equip ${it\_name[a]}  
> …\*set it\_loc[a] 2  
> …You equip ${it\_name[a]}  
> …\*set a a+1  
> …\*if (a \<= 8)  
> …\*goto loopstart1  
> …#Return to stat screen  
> \*goto scr\_main

So, array it\_loc contains locations of items, with value 1 being carried and value 2 is equipped. it\_name contains names of the items. This loop is supposed to go through all the items in the array and display a choice “Equip X” for each item that has value 1 as its it\_loc.

But it’s not working. I get a message "Invalid indent? Expected an #option here not \*label at the \*label loopstart1.

So, if any gurus out there can help me out with this? I know that arrays are pretty much ignored and perhaps even half-implemented by the devs, so is this even possible in CS? I’d really hate it if i’d have to go through each and every item in the game via if… choices…

Any help is greatly appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![iota](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/iota/32/144568_2.png) [@iota](https://forum.choiceofgames.com/u/iota)\
**Post date:** [January 6, 2020, 9:31pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/2 "2020-01-06T21:31:42Z")

</div>

Try putting your \*label before your \*temp variable?

---

<div class="post-metadata">

**Author:** ![GoldenSilver](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/goldensilver/32/110153_2.png) [@GoldenSilver](https://forum.choiceofgames.com/u/GoldenSilver)\
**Post date:** [January 6, 2020, 9:38pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/3 "2020-01-06T21:38:46Z")

</div>

Afaik I don’t think there is a way to create dynamic choices like these, I wish it was possible. You’ll have to manually create these choices for each of the items (I had to do the same).

This is pretty much why I limited my stuff to a fixed number (6) since I would have to deal with each choice manually.

 ![image](https://global.discourse-cdn.com/choiceofgames/original/3X/8/f/8f3eab3ebe25d323fc72762d1ddf9d1c63818dec.png)

Also I recommend you use some sort of identifier for the index instead of just “a” (even something like “counter” is easier to understand), something that is more clear what that number is supposed to be.

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [January 6, 2020, 11:12pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/4 "2020-01-06T23:12:13Z")

</div>

I was speaking to GoldenSilver about this a few days ago. I’ve actually got a half-written article/tutorial written on how to create ‘dynamic’ choices in ChoiceScript. GoldenSilver is right in the sense that you can’t just “loop over” option lines. I honestly think this is a _good_ thing, at least as far as syntax and code presentation go. Now you _can_ dynamically “construct” choices, but alas, you’ll always have that finite cap on the number of options you can have (based on the amount of static physical # options you’re willing to write out). And let’s be honest… Anymore than 6 or so options on a screen can get messy anyway.

> Afaik I don’t think there is a way to create dynamic choices like these, I wish it was possible.

All that said, I think there is definitely a way to create truly dynamic choices, with a reasonably infinite\* limit, and variable amount, of options. It’s not a novel concept, quite an old one actually, but I think it’s an application that hasn’t seen use in ChoiceScript yet: **pagination**.

> **Example GIF**
>
> [Dropbox - cs\_pagination.gif - Simplify your life](https://www.dropbox.com/s/4vryzo9jtm9d426/cs_pagination.gif?dl=0)

Admittedly it comes with its own caveats (like the tedium of clicking next/previous\*), but I do believe it solves the “issue” at hand. I’ve spent a lot of time working on systems implementing various implementations of this, and have been very impressed with what I’ve been able to create (including email inbox, and an equip/unequip inventory with criteria filtering). It’s probably not going to be a catch-all for everyone, but it might just be enough to at least inspire more out of the box thinking when it comes to constructing more dynamic choices.

\*Which you might see I’ve spent some time improving with a ‘predictive’ system that swaps out the positions of the next/prev options, so the most likely to be selected is the default.

---

<div class="post-metadata">

**Author:** ![fishbait](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fishbait](https://forum.choiceofgames.com/u/fishbait)\
**Post date:** [January 7, 2020, 2:37pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/5 "2020-01-07T14:37:28Z")

</div>

Thank you for your help!

I’ve listened to GoldenSilver’s advice as it seems that there is no way to get a loop into \*choice construction ☹ However then I’ve run into another problem, and that is that variables do not update automatically once you leave stats screen… It seems that you have to move to the next passage in order for variables to update - not good.

So, my idea was to be able to have a following scenario: You encounter a door, you go to your stats screen and do use item → key, when you leave your stats screen the passage is updated with a message that you’ve opened the door using the key (and choices update to show that you can now go through it). However, with the way CS seems to be set up this wouldn’t be possible… Or does anyone have an idea how to get around this limitation?

Oh and as for CJW’s remark, I don’t think the number of options would be a problem… The loop checks whether the item is in your inventory (which can be made limited for extra old-school vibe) and then presents a choice based on that. Also, most items cannot be equipped so with the item set I have in mind i don’t suppose there would ever be more than four or five items on the list…

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [January 8, 2020, 10:31am UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/6 "2020-01-08T10:31:19Z")

</div>

> [@fishbait](#):
>
> However, with the way CS seems to be set up this wouldn’t be possible… Or does anyone have an idea how to get around this limitation?

See this post on \*redirect\_scene: [Grr...More restrictions found on the stats screen - #24 by dfabulich](https://forum.choiceofgames.com/t/grr-more-restrictions-found-on-the-stats-screen/2568/24)  
Paying specific attention to the following comment:

> If you want to make a change on the stat screen that has an immediate effect on the main game, use `*redirect_scene` instead of just `*set` .

> [@fishbait](#):
>
> Oh and as for CJW’s remark, I don’t think the number of options would be a problem… The loop checks whether the item is in your inventory (which can be made limited for extra old-school vibe) and then presents a choice based on that. Also, most items cannot be equipped so with the item set I have in mind i don’t suppose there would ever be more than four or five items on the list…

Then doing as GoldenSilver suggested will work just fine.

---

<div class="post-metadata">

**Author:** ![fishbait](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fishbait](https://forum.choiceofgames.com/u/fishbait)\
**Post date:** [January 8, 2020, 12:10pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/7 "2020-01-08T12:10:02Z")

</div>

Thanks!  
I’ve already found that thread but I can’t understand how to use \*redirect\_scene in this context. Basically it is just like \*goto\_scene, right? So… as I understand it I change the variable and then use \*redirect\_scene to return to the original passage or what? I don’t see how this would help in any way because it would return me to the beginning of the scene rather than the passage I accessed stat\_screen from…

Please, could you give me a code example of how to use \*redirect\_scene for this purpose?

---

<div class="post-metadata">

**Author:** ![CJW](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/cjw/32/111787_2.png) [@CJW](https://forum.choiceofgames.com/u/CJW)\
**Post date:** [January 8, 2020, 1:40pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/8 "2020-01-08T13:40:59Z")

</div>

You’re right, it’s very difficult to do (as you never know when the stats screen is going to be called from). I’d sooner suggest you just add a hidden option to a choice in the main game code, rather than trying to allow usage of the ‘key’ from the stats screen.

---

<div class="post-metadata">

**Author:** ![fishbait](https://avatars.discourse-cdn.com/v4/letter/f/b19c9b/32.png) [@fishbait](https://forum.choiceofgames.com/u/fishbait)\
**Post date:** [January 8, 2020, 8:43pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/9 "2020-01-08T20:43:55Z")

</div>

Oh well… I guess I’ll just have to give up on my grand project of a puzzle based CS game… I doesn’t really make sense to have a puzzle if the solution is clearly offered as a choice. ☹

Imho, CS is a bit too tight and stingy with features. I understand the minimalism and simplicity argument but I believe it can be going too far if you are sacrificing utility for it, especially if we are talking about a creative tool rather than a consumer product. I don’t know why the devs are so afraid of giving more power to creators who would like to be a bit different… Even simple arrays are all but hidden in documentation for fear of… I don’t know what. “Adding complexity without adding value.” Lol. I don’t want to sound salty, but it does seem kinda patronizing and passive aggressive to me. “I personally can’t think of why would anyone need arrays (or variable changes in stat screen) so I am not going to give it to them… because I’m the smartest person around and no one can think of anything that I can’t imagine”.

---

<div class="post-metadata">

**Author:** ![Lan](https://sea3.discourse-cdn.com/choiceofgames/user_avatar/forum.choiceofgames.com/lan/32/80851_2.png) [@Lan](https://forum.choiceofgames.com/u/Lan)\
**Post date:** [January 8, 2020, 9:55pm UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/10 "2020-01-08T21:55:01Z")

</div>

So I’m not sure if this is what you’re going for, but have you considered using `*input_text` as a pseudo parser to interact with objects?

Teach the player some simple commands so they know what to try and you have a flexible system for letting them puzzle out what to do.

In your example, you could have a choice at the door that leads to a text field where the player would need to enter “use bronze key” (for example) with the key in their inventory in order to unlock the door.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/choiceofgames/original/3X/e/f/efe1497a762edecdb6d569a1c4abee87508bf002.png) [@system](https://forum.choiceofgames.com/u/system)\
**Post date:** [January 14, 2020, 9:57am UTC](https://forum.choiceofgames.com/t/arrays-and-loops-and-choices-oh-my/71860/11 "2020-01-14T09:57:15Z")

</div>

Closed on January 14, 2020, 9:57am UTC

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.
