0 votes
by (160 points)
closed by

hello!
i am using twine sugarcube to try and make a game where you can enter a "universal designation" in a format such as A1 or Z28. it is not meant to be a very intensive thing, just a fun thing to see descriptions of (at maximum) 784 different universes (most of which i will probably get from my friends or other players)

now, the problem i am having is this: I want to make it so that the user can type in any designation from A1 - Z28 and go to the specified passage. I put in some simple code that I thought would do the trick, but when i try to go the passage "A1" the program says "Error: the passage "A1" does not exist" despite the fact that it does.

i apologize if i have done this in a stupid way or don't understand how the autofocus command is supposed to work, i am not very good at this and have little coding experience.

here is the code i used:  

<<textbox "$UD" "" "$UD" autofocus>>

the variable is $UD (shorthand for "universal designation") and is supposed to be changed to whatever the user inputs, and then use the second "$UD" to designate which passage the autofocus should go to. 
please tell me why this doesnt work! 
thank you in advance

closed with the note: i answered it myself

1 Answer

0 votes
by (160 points)

i'm answering this myself because i found something that works, but i think it would be helpful to keep this question up in case anybody else is trying to do something similar. 
rather than trying to get the autofocus go to the passage with the inputted variable, i made the autofocus go to a passage called "Through the Portal" which is to contain nothing but code similar to this:

<<if $UD is "A1">><<display "A1">><</if>>

<<if $UD is "A2">><<display "A2">><</if>>

what this does is check if the player inputted a specific designation, and then display the text for the corresponding passage. this is kind of a meticulous way to do it, but in the end I need to create 784 passages anyways, so why not add 784 if then statements to the count

by (63.1k points)
edited by

Locking it down a little soon, don't you think?

Here's a better way:

<<textbox '$UD' '' autofocus>>
<span id='report'></span>

<<link 'Go!'>>
    <<if Story.has($UD)>>
        <<goto $UD>>
    <<else>>
        <<replace '#report'>>\
            No such universe exists!\
        <</replace>>
    <</if>>
<</link>>

Some notes: 

If this code doesn't work then you're on sugarcube 1, try switching to sugarcube 2. If you don't want to switch (you should), let me know and I'll whip up some code for 1 for you. 

If your answer involves writing out several hundred lines of code, then it isn't a good answer, and you probably shouldn't shut down your question. That's not a dig at you. I just hope that future readers get this far before writing all that code or giving up. 

Also, here's a definition of focus: 

https://javascript.info/focus-blur

All autofocus does is make the element receive focus as soon as it shows up. It isn't some kind of passage traversal mechanism. 

by (160 points)
thank you for your answer!
im not very good at coding and tend to try and brute force my way into making things work
i tried the code you gave me in sugarcube 1 and 2 and it didnt work either time
it gave me a couple of errors

"Go!
Error: <<if>>: bad conditional expression in <<if>> clause: Story is not defined
Error: macro <</link>> does not exist"

all of the above appeared in red after inserting it into the passage
by (63.1k points)
edited by

You're not using sugarcube 2 then. That looks like an error message from sugarcube 1 or, possibly, sugarcane. I tested it before I posted it and again just now, and it works in sugarcube 2.18.0, and should work in versions probably all the way back to 2.5.0, if not earlier.

Which version of sugarcube 2 did you use? Are you sure you tested it in sugarcube 2? Are you using Twine 1 or Twine 2? Are you sure you didn't mix up sugarcane with sugarcube? (though there isn't a sugarcane 2, so I don't know how that would work out). 

Also note that twine doesn't keep your story formats up to date for you, you have to manually install updates (the same way you'd install a story format). 

Edit: if you change the <<link>> macro to <<click>> (and <</link>> to <</click>>), and change 'Story.has...' to 'tale.has...', this setup should run in sugarcube 1 (but not sugarcane). That said, if you're starting a newish project, there is absolutely no reason to use sugarcube 1 over 2. (Or sugarcane over either, honestly.)

by (160 points)

thank you, that worked!
also, i was using sugarcube 2.1.8.0 as shown in this screenshot but for some reason both sugarcubes are checked and i cannot check only one of them. 
but whatever, as long as the code works
thank you again for your help! 

by (63.1k points)

Oh boy.  That looks like a bug.  Would you mind creating an issue on the issue-tracker and showing the dev team that image to see what they think?  You might also need to tell them some other information: the complete version number of Twine, whether you're using the downloadable or web-based release (and your browser information, if you're using the web version), and your operating system information.

Twine's issue tracker:

https://bitbucket.org/klembot/twinejs/issues

...