0 votes
by (140 points)
Okay so imagine you've got multiple romance options in a game.

Now imagine each person has a max of 9 romance points available to earn. (I didn't set a max, this is just the very most you can earn from each one)

Now, I figured it'd be easier to test them against each other if I grouped them together.

So, let's name the romance options A,B,C,D,E,F

And their grouped together names are AB, CD, EF

How do I code it so that AB is checked against both CD and EF-- and whoever is highest then gets it's two people within compared to each other, like.

$AB < CD & EF


E < F

so that when I code in an if statement like this:

<<if $F>>

~Display the text that should go here if F is highest!~

<<elseif $E>>

~Display text for if E is highest!~


I can't just compare all variables to each other because I have other variables for other things in my StoryInit. So grouping them up and giving them special names for each group makes sense to me.

But also, how would I set that up in the StoryInit?

If you have a better way than this for me to do these things, feel free to recommend it. But please keep the code as simple and minimal as possible if you don't mind. So it's easier for me to understand.

1 Answer

+1 vote
by (44.7k points)

It seems like you added some extra information which kind of made your question unclear.

Are you saying you have six variables, and you're trying to find which variable has the highest value?

Or, are you saying that you have three groups of two variables, and you want the variable with the highest value from the group which has the highest combined value?

Because those are two different questions and could result in different answers.

For example, if the values are (4, 5), (6, 1), (2, 3), then the "AB" group has the highest combined value (A+B = 9), even though the "CD" group has the highest individual value (C = 6).

Also, do you need to know which variable is the largest?  Or just the largest value?  It looks like you need the former, but I'm asking because the latter is fairly easy (if the values are in an array then just "<<set _highestValue = Math.max($arrayOfNumbers)>>" would work; documentation).

Furthermore, does this need to be for an arbitrary number of values?  Or will it always be six values?

A bit of clarification would help make sure you get the right answer.


by (140 points)

Sorry I guess I was a bit all over the place last night. I was so tired and wrung out from trying to find an answer, I got all...well, you probably know how it is.

I have no idea what an array is or how to do that but I will be looking it up now to see if it'll be do-able, thank you.

Anyway, allow me to explain a little better.

I wanted to be able to find the highest value in a group of a LOT of variables. The exact number would be Seven, but I decided to not use one of the variables until it could be grouped with another variable later which I am beginning to rethink because you had a good point about the groupings.

I just wanted to make it easier to calculate which one had the highest number in it, but cumulative scores do make that harder.

Okay so.

Seven variables, all of them pertaining to a romantic option in a romance game. A,B,C,D,E,F,G.

Each one has nine possible points you can gain over the course of the first part of the game. But a lot of those flirt points/romance points are only possible points you can gain if you choose between two options so if you choose all of one character's possible nine points-- the other characters will have less flirt points by default even if you flirted with everyone at every available opportunity otherwise.

So to keep track of who has the most flirts, I wanted to have something that could check the value of A-G but doing an if/elseif thing for Seven variables in a row is not only unwieldy and long, but it doesn't always work for me for some reason to add more than a few variable comparisons.

So I need a way of finding the variable with the highest number and being able to make that trigger things in the story.

Such as finding that C has the highest romance/flirt points count and therefore in the next scene a consequence is triggered where you dance with C instead of any of the other romance options.

If further clarification is required, please do ask, I appreciate the help so much!


by (44.7k points)

Using an array is one way you can store multiple values within a single variable, and each value (also called an "element") can then be found by referencing a number (the array's "index") which tells the code which value to get from the array.  The values in an array always start at index 0 and then go up from there.

So, you could have an array variable named $stats, and have each stat (A through G) be a different index in the $stats array.  For example doing this:

<<set $stats = [4, 5, 6, 1, 2, 3, 7]>>
<<print $stats[0]>>

would cause the code to display "4", because "4" is the value at the "0" index in the $stats array.

I have some information here (in a help file I'm working on) which explains a bit about using arrays (and generic objects) in Twine/SugarCube.

Anyways, if you wanted to find the highest value in the $stats array, then you could use "<<set _val = Math.max($stats)>>", and that would, in this case, set _val to 7, since it's the largest value in the array.  Once you know what the maximum value in the array is, you can then use the .indexOf() method to find the first element in the array which has that value, for example:

<<set $stats = [4, 5, 6, 1, 2, 3, 7]>>
<<set _val = Math.max($stats)>>
<<set _index = $stats.indexOf(_val)>>

So, _val would hold the largest value in the $stats array (_val == 7), and _index would hold the index of the first element in the $stats array which has the value of "7" (_index == 6).  (Note that if two or more values are tied for largest, this method will only point to the first one with the highest value.)

This means that it takes just two lines of code to figure out which element, out of any number of elements, holds the largest value in an array.  And you could even shorten it down to one line of code like this:

<<set _index = $stats.indexOf(Math.max($stats))>>

And if you wanted to display that value, then, after the above line, you could just do:

<<print $stats[_index]>>

If you need more information on arrays, then you can check out the W3Schools array documentation and the MDN array documentation.

Hope that helps!  :-)

by (140 points)
That IS all very helpful but I have a question about that array thing you just explained to me because this is starting to really confuse me.

How am I supposed to put the variables for each character's flirt points in the array? Cause all I'm seeing are numbers up there and the flirt points are being kept count of by the game.

So would I do it like this?

<<set $flirt_set = [$flirt_A, $flirt_B, $flirt_C, $flirt_D, $flirt_E, $flirt_F, $flirt_G]>>
<<set _val = Math.max($flirt_set)>>
<<set _index = $flirt_set.indexOf(_val)>>

But then how do I use that to have it activate certain scenes? All the Array stuff I've been able to find has been words and numbers, not variables and it's super confusing, can I not use variables? How else am I supposed to do my variables?

My variables are only supposed to have the number value that the player chooses so me putting a number in there to represent a value that might not match that number makes no sense!

And yes, I will follow all the links you gave me now. But it's super frustrating to only see a bunch of words or numbers in all these examples and not find a single instance where someone can explain in full detail how you're supposed to do the thing with variables.

(I literally read every piece of information I could find last night on Arrays and not a single one said anything about what I needed and I'm more confused than ever, thanks so much for your help.)
by (44.7k points)
edited by

What's confusing you is that you're thinking of arrays and variables as different things.  An array is a variable, it's just a specific type of variable which can contain multiple values, instead of just one value like most other types of variables.  So, instead of using seven different integer variables to keep track of seven different numbers, you can use one array variable to keep track of all seven numbers.

If it helps, the classic way of describing arrays is thinking of them as being like mail rooms or post offices.  The name of the array variable tells you which mail room you're in, the index (the number inside the brackets which immediately follows the variable name; e.g. $arrayName[index]) tells you which p.o. box in that mail room, and inside that particular p.o. box is a value.

So, for example, instead of doing:

<<set $flirt_A += 1>>

to add one to the first flirt value, you would do:

<<set $flirt_set[0] += 1>>

and that would add one to the first element in the $flirt_set array.  So you can change the values in an array the same way you could with any other variable.

Or, for another example, instead of doing:

<<if $flirt_C > 5>>

you would do:

<<if $flirt_set[2] > 5>>

and that would check to see if the value of the third element (because elements 0 and 1 are the first and second elements) is greater than five.

See, the number inside the brackets lets the code know which value in the array to use, and then it checks just that one value in the <<if>> macro, the same way it checks any other variable.  In other words, an array works like a whole collection of variables, all wrapped into one variable.

Because of that, there's no need to have both $flirt_A through $flirt_G and $flirt_set; instead you would just use $flirt_set, since it would be able to act like all of those variables, but also have some additional functionality because it's stored as an array.

Now, let's say you have this:

<<set $NumText = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]>>

The above array holds 10 strings.  You could use that array if you wanted to convert a number from 0 to 9 into its equivalent word.  For example:

<<set _someValue = 5>>
You have <<print $NumText[_someValue]>> moves left.

The above would display, "You have five moves left."

As long as $NumText's index is something in the 0 through 9 range, then that array will return the word equivalent of that index, because of the data we put in that array.

Besides modifying the data in an array, you can also add or remove values to and from arrays.  Here are a number of examples:

/* This creates an empty array. */
<<set $NameList = []>>

/* This adds two values to the end of the array. */
<<set $NameList.push("Bob", "Charley")>>

/* This adds one value to the front of the array. */
<<set $NameList.unshift("Andy")>>

/* Currently this will display "Andy, Bob, Charley". */

/* This will display each name in the array in the "index: name" format. */
<<for _i = 0; _i < $nameList.length; _i++>>
	_i: $nameList[_i]

/* This changes "Charley" to "Chuck" */
<<set $NameList[2] = "Chuck">>

/* This moves the first name in the $NameList array to the $FirstName variable. */
<<set $FirstName = $NameList.shift()>>

/* Currently this will display "Bob, Chuck". */

/* This moves the last name in the $NameList array to the $LastName variable. */
<<set $LastName = $NameList.pop()>>

/* Currently this will display "Bob". */

Because of this, array variables can be quite useful if you need to make lists of an unknown number of values, if you want to have a bunch of values in a certain order, and/or if you have a list of values you need to display.

Hopefully that clears things up.  Feel free to ask if you have any other questions.  :-)

by (140 points)
Oh my GOD.

Okay. Thanks so much because that was literally nowhere, that exact explanation I couldn't find ANYWHERE and you just saved my whole entire life, thanks. It was a simple thing that I could've understood in two seconds if somebody somewhere had taken the time to write the most basic explanation of Arrays instead of assuming you know.

Some of them explained Arrays only as a variable that holds multiple values without explaining that each of those values can be substituting a variable you need to use and isn't just a number value you need to be held on to. That was the part that threw me.

I thought the numbers were being held for some kind of value that was set, not that they could be climbing number values. God it's so obvious now that you've explained it like that!

The problem with some of these tutorials and stuff is that people just assume you already know what they're talking about and don't explain the basic stuff. When like, Twine is advertised as being a low-coding level knowledge software.

It's supposed to be simple and easy for people who DON'T know this stuff. So when people assume a certain level of knowledge it can be incredibly frustrating for people who don't have it.

Thankfully there's people on this forum willing to explain things but we really need somebody to make really entry-level tutorials about the more advanced function things without assuming any kind of knowledge level at all.

by (44.7k points)
Cool.  Glad I could help.  :-)