var AllLevels = "DBCA"[/code:23l96eti]
We're going to use a method to do the following
[ul]1. Pick a random character (i.e. one of the letters) from AllLevels, and save it to a variable
2. Remove the character from AllLevels
3. Go to that picked layout[/ul]
We're going to leverage the Len(), Random(), Floor(), TokenAt(), and Replace() expressions. Here's their definition from the manual:
[ul]
[b]len(text)[/b]
Return the number of characters in text.
[b]random(x)[/b]
Generate a random float from 0 to x, not including x. E.g. random(4) can generate 0, 2.5, 3.29293, but not 4. Use floor(random(4)) to generate just the whole numbers 0, 1, 2, 3.
[b]floor(x) [/b]
Round down x e.g. floor(5.9) = 5
[b]tokenat(src, index, separator[/b])
Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.
[b]replace(src, find, rep)[/b]
Find all occurrences of find in src and replace them with rep.[/ul]
Alright, just to reiterate, the first step is to pick one of the random characters (i.e. letters) from the AllLevels variable. To do that, we first need to know how many total characters there are in the string. That's where the Len() expression comes in handy. Let's get the length of AllLevels, and stick it in a variable called "StringLength":
[code:23l96eti]
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)
[/code:23l96eti]
Right now, the string is 4 characters long. Since Construct 2 uses 0 based indexes, those 4 characters correspond to index 0 (D), 1 (B), 2 (C), and 3 (A). We need to randomly choose one of those indexes. To do that, we'll use the random() and floor() function, and stick the random index into a variable called "RandIndex".
[code:23l96eti]
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))
[/code:23l96eti]
Now that we have our random index, we need to get the character at that index from AllLevels. To do that, we'll use the tokenat() function, and assign the results to a variable called "SelectedLevel"
[code:23l96eti]
var Allevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))
var SelectedLevel = tokenat(AllLevels,RandIndex,"")
[/code:23l96eti]
Ok, we have our letter, and we'll go to the appropriate layout in a moment, but let's first remove the SelectedLevel from AllLevels, so the process doesn't pick it again later. There are a couple ways to do it, but let's use the Replace() expression.
[code:23l96eti]
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))
var SelectedLevel = tokenat(AllLevels,RandIndex,"")
var AllLevels = replace(AllLevels,SelectedLevel,"")
[/code:23l96eti]
Finally, let's go to the selected Layout.
[code:23l96eti]
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))
var SelectedLevel = tokenat(AllLevels,RandIndex,"")
var AllLevels = replace(AllLevels,SelectedLevel,"")
Go to Layout by name "Layout"&SelectedLevel
[/code:23l96eti]
There you go. Repeat the process whenever you need to go to another Layout. You should now know how to use variables to go to a random, non-repeating stage. We're done here, right?
[h2][b]NO![/b][/h2]
This is convoluted and problematic. For example, it won't work for more complex Layout names? The bottom line is that variables are a poor way of selecting random non-repeating values. Variables are meant to hold 1 value, not a series of values. Sure, you could make it work, but they're not the right tool for the job. Arrays, on the other hand, are far more appropriate. Arrays are, by definition, a series of values. They're also far more flexible, and easier to manipulate.
I'm assuming you're asking how to use variables in this scenario because you're not sure how to use arrays. Well, if you were able to follow along with the above steps, you can use arrays because it's pretty much the same process. Here's how to use the exact same method with Arrays:
Set the size of the array to 0,1,1. This empties the array, but makes it ready to receive a list of values.
[code:23l96eti]
Array Size = 0,1,1
[/code:23l96eti]
Insert the different layout values to the array. I'll use just the letters to remain consistent with the previous method, but you could just as easily use the full Layout names.
[code:23l96eti]
Array Size = 0,1,1
Insert "D" into Array
Insert "B" into Array
Insert "C" into Array
Insert "A" into Array
[/code:23l96eti]
Determine a random index based on the width of the array. No need to use len().
[code:23l96eti]
Array Size = 0,1,1
Insert "D" into Array
Insert "B" into Array
Insert "C" into Array
Insert "A" into Array
var RandIndex = floor(random(Array.Width))
[/code:23l96eti]
Get the value stored in the array at the random index. No need to use Tokenat().
[code:23l96eti]
Array Size = 0,1,1
Insert "D" into Array
Insert "B" into Array
Insert "C" into Array
Insert "A" into Array
var RandIndex = floor(random(Array.Width))
var SelectedLevel = Array.at(RandIndex)
[/code:23l96eti]
Delete the index from the array. No need to use replace():
[code:23l96eti]
Array Size = 0,1,1
Insert "D" into Array
Insert "B" into Array
Insert "C" into Array
Insert "A" into Array
var RandIndex = floor(random(Array.Width))
var SelectedLevel = Array.at(RandIndex)
Delete index RandIndex from Array
[/code:23l96eti]
Finally, go to the selected layout:
[code:23l96eti]
Array Size = 0,1,1
Insert "D" into Array
Insert "B" into Array
Insert "C" into Array
Insert "A" into Array
var RandIndex = floor(random(Array.Width))
var SelectedLevel = Array.at(RandIndex)
Delete index RandIndex from Array
Go to Layout by name "Layout"&SelectedLevel
[/code:23l96eti]
Check out the capx I posted above. If you made it to the end of this unexpectedly/unnecessarily long post, I bet you can understand the capx now. Like I said earlier, Arrays are extremely useful, worth learning, and in this case, the best solution.