Welcome back to another construct video and this is part of my new series where i take a popular mechanic from a game and i try and recreate it in construct providing a tutorial at the same time this week i thought have a go at minecraft’s terrain generation so much bigger than The normal stuff i normally cover on here but i thought really interesting one and how can we generate that terrain now i was in minecraft’s 3d game and construct does support 3d but as you’ll see from this 3d example that i’ve set up inside of construct while we have got Terrain generation i’ve also got a tremendous amount of lag i’m only actually spawning quite a small block at the moment so even if i optimize this with chunks it’s still very very slow so contracts are not very optimized for 3d so instead we’re going to go for a more Of a terraria route and go for a 2d concept as well so load up constructs and let’s get started so once we load that first thing we need to do is actually create a player so i’m going to insert a new object scroll down to sprite and create player Now for mine i’m just going to use my normal avatar that i normally use when i make these tutorials most important thing is that we’re going to resize it to 16 by 16 because that’s what we’re going to be working in for block sizes so we want Our character to fall into the same rules once it’s done we can then just go and add our two behaviors to it so the first one is going to be scroll to so the camera follows our player and the second object we’re going to add To it or behavior we’re going to add to it is going to have a little platform behavior so it behaves like a platform character now we can start creating our different block types so i’m just going to move my player out the way to the top of the screen And we’re going to insert some new objects so first we’re going to do is we’re going to take the brass block click anywhere i’m going to resize it to 16 by 16 and i’m just going to take flat color of green and just apply it once i’ve got my grass block i’m just Going to move it to the top where our player is just so it’s off the screen and then i need to do the same for the following blocks so i need a stone block and then i need some oars i’m going to create three ores a diamond a gold and An iron so i’m going to time that through this really really quickly for you Now once we’ve got all those blocks in place we’re just going to go through one at a time and just add the solid behavior to them and now we’re all set up to actually start creating our game so last thing i want to do very quickly is just click on the layout And we’re just going to change the background color to a light blue this would just be our sky color so let me find one that i like go for that and apply it to our background now the rest is going to happen inside our event sheet so first we can grab the Advanced random object so just click on the objects folder scroll down to find the advanced random this can be really really important what we need to do today so first we’re going to add a new line of code we’re going to say on start of layout so this is going to Work as soon as the game loads and then what we’re going to do is go to advanced random and set octaves to what next and we’re going to have a lot of this is we’re going to add what’s called a blank sub event this gives us a gap underneath that We’re able to run code and this code will run only if this condition is met so on start layout this code will also run as long as the conditions met so i’m going to start by creating some variables so the first variable that we need is one called density okay and i’ll explain What this is used for when we get to it the second one that we’re going to need today is called value and again once we’re there i’ll explain what’s going on with that then or and finally a value for y well that’s up we’re ready to move to our next condition So what we’re going to do is we can go to system scroll down to repeat and we’re going to repeat for the layout heights divided by 6 steep 16 being the size of each one of our blocks if your block size is different obviously you just need to change this bit here And then click that then what we’re going to do is we’re going to go to our system i’m going to set a value i want to set the value of y equal to the loop index so how far through this loop are we this is allowing us to place a block in every Single position we’re also going to apply the density here so we’re going to set value of the density and this could be set to max and then we’re going to take y plus 20 comma zero and divide by 60. so with that in place we’re now able to Add another sub condition so add a blank sub event and then double click on it and this is going to be another repeat and now we can place a block on every single spot across the x direction we also want to be able to go into the y direction as Well so we can cover every single space so now we repeat and this time we’re doing it for the layout width sorry so we’ve done the height now we’re doing the width and again this is divided by 16 for our block height and now we can take our advantage random for the first Time but we’re going to actually do this using the set value i’m going to use this value here that we created and this is going to be advanced random classic 2d then inside the brackets we’re going to put y and the loop index loop index being that Second loop we’re creating at the moment we’re going to times this by our density and hit done now those values can be really important because this is going to allow us to check how high up the sky we are and from that we can choose to place air grass stone and so on So with that done we can now add another sub events again we’re going to have lots of these sub events going in today because we want this to happen every single loop or inside both these loops so first we’re going to check what this value equals so we’re going to compare two values Or compare value or variable we can do actually and we can say if this value is actually greater than 0.5 this gives us a value between 0 and 1. and we want to check if that’s above 0.5 this actually checks the bottom of our screen so we can check for stone So really really useful and if not we can do some other stuff on top of this used to create stuff like grass okay so we’ll look at that in just a bit first of all we need to add a new sub event and this is going to take use of our Advanced random again so we’re going to say system there are two variables and we’re going to say if advanced random this time cellular 2d and again it’s going to be our y and our index inside the two brackets today so greater than 0.55 here and then press done Now this number here is deciding if we want to place or not so if it’s greater than 0.55 we’ll just place or over stone if we want to have more stone than ors we just have this value okay so really really useful and easy to implement there i’m going to add Another sub event underneath here and this is as low as we’re going for our sub events and now we can check which all we want to place so what we’re going to do first of all before we do that blank sub condition i’ve just missed a step is we just need to actually Work out what we’re going to place first and do this all we’re going to do is we’re going to use system we’re going to go back to Set value or it’s going to be really similar to what we’ve just done before so we’re using the advanced random again so cellular 2d and then x and then loop index we’re just storing that value inside the or oh sorry y is good x there we go Now with that in place what we can do is we can check what all we want to place so we can say compare variable and we can say or is greater than 0.95 then what we want to do is we want to take our system create object And we can create my all one which is to replicate my my diamond ore if you’d like and then this is going to be set to the loop index times 16 because again we’re working with 16 size blocks this is going to be set to y times 16. This is going to place a diamond block now 0.95 means there’s a five percent chance that we’ll spawn a diamond block okay so if you want to make it less common or more common we just change that value there gonna add an else this means if we’re not placing A diamond block what we’re going to do instead and we want to place more than just a second block so what we want to do is add another condition to this and the other condition is going to be system compare variable i’m going to check if or is greater than 0.8 So this means now that we’ve got a 15 percent chance going from 0.8 0.95 that’s our difference it’s a 15 chance of another or spawning of me that’s going to be a gold ore so i’m just going to take this condition that has paste it in and just simply change it from A diamond to a gold ore i’m going to do one more so i’m going to add as an else i’m not going to add another check for all because i’m just going to say that any more orders we’re going to place are going to be my equivalent of An iron ore and again i can just copy and paste this the ease and just change this to iron now again this bit here is just checking if this value is above 0.65 so giving us a 35 chance of or spawning over stone so all of this is just controlling ores only We want to take this condition add an else to this this will add an else statement underneath and now we can do is actually copy and paste our correct object and make this stone so stone’s going to fill the rest of it like so So now we’ve got set up is we’re able to sort our different ores diamond gold and iron and we’re able to create stone as well what we haven’t done yet is actually looking at creating grass or anything like grass yet so to do that all we’re going to do Is we’re going to take this statement here add a new else statement below and then we’re also going to add another condition system compare variable i’m going to say if value is greater than 0.47 and then what we want to do is create our grass block Now what this is doing is this gives us a 3 chance of grass spawning and because the value is based off height levels it means that once we finished all our stone we’ll play some grass and then anything after that which is going to be a huge amount after it’s just going to Be a chance for air to spawn instead which is why we’re leaving that blank so now we’re ready to do our very very first test of this let’s see if it’s working so our cube spawns in and you’ll see that we’ve got a level that’s somewhat randomly generated Now this level itself is really really small so what we’re going to do is we’re going to change this so first i’m going to do is actually up these numbers this will give us much better generation than what we’ve got at the moment and look much better so let’s Do six uh six thousand by three thousand much bigger of a level also we’re gonna do is going to go to project properties we’re going to take our viewport size i’m just going to halve both those numbers so we’re looking at a bit of maths here Four four eight two five six i’ve got it saved already let’s run this again and now you’ll see that i’m able to move around my level you see there’s drops as well so it’s not like it’s completely flat and then every so often i should get some random or spawning inside my stones There you go there’s a big iron vein that i’ve got there that will spawn in veins thanks to our advanced random and we can move around the level there’s some gold now as well and then we reach the end of the level we can fly through to see all these different or spawning So really really simple bit of code once you get your head around it last thing i want to show you with this is how to set up a seat so with computers computers work off pseudo random numbers not actual random numbers so what we can actually do is we can take The seed that it’s normally provided by something like the clock which is always changing we can give it our own seeds so now we can make it so the same level spawns every single time and to do this we just go to advanced random update seed And inside here we can put the word hello hi whatever you want in here to put whatever you want in here and as long as you’ve got something inside this box it will give you the same seed every single time so we can test this by writing hello run our code And we’ll drop down we’ll see we go to a flat bit and all the way falling down there’s some iron and a curve at the bottom as long as i keep this at hello i’ll get the exact same thing happening so again exactly the same and then that iron curve at the bottom So seeds are really really useful if you don’t want it to have a scene you want to be random just delete this and that will solve that problem there the final thing i want to mention today is that this tutorial would not be possible without this procedural terrain generation example that’s on the Construct free website this helped me quite a lot understanding how this all works so they’ve got the full code here the code is in far more depth and starts dealing with chunks and world saving and how to scroll the world properly so hopefully this video today gives you An understanding of how to look and understand their codes if not just like fun that you’ve learnt today and being able to play around with if you’ve enjoyed please like and leave a comment subscribe and i’ll see you in the next video You Video Information
This video, titled ‘Minecraft’s random terrain generation with ores in Construct 3’, was uploaded by Game Design with Reilly on 2021-12-25 18:54:49. It has garnered 1310 views and 35 likes. The duration of the video is 00:16:44 or 1004 seconds.
In todays video I will show you how you can create randomly generated terrain for your 2D game including ores. Let me know what mechanic from other game I should create next.
Intro / outro music – https://opengameart.org/content/theme-song-8-bit