[SugarCube] Text input

1,041 views
Skip to first unread message

lectronice

unread,
Dec 3, 2013, 3:19:32 AM12/3/13
to twee...@googlegroups.com
First of all, thanks for releasing SugarCube. A complete saving system is really something Twine was lacking.

I'm currently trying to use the textinput macro described here : https://groups.google.com/d/topic/tweecode/Rl-OhqTQvb4/discussion - but no luck so far, I get various errors, not matter what I try. I guess there's some incompatibility due to history[0] as described in the SugarCube documentation, but my JavaScript skills are way to limited to understand how to fix this issue.

Could someone help to make this work?

Thomas Michael EDWARDS

unread,
Dec 3, 2013, 6:57:50 AM12/3/13
to twee...@googlegroups.com

--- lectronice on 12/3/2013 2:19 AM, wrote:

> I'm currently trying to use the textinput macro described here :
> https://groups.google.com/d/topic/tweecode/Rl-OhqTQvb4/discussion -
> but no luck so far, I get various errors, not matter what I try. I
> guess there's some incompatibility due to history[0] as described in
> the SugarCube documentation, but my JavaScript skills are way to
> limited to understand how to fix this issue.

Change this:
state.history[0].variables[v] = document.getElementById(d).value;

To this:
state.active.variables[v] = document.getElementById(d).value;

And, when you use it, quote the $variables it's passed. For example:
<<textinput "$foo">>

--
Thomas Michael EDWARDS
Email: tmed...@motoslave.net Website: http://www.motoslave.net/thom/
"That a word like unnatural exists at all indicates a serious deficiency
in common sense...." -- Anna Puma, Dominion: Conflict 1 (No More Noise)

lectronice

unread,
Dec 3, 2013, 7:41:11 AM12/3/13
to twee...@googlegroups.com, tmed...@motoslave.net
Thanks, it works :)

However, would it be possible to make it work with this macro from the 1.3.6. Sugarcane ? It allows an interesting second parameter: the name of the passage the player is sent to after after her answer.

    macros.textinput = {
        handler
: function (a,b,c) {
           
if(c[1]) {
               
var d = c[0].replace(/\$/g,"");
                el
= document.createElement("span");
               
var e ="<input id='"+d+"' type='text' onkeypress=\"var charCode; if(event && event.which){charCode = event.which;}else if(window.event){event = window.event;charCode = event.keyCode;}if(charCode == 13) {state.active.variables."+d+" = this.value; state.display(\'"+c[1]+"\')}\"/>";
               
if (c[2]){
                    e
+="<input type='button' value='"+c[2]+"' onclick=\"state.active.variables."+d+" = getElementById(\'"+d+"\').value; state.display(\'"+c[1]+"\');\"/>";};
                el
.innerHTML = e;
                a
.appendChild(el);
           
}else {throwError(a,"\"[PassageName]\" parameter missing");}
       
}
   
};

I replaced history[0] by active, but I get an error:

Error: cannot execute macro <<textinput>>: c[0] is undefined

I get this by typing <<textinput $name Hi>> (Hi being the name of the passage the player is sent to).

Thomas Michael EDWARDS

unread,
Dec 3, 2013, 10:37:45 AM12/3/13
to twee...@googlegroups.com

--- lectronice on 12/3/2013 6:41 AM, wrote:

> However, would it be possible to make it work with this macro from
> the 1.3.6. Sugarcane ? It allows an interesting second parameter:
> the name of the passage the player is sent to after after her answer.

My word, that's a mess.


> Error: cannot execute macro <<textinput>>: c[0] is undefined
>
> I get this by typing *<<textinput $name Hi>>* (*Hi* being the name
> of the passage the player is sent to).

As I mentioned previously, you must quote the $variable name. Or,
I suppose, you could simply not add the $-sigil. (the reason for
this is at the bottom)

Instead of this:
<<textinput $name Hi>>

Do this:
<<textinput "$name" Hi>>

Or this:
<<textinput "name" Hi>>

I'd also get into the habit of quoting passage titles as well, so
if you ever find yourself with a passage title that must be quoted,
say one containing a space or something like that, you'll already
be in the habit of quoting them. For example:
<<textinput "$name" "Hi">>


The reason for quoting the $variable name is because all macros in
SugarCube get $variable substitution automatically. So, if you don't
quote the $variable name, the macro will receive its value instead
of its name. And, in this case, the macro needs the name of the
$variable, not its value.

lectronice

unread,
Dec 3, 2013, 11:14:32 AM12/3/13
to twee...@googlegroups.com, tmed...@motoslave.net
In fact, I tried with the quotes at first, but I've got another error:

Error: cannot execute macro <<textinput>>: assignment to undeclared variable el

I guess the 1.3.6 macro doesn't work with quotes. Thus this works perfectly with the first macro, and I've found a painfully simple solution to specify the passage:

<<textinput "$name" Hi>> [[OK|Hi]]

Which means I don't need to use the 1.3.6 macro anymore, sorry for bothering you with that. I'll just use a bit of styling to disguise the OK link as a button, and this should do the trick.

Thank again for your help, and keep up the great work!

Thomas Michael EDWARDS

unread,
Dec 3, 2013, 12:46:26 PM12/3/13
to twee...@googlegroups.com

--- lectronice on 12/3/2013 10:14 AM, wrote:

> In fact, I tried with the quotes at first, but I've got another
> error:
>
> Error: cannot execute macro <<textinput>>: assignment to undeclared
> variable el
>
> I guess the 1.3.6 macro doesn't work with quotes.

That's not it, the problem is what it says on the tin. The macro
isn't declaring the "el" variable, which means it's implicitly
assigning to the el property on the global window object, rather
than to a local variable named el. You never want to do that.

The old headers allow, and even participate in, that sort of bad
behavior. SugarCube, however, does not, hence the error.

Changing this:
el = document.createElement("span");

To this:
var el = document.createElement("span");

Should fix that problem.


> Which means I don't need to use the 1.3.6 macro anymore, sorry for
> bothering you with that. I'll just use a bit of styling to disguise
> the *OK*link as a button, and this should do the trick.

Well, SugarCube now comes with a <<textbox>> macro which does what
you wanted. So, if you grab the latest release (build 2740), you
could use that.

lectronice

unread,
Dec 3, 2013, 2:19:41 PM12/3/13
to twee...@googlegroups.com, tmed...@motoslave.net
This is awesome. Thanks!

lectronice

unread,
Dec 4, 2013, 3:16:50 AM12/4/13
to twee...@googlegroups.com, tmed...@motoslave.net
May I suggest a third (optional) argument for a button next to the input field? Something like this:

<<textbox variable_name [passage_name] [button_name]>>

This would ensure players always know what to do, since most web forms use a button to send content. I guess most players will know they must press enter, but some may not understand. This could also be a nice addition to set the tone of the action taken after entering the text.

Thomas Michael EDWARDS

unread,
Dec 4, 2013, 5:34:23 PM12/4/13
to twee...@googlegroups.com

--- lectronice on 12/4/2013 2:16 AM, wrote:

> May I suggest a third (optional) argument for a button next to the
> input field? Something like this:
>
> <<textbox variable_name [passage_name] [button_name]>>

Unless you're planning to scatter buttons liberally through your
story, I'd think that would be more jarring than anything. If you
really feel the need to have something to click on, it might be
better, for consistencies sake, to forgo auto-forwarding altogether
and simply do something like this:

<<textbox variable_name>> [[Next...|passage_name]]
Reply all
Reply to author
Forward
0 new messages