How do I random layout?

0 favourites
  • 12 posts
From the Asset Store
Easily generate many levels from a set of pre-built scenes (Construct 3 template)
  • I have several layouts and want to add an action to go to random layout. Can someone help me out? I can not find anything about it.

  • on action > go to layout by name choose("layout1","layout2", "layout3")

    this will choose one of the three layouts by random

    You can also put the layouts names in variables and use "layout" &variable to make the string shorter.

  • I tried the first option and syntax error

  • Did you write the string exactly like this below?

    choose("layoutname1", "layoutname2", "layoutname3")

    You have to use " before and after each layouts name and a comma between each layout.

    "Layoutname" should of course be changed to a real layout name in your project

  • Ok, sorry my fault.

    And so as not to repeat the layout once started? that is, random but never repeat

  • I think that arrays would be best for that so when a layout has been used it gets removed from the array. But that is quite complicated if you're not experienced with arrays. But very very useful to learn to use early which I didn't <img src="{SMILIES_PATH}/icon_razz.gif" alt=":P" title="Razz">

    Take a look here:

    Subscribe to Construct videos now
  • Thanks, really if it seems complicated.

    Perhaps someone has a different and simpler method.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • SYSTEM > SELECT LAYOUT BY NAME > choose("name1","name2","name2,"name3")

    in this example, the system will select a random layout searching by the name... in this case we have 4 layouts ("name1","name2","name2,"name3") ... the "name2" layout appears 2 times, it means that there is one more chance to the system select the layout name2...

    To make it not repeat, you can try to use variables, for example: if the current layout is the name1 , so, the variable layout1 will be = 1 and then you can compare variables and make the things work.

    I hope it is not too confuse.

  • Thanks, really if it seems complicated.

    Perhaps someone has a different and simpler method.

    Arrays aren't complicated, but like many data structures, can be a bit hard to conceptualize at first. They're extremely useful, worth learning, and in this case, the best solution.

    Example

  • caiocesar

    Can you please explain more how to make layouts not repeating by making Variables? It will be SO helpful.

  • 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":

    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.
  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)