How Do I Use Random Terrain With Arrays and More

0 favourites
  • 6 posts
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • Hey! It's me again, been a while since I toyed around with Construct 2, or any game creating engine to be honest, I was looking through some of my projects and found on Game Maker my attempt at a Terraria type game and thought "This could be done so much easier in Construct 2!" so I'm converting my entire project to Construct 2 and I was looking around at the best way to do the terrain generation and found Arrays to be the most efficent way (Of course) and I started toying around with it.

    It's kinda working, as in everytime I launch it / refresh the page I get a new randomly placed jumble of air, dirt and stone blocks.

    But of course this is not exactly how I want it to work, I was wondering how do I make it so that say between the top of the layout till about... For argument sake say 32 pixels down it will only generate air blocks and then between 32 and say 64 only dirt blocks, between 64 and 80 it can mix stone and dirt and anything below that is stone.

    I have tried making a variable called worldheight which gets it's number from the layout height and then try to read inside that but I just can't get it to work.

    I'm assuming I'm gonna have to find some sort of perlin noise plugin to actually generate the terrain to make hills but to begin with I just want to be able to generate a flat surface as a starting point.

    I have a few other questions that pops into my mind as these will come up later I'd rather ask them now than later, better to keep them all in one place right?

    1. Should I be using Arrays to create trees or should I create a preset loadout of trees that is randomly picked everytime a tree is spawned / grown from a seed or is there a better way to randomize trees with leaves and all?

    2. Ore deposits, how would I create this the most efficent way without making it too much or too little? Also I assume I could use this same techinque to make underground cave systems by just replacing the ore spawned with air blocks to make air pockets?

    3. Is there a way to make so that everything outside the screen for the player is not loaded but the information is there so it loads up as the player gets there? This would reduce lag due to the lower RAM usage I imagine as this was an issue I had in my project in Game Maker or will this not be an issue as I will have set world sizes by making it so that they only fit inside the layout size which will have multiple different size settings when creating the world?

    4. Anyone know any good inventory / hotbar tutorials I can take a look at? I got a basic one up and runing but it only makes it so dirt can only be in one specific slot and stone in another specific slot so it's not very effective as I want the player to be able to move the items around as they please.

    5. Also, crafting, how hard would this be to make? I was thinking a fairly simple crafting system like Terraria where if I have the required materials in my backpack it appears in my list as craftable items and I click it and it gets made, it shouldnt be too difficult. Come to think of it I could probably figure this one out myself but I just wanna check if my thoughts on it work.

    Wouldnt it just be a set of variables? A variable check that checks for say 2 wood and 3 stones and a button appears in my crafting list that says "Stone Pickaxe" I press it, the variables are pulled off my total amount, a stone pickaxe is added in my inventory and if my total material amount goes below the requirements the button vanishes/gets greyed out from the list. Is this the correct way to go?

    Another question that is not related to this project is I've noticed when I try to testrun my game either with or without debug mode sometimes it just don't load / takes up to 5 minutes to load, usually I have to save my project and restart the program before it starts up instantly when I press play.

    This is not on big projects, I could literally place down a 32x64 platform with solid behavior and a little 16x16 character with the platform movement behavior and it'd still take so long sometimes and sometimes it's instant.

    I'm sorry for asking so much but I find it's better to ask everything at once instead of asking it separate, I have looked at many capx files for terrain people have posted and some have been fairly nice but I havent been able to figure out what makes them tick and most have not been what I'm looking for.

    Thanks in advance for anyone who can help me with either of these questions or point me in the direction I need to look in order to start tackling this.

  • I need this solution too!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Assuming in your array you use:

    0 for air

    1 for dirt

    2 for stone

    You could do the following as sub-events to a start of layout to populate the array:

    for "y" from 0 to 32

    for "x" from 0 to array.width-1

    --- set array at (loopindex("x"), loopindex("y")) to 0

    for "y" from 33 to 64

    for "x" from 0 to array.width-1

    --- set array at (loopindex("x"), loopindex("y")) to 1

    for "y" from 65 to 80

    for "x" from 0 to array.width-1

    --- set array at (loopindex("x"), loopindex("y")) to choose(1,2)

    I don't know how you're placing the tiles so you can see them but the usual way would be to loop over the array and create the correct tile based on the value in the array. Say your tile size is 32x32 you'd create the tile with something like "create dirt at (array.curx*32, array.cury*32)".

    It will be slow if you use a sprite per tile so a better way would be to use the tilemap object instead and set the tiles from the array.

    For hills and whatnot this post could be helpful, it also shows the use of the third party noise plugin:


    It all depends on how you're doing the trees, like will they be blocky or sprites. Either way you could loop over the surface tiles and occasionally grow a tree. One way would be to spawn random seeds in the air, let them fall and have them grow where they hit.


    For that perlin noise could be useful. Or you could use some oddly shaped and randomly placed sprites during the generate stage, and set all the tiles it overlaps to ore or something.


    The tilemap object will be your biggest friend for this as it's very efficient at drawing only what's on the screen regardless of how big the world is.


    I don't know of any off hand but I know there are lots of tutorials and examples to study. Do a search, I'm pretty sure there are examples that do as you describe.


    There are many ways to do it, but your idea and the approach you used in gm will most likely work here.

    As for 5 minute loading, I haven't ever encountered such a delay. If use a massive array it could take some time to loop over to generate, but otherwise I have no idea.

  • Thanks for your help man, I've been trying to do this all day and I can get the loop and array in order but I just cant figure out how to make the tilemap spawn the tiles at the right place atall...

    The Array states is right as I have it X is 5 and Y is 10, I set the loop to go Y 0-3 = 0 (Air), 4-6 = 1 (Dirt) and 7-9 = 2 (Stone) and the debug mode for the Array says 1-5 and they all have 0,0,0,0,1,1,1,2,2,2 so the arrays are correct.

    I have tried everything I can think of (obviously not the right things XD) to try to make the tilemap set the right tiles in the apropriate locations but all I get is the air tile with 16 16x16 blocks space apart from each other both on the X and the Y axis.

    My code for spawning blocks atm looks like

    Event: Array Current Value 0

    Action: Tilemap Set Tile (Array.CurX*16, Array.CurY*16) to tile 0 (normal)

    The same for dirt and stone but replacing the 0 with 1 and 2 respectivly.

    It work with just the one sprite called Blocks and just set one animation frame for each type of material but not the tilemap.

    Sorry for the super slow response, I never got a email that I got a reply on this and only just saw it the other day when I got in on the site by accident.

    Edit 5 minutes after post... I realized I'm a stupid... The CurX*16 and CurY*16 would only be for the sprite wouldnt it? Cus when I removed the *16 from them it all came together... xD

  • So I've managed to make the tilemap behave as I want it to but I cant for the life of me figure out how I'm meant to make it so that when I erase a tile it give me the resource I just mined... As far as I can tell there is no way to check what ID the thing I just erased had and use that to trigger an adding of 1 dirt / stone

  • Phealin

    The .TileAt(x,y) expression should give you the ID. Just look at it before you erase the tile.

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