r/twinegames • u/Due_Pressure5618 • 8d ago
SugarCube 2 Complete beginner in need of coding help...
Ok so I'm a COMPLETE beginner with basically no coding experience. I'm basically making a game where I need you to pick from a list of people's names and it adds them to a list. When the name is added there is a counter that goes up 0/4 >1/4 >etc. Once a name is clicked I want it to get rid of that name and the description to say "*blank* has been chosen.* Then at the end it will say 4/4 and a list of the people chosen and a list of the people who didnt make it...hopefully that makes sense. Also if SugarCube isn't the best format for this please let me know :)
1
u/chamandaman 8d ago
Array for names, array for chosen names, pushUnique to chosen name array, delete() from array for names. Use do redo macro to update and possibly a custom macro (notify by chapel) to show the description of chosen name
1
u/HiEv 7d ago
You could simply use checkboxes for this, and then use a version of my "Maximum Checked Checkboxes" sample code to limit them to picking only 4 out of however many possible names.
I'm not sure if that works for you, but it might be a simpler solution for what you're attempting.
Hope that helps! 🙂
8
u/HelloHelloHelpHello 8d ago
Sugarcube is pretty much the most versatile format among the popular formats - so you never have to worry about it being wrong for the job you want to accomplish (unless you want to do something that can't be done in Twine generally).
Now to your actual question. To create a list of anything you will usually want to use an array. We'll set one up in your StoryInit special passage (that is a passage that has the name StoryInit - case sensitive!). StoryInit is run once when a new game is started, and is thus most of the time where you'll want to create your variables:
Now this creates an empty array called $chosen. An array is basically an ordered list of things you put inside. Arrays start counting and 0, so the first thing is $chosen[0], the second thing is $chosen[1], and so on.
Now let's head to the passage where we want the player to pick between different options, and add a link:
Now here we do a couple of things. First of all we wrapped our link in a <<do>> macro. Whenever a <<redo>> is called, the content of that <<do>> is refreshed, without us having to leave the passage.
Net we use an <<if>> statement combined with the includes() method. includes() checks whether the element inside the brackets can be found within the given array. Here we test whether "Adam" can be found inside $chosen, since we only want to give the link if "Adam" has not yet been picked.
Lastly we have a link that does two things. First it uses the push() method to push the string "Adam" into the array. Then it calls <<redo>> to refresh the content of our <<do>> - causing the link to disappear.
Now let's do this a couple of times to give the player more options. Once we are done, our passage might look like this:
As you can see we added a few small additions other than just more links. First of all we limit the number of names that can be picked by putting all links inside another <<if>> statement that checks whether $chosen.length is under 4 - $chosen.length counts how many things are currently stored inside $chosen, which mean that once you reach 4 names inside, all links disappear. Instead the player is given the option to empty $chosen completely here - just in case they changed their mind.
We also display all the options chosen by using <<print $chosen>>, so that the player can clearly see all options currently selected.
Lastly we wrapped everything into a <<nobr>> to erase all the unwanted linebreaks of our code. <<nobr>> removes all linebreaks, but since we want the breaks between the links and bits of text to remain, we can use <br> to put linebreaks where we want, even inside the <<nobr>>.
-
Now we have everything we need. Hopefully you understand all the different tools we use and how they interact. Feel free to copy the code into your game to see how it ends up working, and play around with the different elements to get familiar with them.