Hi,
I'm using Twine 1.4.2 with Sugarcube 2. I want to have the player enter some text into a single-line <<textbox>> macro, but I don't want them to be able to enter something too long - I want to cap the input at a certain number of characters.
Is it possible to specify a max length for the <<textbox>> so that the player can't enter more characters than that? Or am I going to have to let them enter as many as they like and then tell them it was too long?
Comments
Put the following code in a script section: (Twine 1: script-tagged passage; Twine 2: Story JavaScript)
Usage: That will set the <<textbox>> which alters $foo to a maximum length of eight characters.
You say you're using SugarCube v2, what's the full version?
Could you supply an example of the code you were using that did not work for you?
This code doesn't limit the size of the textbox:
(My own code is more complicated, and the textbox is actually inside another passage that's being displayed with the display macro inside the replace, but this is the simplest code I could come up with that demonstrates the problem.)
I'm using Sugarcube 2.12.0 (I think).
The following is a modified version of that function that tries to instantly update the input element created by the <<textbox>> macro, this version will not work for a <<textbox>> macro used directly in the current passage.
To use it modify your example like so: ... notice it is called after the <<replace>> has finished, this should allow the input element created by the <<textbox>> macro to exist before the setTextboxMaxLength tries to modify it.
In particular, when you're using any kind of link macro, you're doing things asynchronously and that is something you should most definitely mention.
Anyway. greyelf's revised solution should work for what you're doing.
Calling out <<display>> here is something of an unintentional red herring. While it's true that invoking <<display>> does not trigger navigational tasks, it's also mostly irrelevant here. The key issue is here is one of when, not how. The "when" in this case is "whenever the player activates the <<link>> containing the <<replace>>, which is long after all navigational tasks for the current turn have been done".
For example, the following would work with my original code even though it uses <<display>>:
And apologies for not mentioning it from the beginning; I'm not used to thinking in terms of events being triggered, so I didn't realize it was pertinent. I'll be sure to mention it if I have another problem like this.