Variable selection with logical operators

146 views
Skip to first unread message

RedorYellow

unread,
May 5, 2013, 11:25:04 PM5/5/13
to twee...@googlegroups.com
Hi,

I'm having so much fun playing with Twine, even as a newbie! Part of that newness, though, is that I don't totally know the limits of what can and can't be done.

I'm hoping to identify the two largest variables from a list. Let me make up a basic example of what I mean, so that it hopefully makes sense. I might not be specifying variables in the best way to accomplish this, either.

Imagine a game where you can interact with three friends: Joe, Bill, and Steve. Based on character choices you make at the start, you will be raising or lowering your $joescore, $billscore, and $stevescore. After this initial period, I want to be able to select which two of the three scores are the highest and print them on the page: "Your best friends are (result 1) and (result 2)." In other words, imagine that Joe' score gets raised with adventurous choices, Bill's with responsible choices, and Steve's with friendly choices. So, based on what kind of character you choose for yourself in the opening passages, you would be told that your two best friends are the boys who best reflect your chosen behaviors.

Is this possible? How would I go about doing so? I'm currently hung up on 1) using logical operators to choose between multiple variables, not just two, and 2) on how I would record these choices for output to the player. Thank you so much for any help!

Leon Arnott

unread,
May 6, 2013, 4:51:53 AM5/6/13
to twee...@googlegroups.com
Since you're fairly fresh to code, here's a fairly simple implementation of code to select the name with the highest score:


<
<if ($billscore gte $stevescore) and ($billscore gte $joescore)>>
<
<set $highest = "Bill">>
<
<set $secondhighest = ($stevescore gte $joescore ? "Steve" : "Joe")>>
<
<else>>
<
<if ($joescore gte $billscore) and ($joescore gte $stevescore)>>
<<set $highest = "Joe">>
<
<set $secondhighest = ($stevescore gte $billscore ? "Steve" : "Bill")>>
<<else>>
<<set $highest = "Steve">>
<
<set $secondhighest = ($joescore gte $billscore ? "Joe" : "Bill")>>
<<endif>>
<<endif>>

This code will set $highest to the name of the person with the highest score, and $secondhighest to the greater of the runners-up. The question-mark and colon syntax is the "ternary" - a Javascript operator, but one I find simple enough for most people to understand. I describe it here - it's basically a condensed <<if>> statement.

Devi

unread,
May 6, 2013, 8:08:37 AM5/6/13
to twee...@googlegroups.com
I don't know if you've seen this, but it's a good list of the logical operators you can use in your game: http://gimcrackd.com/etc/doc/#code,if

To display the names of the friends using the code by Leon, you just need to put in the passage something like: 

"Your best friend is <<print $highest>> and next is $secondhighest." That will display something like "Your best friend is Bill and next is Steve." 

RedorYellow

unread,
May 8, 2013, 12:15:44 AM5/8/13
to twee...@googlegroups.com
Wonderful, thank you both so much! This was very helpful, and I'm glad to know that it's possible.

Peter Hughes

unread,
May 8, 2013, 4:43:47 AM5/8/13
to twee...@googlegroups.com
Another solution that requires less coding (although might be a  bit more complicated if you're new to datastructures ...)

Setup your friends:

<<set $joe =  {name:"Joe", score:0}>>
<
<set $bill = {name:"Bill", score:0}>>
<
<set $steve =  {name:"Steve", score:0}>>

You can increment/decrement scores like so:

<<set $bill.score = 4>>
<
<set $joe.score = $joe.score + 1>>


Then you can order them at any point with this code:

<<set $order = [$bill, $joe, $steve].sort(function(a,b){ return a.score - b.score; })>>

This makes a list (technically, an array) of friends, ordered from lowest to highest. Once done, you can print out the names like so:

Your best friend is <<print $order[2].name>> and your second-best is <<print $order[1].name>>

If you have never used arrays, the trick to remember is that if you want to get the 1st entry, use $array[0], 2nd entry is $array[1] etc...   in the example above, because the array is ordered from lowest score to highest, you'll want to take the last (i.e 3rd) entry as the 'best friend' value, so you do $order[2] (as there are 3 values)

Arrays can be a bit weird, but once you understand them they're also a bit easier to deal with than lots and lots of <<if>> statements. The choice is yours :)

Devi

unread,
May 8, 2013, 8:05:39 AM5/8/13
to twee...@googlegroups.com
A minor correction: The code on my previous post should be

"Your best friend is <<print $highest>> and next is <<print $secondhighest>>." 

Good luck with your game!

RedorYellow

unread,
May 8, 2013, 10:58:49 AM5/8/13
to twee...@googlegroups.com
Thank you again, this would probably work even better! I'm familiar with arrays from doing statistical work in R, so I think I should be able to translate this into Twine with only some minor hair-tearing as I become used to it. :)

Peter Hughes

unread,
May 8, 2013, 11:56:00 AM5/8/13
to twee...@googlegroups.com
Excellent!

Whilst the Twine syntax may be a little different to what you're used to, the easiest way to deal with it (if you're comfortable with languages like R), is to treat anything within a <<set>> or <<if>> tag as a JavaScript expression; hence, you can write whatever valid JS you like. A lot (ish) of the concepts of R can be replicated within Javascript, although it may require a bit of fudging. Prepending variables with a $ symbol exposes them within Twine for later use, and as shown above you can reference them within JS too. This can be as simple or as complex as you like: you could do something like the following if you really wanted:

<<set
var arr = [$bill, $joe, $steve];
var modifier = function(p) {
   
p.score = p.score * $some_other_value;
}
var sorter = function(a, b) {
   
return a.score - b.score
}

$
sorted = arr.forEach(modifier).sort(sorter)
>>

Your highest scorer was: <
<print $sorted.pop().name>>

Bashu Naimi-Roy

unread,
Aug 28, 2015, 5:28:18 PM8/28/15
to Tweecode / Twine
This was very useful and helpful. Thanks.
Reply all
Reply to author
Forward
0 new messages