Im reletively new to twine (Ive edited and modified twine games, but im new to making them myself) and i cant figure out why this code isnt working.
<<textbox "_input" $mc-age,"25">>
<<if $mc-age lt 30 [set $mc-agegroup to young-adult]>><</if>>
<<if $mc-age gt 31 [set $mc-agegroup to adult]>><</if>>
It keeps coming back with
Error: <<if>>: bad conditional expression in <<if>> clause: unexpected identifier 'state'
and the age textbox keeps coming back as “undefined”
I cant figure this out. I took a coding course in high school and did pretty well, but debugging was always my least favorite part as i could rarely see the problem without help. Ive been fairly ok so fsr, but this one is just not computing in my brain…
In a passage titled StoryInit we should create a default value
<<set $mc_age to 25>>
Next, in our story start passage, we create a textbox and a link to the next passage
The order that we need to use is laid out here. Below, our arguments include the variable to set ("$mc_age") and then the default age shown in the text box (25 ).
<<textbox "$mc_age" 25>>
[[Next]]
In a passage titled Next we print the results of what the textbox processed
Note that there are no square brackets; I’m not sure if there’s a way to format it similar to what you tried, but this is how it’s usually done. See this page for if and set.
Also note the use of lt and gte below … your way would exclude 30 and 31 from the possible age range.
And finally we can just print the outcome so it’s visible … you can change this later depending on whether you need the player to see the values.
<<if $mc_age lt 30>><<set $mc_agegroup to "young-adult">><</if>>
<<if $mc_age gte 30>><<set $mc_agegroup to "adult">><</if>>
<<print $mc_age>>
<<print $mc_agegroup>>
I also had trouble using dashes, so I changed those to underscores (ie. $mc-age to $mc_age)
A standard dash/minus sign - is not a valid character in a variable name, because it can be confused for a mathematical minus sign.
A comma is not a valid separator between macro arguments, SugarCube uses a SPACE character for that.
The [<<textbox>>](SugarCube v2 Documentation obtains String value from the end-user, and you want a Numeric value, so I suggest using the <<numberbox>> macro instead.
The entire contents of the Passage being visited is processed before the page is updated to show the input field the end-user will use to supply the requested input. This means that your <<if>> macro calls will be evaluated before the end-user has a change to supply an age.
That argument of the <<textbox>> and <<numberbox>> macros is the name of the variable that the end-user’s input will be stored in. In your example you supplied a (temporary) variable name of _input, so that’s the variable name you should be checking in your <<if>> macro calls.
Using a temporary variable to collect the end-user’s input is a good idea, as they often don’t provide what is asked of them. So the value of that temporary variable can be validated before being assigned to a $mc_age story variable.
Often when asking an end-user to enter input of any kind, a link or button is used for their to indicate that they have finished suppling the input. This gives the Author a place to execute their validating code.
<<textbox "_input" $mc_age>>
<<link "Done">>
/* Is the number within an acceptable range? They may of supplied a negative */
<<if _input <= 1 or _input >= 100>>
/* what to do? */
<<else>>
<<set $mc_age to _input>>
<<if $mc_age lt 30>>
<<set $mc_agegroup to "young-adult">>
<<else>>
<<set $mc_agegroup to "adult">>
<</if>>
<<goto "Name of Next Passage">>
<</if>>
<</link>>
This fixed the problem (with the use of <<numberbox>> instead of <<textbox>> from the other guy as well) so many thanks for that. If i were wanting to add a middle range (say i wanted under 30 to be young adult, 31-70 to be adult and 71+ to be elderly, how would i do the middle bit. I used the default twine format for a bit and it had an “is in” component, but i cant suss out how to do that with sugarcube.)
under 30 to be young adult, 31-70 to be adult and 71+ to be elderly
There is an error in that set of ranges, it doesn’t do anything if the age is 30, so I will assume you meant 30 to 70 (inclusive).
The <<elseif>> macro can be used to check for 2nd (or more) condition within a range.
<<if $mc_age lt 30>>
<<set $mc_agegroup to "young-adult">>
<<else $mc_age lte 70>>
<<set $mc_agegroup to "adult">>
<<else>>
<<set $mc_agegroup to "elderly">>
<</if>>
The above works without needing to explicitly check for the lower (30) bounds of the 30 to 70 (inclusive) range, because the 2nd (later) condition in that set of related “if” checks will only be evaluated if the outcome of the 1st (early) condition was false. And if the 1st condition was false then we know the age value is at least 30.