Help using a radiobutton to assign multiple variables and more! Sugarcube 2

+1 vote
asked Aug 14 by AnoeticDuckling (1,930 points)

Haha, I don't know what I'm doing. Thank you in advance.

Okay, let's get into the first question. Can I use a radio button to assign multiple variables? I want to use a radiobutton to decide var1 and add 5 to var2, is that possible?

Second, I want a static textbox, one that the user can't change, is that a thing? I just want to give the illusion of input because it fits in with the rest of the story. I'm chill if it's not a thing, it's more an aesthetics thing.

Third I want to make a textbox (or is there a drop down menu I can use? I didn't see one) that has a limit to what the user can put in, which would be the numbers 01 through 30, no letters.

Fourth, is there an easy (or rather shorter) way to assign variables to whatever number the textbox is. Like if it's a 20 I want to add a bunch of things to several variables. Like 

<<if $var4 is ‘20’>><<set $1 += 15>><<set $2 += 10>><<set $3 += 20>><<set $4 += 15>><<set $5 += 15>><</if>>

But times the thirty it'd be to get there. Plus each input has different amounts added to each one with some exceptions. 

And then finally I think there's the question as to where I would put all this adding of values. Should I put the addition within the textbox that they belong to or should I put them at the top of the next passage?

2 Answers

+1 vote
answered Aug 15 by Chapel (26,410 points)
selected Aug 15 by AnoeticDuckling
 
Best answer

I agree with @Akjosch that this should be broken up into multiple questions.  Some closely related questions, or follow-up questions are fine, but this site is designed to be a place where others can easily search for answers as well as ask questions, and questions that depart too far from their main thrust aren't helpful in that regard.  Anyway, it's just something to consider going forward.

Okay, let's get into the first question. Can I use a radio button to assign multiple variables? I want to use a radiobutton to decide var1 and add 5 to var2, is that possible?

It's possible, but you shouldn't do this. Imagine an indecisive user switching back and forth between both radio buttons a few times and accidentally gaining a large number to some variable.  Instead, a safer approach would be to evaluate the answer in the next passage:

<<silently>>
    <<if visited() is 1>>
    /% you can omit this first <<if>> if the user can't return to this passage %/
        <<if $radioButtonVariable is true>>
            <<set $otherVar to 'something'>>
        <<else>>
            <<set $otherVar to 'another thing'>>
        <</if>>
    <</if>>
<</silently>>\
Passage text.

Second, I want a static textbox, one that the user can't change, is that a thing? I just want to give the illusion of input because it fits in with the rest of the story. I'm chill if it's not a thing, it's more an aesthetics thing.

You can just use pure HTML for this; the 'readonly' attribute will do what you want: 

<input type='text' readonly='readonly' value='blah blah blah' />

Third I want to make a textbox (or is there a drop down menu I can use? I didn't see one) that has a limit to what the user can put in, which would be the numbers 01 through 30, no letters.

Here's a simple drop down macro:

Macro.add('dropdown', {
	handler : function () {
		
        var $wrapper = $(document.createElement('span'));
        var $drop    = $(document.createElement('select'));
		
        var opts      = this.args;
        var storyVar = opts.shift();
        var store;
        var i;
		
        // handle simple errors
        if (typeof storyVar == 'undefined') {
            return this.error('No arguments provided.');
        }
        if (opts.length < 1) {
            return this.error('Must provide at least one list option.');
        }
		
        if (storyVar.charAt(0) === '$') {
            store = State.variables;
        } else if (storyVar.charAt(0) === '_') {
            store = State.temporary;
        } else {
            return this.error('First argument should be a valid TwineScript variable.');
        }
        storyVar = storyVar.slice(1);
        store[storyVar] = opts[0];
			
		
        for (i = 0; i < opts.length; i++) {
            var $option = $(document.createElement('option'));
            $option
                .attr('value', opts[i])
                .wiki(opts[i])
                .appendTo($drop);
        }
		
        $drop
            .attr({
                name : 'dropdown-macro',
                id   : storyVar
            })
            .appendTo($wrapper);
		
        $wrapper
            .addClass('macro-' + this.name)
            .appendTo(this.output);
		
        $(document).on('change', 'select#' + storyVar, function () { 
            store[storyVar] = $('select#' + storyVar).val();
        })
		
		
    }
});

 Syntax:

<<dropdown '$variable' 'option1' 'option2' 'option3'>>

The macro sets the provided variable (must be a story variable ($var) or a temporary variable (_var)) to the indicated option.  In your case, you'd want something like:

<<dropdown '$var' '01' '02' '03' '04' ...etc>>

You could also use a text box, let me know if you want an example of that instead.

Fourth, is there an easy (or rather shorter) way to assign variables to whatever number the textbox is. Like if it's a 20 I want to add a bunch of things to several variables. Like 

Not really, no.  Other that using semicolons or commas (as shown by @Chillbit), there isn't much else you can do. 

Edit. Formatting

commented Aug 15 by AnoeticDuckling (1,930 points)
thank you! this is superb
0 votes
answered Aug 15 by Chillbit (180 points)

I'm not knowledgeable enough to help with most of your problems(sorry), but setting multiple variables with a single <<set>> macro is a simple case of using semicolons, like so;

<<if $var4 is ‘20’>><<set $1 += 15; $2 += 10; $3 += 20; $4 += 15; $5 += 15>><</if>>

 

commented Aug 15 by Akjosch (3,950 points)
I'd suggest making all your questions into actual questions instead of dumping them all into one big bag of everything and nothing in particular. You get a better chance of getting proper answers this way and others get a better chance of finding answers to similar questions they have later.

Also, why can't we comment on the answer directly here?
commented Aug 15 by AnoeticDuckling (1,930 points)
Yeah that's true, I just didn't want to fill the questions lists with questions that felt closely entwined enough to put into one thing.
...