So, I'm writing a game where you are making dinner. You have 9 ingredients, and may choose two. There are then many branching paths (though obviously not a full 81 or whatever) from there. I'm having trouble figuring out an elegant way to do the very beginning - coding that initial set of choices. I know if worst comes to worst, I could brute force it and make like 81passages (First choice meat, and then first choice meat, second choice lettuce, first choice meat, second choice bread, etc), but I know there must be a more elegant and simpler way to do this. I've read about variables and expressions, and I have some very basic experience in coding with BASIC and the long-dead Adobe Director program (anyone else remember that?), but I can't seem to figure out exactly what to do. My first thought is to use checkboxes, but I'd have to find a way to limit the choice. And then, I haven't been able to find a more comprehensive guide for writing that kind of code to go with it. I've read the wiki, but I haven't been able to find exactly what I'm looking for.
Specifically, can I write something so after the two checkboxes are checked, then Meal=M,O if Meat and Oil are chosen for example, and then the game code goes "If Meal=MO or OM, goto Steak, if Meal=MP or PM then goto Spaghetti", that kind of thing? And if limiting checkboxes aren't an option, what recourses do I have? Would having two pages with radio buttons work better/easier? Any help is much appreciated.
Wait, like, could I have one page for the first ingredient, then the next page is missing/hiding that ingredient, and then the next page adds the two choices together to goto the appropriate passage? Thanks!
Comments
With each ingredient when you add it set a variable that gets checked later on.
Using "if" statements you can do everything you want to do.
It consists of four passages: "Select From Ingredients", "Show Possible Ingredient Choices", "Made Choice", and "Make Meal" as well as some Javascript that you will need to add to your Story Javascript area.
1. The Javascript: this potentially adds two methods to the standard array object to make it easier to find and delete an element from an array.
The first method allows us to find an element within an array, some older web-browser may not have this method built-in.
The second method allows us to delete an element from an array and uses the first method to find it.
2. The four passages:
note: "Select From Ingredients" is the first passage in my story.
a. Select From Ingredients
b. Show Possible Ingredient Choices
This passage displays each of the possible Ingredient that the reader can choose from, it will only show a link for items that can still be selected.
c. Made Choice
This passage first removes the selected ingredient from the array of possibles, then adds the selected ingredient to the array of selected. It also checks to see if enough ingredients have been selected (2), if they haven't then the reader gets to choose another ingredient otherwise they are moved to the next passage.
d. Make Meal
This passage is where you check which ingredients the reader has selected and show which meal they made.
One of the issues you had in your example "If Meal=MO or OM" was because you don't know which order the reader will choose the two ingredients you had to check for two values ("ingredient1ingredient2" or "ingredient2ingredient1"), you can get around this by sorting the selected items before joining the two together to form your "value".
I hope all the above makes some sense.
Probably also an idea to set up a nice naming index for the ingredients:
Then some variables to track your ingredients:
Then in the page where they pick their ingredients:
Only thing to watch is that you have to define the ingredients in the index for the cookbook with the lowest choice value item first. If you want Oil_Meat to be different from Meat_Oil you'd need to take out the line that swaps the choice values if your second choice is lower than your first one and ensure you had all combinations defined. As it is, you need only define the combination where the lowest value item is picked first.
And you broken the JSON variables in Harlowe why?
I ask because a number of your variables are missing dollar signs:
Once I added the missing dollar signs the list appears like:
And if I select a second item I get the following error, though that could be due to my adding missing dollad signs to your variables:
Does your suggestion handle the situation of a Reader using the web-browser's Back button?
The back button should work - it's just reversing a passage transition.
Wiki links like this are the problem:
If you were trying to use the horrible shorthand syntax for the <<print>> macro (e.g <<$foo>>), then it is not and will never be supported by SugarCube. You don't need it there anyway, because SugarCube's handling of the optional text portion of the wiki link markup (…) is different from the Twine 1 vanilla story formats. So, instead, it should be something like the following:
Otherwise, if you were trying to print literal double angle-brackets (<<, >>), then you'd do so like you would inside a call to <<print>>. For example, it should be something like the following:
Basically, the Twine 1 vanilla story formats treat the link and optional text portions differently (the former as either plain-text or TwineScript code, and the latter as wiki-text). SugarCube (in Twine 1 or 2), on the other hand, treats both the same, as either plain-text or TwineScript code.
Just needed a few $ and a fix to the not found in cookbook check to get it working:
See here for a live version.