How do I random layout?

Get help using Construct 2

Post » Fri Aug 21, 2015 12:06 am

@SashikLV

Well, I can think of a few ways to use variables to accomplish this. Let's pretend there are 4 layouts:

  • LayoutD
  • LayoutB
  • LayoutC
  • LayoutA

Let's make a variable called "AllLevels", and set it to "DBCA":
Code: Select all
var AllLevels = "DBCA"


We're going to use a method to do the following

    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

We're going to leverage the Len(), Random(), Floor(), TokenAt(), and Replace() expressions. Here's their definition from the manual:

    len(text)
    Return the number of characters in text.

    random(x)
    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.

    floor(x)
    Round down x e.g. floor(5.9) = 5

    tokenat(src, index, separator)
    Return the Nth token from src, splitting the string by separator. For example, tokenat("apples|oranges|bananas", 1, "|") returns oranges.

    replace(src, find, rep)
    Find all occurrences of find in src and replace them with rep.

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: Select all
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)


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: Select all
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))


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: Select all
var Allevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))
var SelectedLevel = tokenat(AllLevels,RandIndex,"")



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: Select all
var AllLevels = "DBCA"
var StringLength = Len(AllLevels)
var RandIndex = floor(random(StringLength))
var SelectedLevel = tokenat(AllLevels,RandIndex,"")
var AllLevels = replace(AllLevels,SelectedLevel,"")


Finally, let's go to the selected Layout.

Code: Select all
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


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?










NO!

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: Select all
Array Size = 0,1,1


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: Select all
Array Size = 0,1,1
Insert "D" into Array
Insert "B" into Array
Insert "C" into Array
Insert "A" into Array


Determine a random index based on the width of the array. No need to use len().
Code: Select all
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))


Get the value stored in the array at the random index. No need to use Tokenat().
Code: Select all
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 the index from the array. No need to use replace():
Code: Select all
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


Finally, go to the selected layout:
Code: Select all
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



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.
B
63
S
19
G
8
Posts: 341
Reputation: 12,130

Post » Mon Aug 24, 2015 12:43 pm

@zatyka

Thank you for your reply.

My random layouts are working now. Thank you for your explanation and Especially for .capx file. Used your instructions, correct Array to what I need and it all works fine.

Thank you :)
- Sashik
B
25
S
8
G
1
Posts: 11
Reputation: 3,026

Previous

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests