Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with setting Twine variables from this javascript dropdown?

I'm working on a small twine project, and I really want to do a drop-down box which:
[list type=decimal]
Sends the player to another Twine passage as soon as they've selected an option (not using a submit button)
Sends the player's choice back into a Twine variable.


I've seemingly worked out the first one.  (I really don't know anything about Javascript, I just looked at other things and bashed stuff together until something seemed to work).  The second one, though, I've had no luck with. 

I've gotten this far, which is admittedly not far at all:
<html>

<form name="myform">

<select name="mydropdown"
onchange="javascript:state.display('NextPassage', this);">
<option value="">--choose--</option>
<option value="1">Page 1</option>
<option value="2">Page 2</option>

</select>
I also understand, vaguely, that 
javascript:state.history[0].variables['TwineVariableName']
should be involved in here somewhere, but I can't figure out a way to change it to the option value (or anything else) my this method. 

I'd be very grateful if someone would point me in the right direction.  This is literally all the javascript that's going in this project, but it's stumped me.  Thank you.

Comments

  • I'm not certain quite how to make it as elegant as possible in terms of Twine design, but this should do what you require:

    [quote]<select name="mydropdown"
    id="mydropdown"
    onchange="state.history[0].variables['my_variable']= document.getElementbyID('mydropdown').options[document.getElementById('mydropdown').selectedIndex].text; state.display('NextPassage', this);">

    Bold text is new. Substitute different nomenclature for the green and orange text as you desire.

    Also- assuming you've got a recentish version of Twine -you don't actually need the <html> tag, or even the <form> tag.
  • Sugarcube has macros for drop down lists (http://www.motoslave.net/sugarcube/docs/#macros-optionlist) although i can't seem to make onchange work...not sure whats wrong here.

    This is just a muck around test, but when I select a food item nothing happens.

    <<set $FoodList = [ "Chicken", "Pineapple", "Quiche", "Ham"]>>
    <<optionlist "food" $FoodList>>
    Choose a food item.
    <<onchange>>
    <<if options.food eq "Chicken">>
    Chicken chosen.
    <<elseif options.food eq "Pineapple">>
    Pineapple chosen.
    <<elseif options.food eq "Quiche">>
    Quiche chosen.
    <<else>>
    Ham chosen.
    <<endif>>
    <</option>>
    <</optionlist>>
  • Dazakiwi38 wrote:

    Sugarcube has macros for drop down lists (http://www.motoslave.net/sugarcube/docs/#macros-optionlist) although i can't seem to make onchange work...not sure whats wrong here.


    1. The &lt;&lt;option&gt;&gt; macros are meant for game options only, not for general use within a game.

    2. &lt;&lt;onchange&gt;&gt; clearly specifies that it silently executes its contents when the option is changed via the control.  It will never produce output normally.
Sign In or Register to comment.