Simple Terrain Generation in Godot

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone not left here today we're going to be generating these random terrains in the video game engine version 3.2.3 stable and let's get right into it i'm gonna make sure that the way i explain this will be applicable to any sort of game or any game project you want to make let's get right into coding this so i want to start off by saying you need to have some assets usually i make my tutorials in mind for people who don't have assets and who just have the good old sprite or default icon but you actually need some assets if you want this to look nice or at least look like a sort of map so you so for this basic setup i just have a snow-capped mountain type color a grassy color a rocky color and a watercolor and i have another project where i would i went with nine or i think eight or nine sprites and you can see the result of that uh later on in the video but essentially what you want to do is you want to take these sprites and you want to make them into tiles and you can see that i can draw with these tiles i can literally do whatever i want but i don't want to do that i want the program i want godot to do that for him we're going to start off by attaching a game script to our game scene or tile map you can do which one ever whichever one you want i just chose game team because this is the only thing i'm doing on this project if you're making a bigger project you might want to attach it to the tile map so what are we going to do or according to first principles what's the best thing to do well we want to get declaration to a couple key variables oh and i forgot to mention one thing uh you want to make sure that these sprites are really small like 8x8 i don't know where there's a shadow there but 8x8 right these are all eight by eight sprites you can see and essentially the reason you want to make it small is because you want the tile map to place these in a way where it looks like terrain of course you can make them bigger of course like uh there's no limit on what size you can do but i just chose eight by eight because it helps make something that fits in with the default uh godot frame size and you can see we just want to make reference to this uh corner here so it says 127 by 74. that's the um that's a really important number because that's actually the width and height 127 is the width height is 74. so uh to code this we want to get some declarations to importing variables so we could say stuff like our width which we just said was 127 let's make it 128 so we don't miss a couple of blocks and i believe it said 74. yeah 74 75 i'm gonna and i'm going to explain why we have to type in a number a little bit more than one over here we also want to make sure we get reference to our sprite no we don't need to get reference short spread there is no sprite um so you have to get reference to a tile map which is the title map over here and of course if the script is on the tile map itself you don't need this whatsoever and then this is the key the key player here is open simplex noise not new we want to make a variable about this uh you can also make osn i just chose open simulx noise so um for those of you watching if you ever forget what osn stands for i'm just making sure that i keep the variable name as clear as possible then in the ready function we want to uh do a couple things we just want to say randomize is uh we just want to randomize the random numbers goodwill uses we want to say open simplexnoise.seed which is actually a property and oops is actually a property inside of this open simplex noise class seed is just a value this uh object uses to help generate its map it's kind of like the seed in minecraft it it specifies each map so in case you ever have some players from um if you ever have like an online forum for your game and someone says oh what um this is a seat i found stuff like that that's essentially what the food is for i can even show how to make the seed specific each time um we also want to set the octaves equal to five i just found that's the best one of course that requires experimentation and i'm going to link another project here and show it uh show what these octaves and a bunch of stuff does but i'm just going to get i just want to get the viewer to a point where they can make this themselves and then we're going to make a generate map function and essentially what we're going to say in the generate map function is actually i'm going to take this out because it's kind of scary looking we just we just want to say something very simple we want to say we want to generate a map and we want to say for x and width for y and height and what does that mean essentially what for x and width and for y and height means is that we're going to go down like this for downwards and then when we hit the bottom we're going to go back up to the top oops we're going to go back up to the top and do the same thing but of course we're going to be passing in different tiles and if you're still confused about why this these lines of code work it's it's essentially saying in fact we can just print out x and y you can see what happens um why do i have to sit here and explain it when you can literally see that good old will say nothing because we never called the function of course uh generate map and you can see godot will literally print out printing takes some time and you can see that every time uh x uh increases our y increases by the entire amount of width and you can see if i scroll down you can see it stops at 74. so and this is exactly where why we need to add plus one it's because for x and width stops one short because it says 4x in let's say 10 it goes one two three four five six seven eight nine and then it says it says ten is not inside of ten so we're not going to use it and we're just going to be using one to nine or zero to nine actually so it still prints out ten digits or in this case it will still print out 75 digits because it starts at zero if that makes sense and that's essentially what we're going to be doing but we don't want to print out x and y because that doesn't show us anything we want to set our tiles to something and you can see with if i replace it with two lines of code we get entirely blue we get something entirely blue if i replace it with two and i'll explain where these numbers are coming from we got something else these numbers essentially refer to these tiles over here and you can see at the end at the end of them it tells you what that is when i typed in zero i got blue when i typed in two i got brown if i typed in one i would get green and you can see because a rand over here is exactly what's over here um it means the same thing we get entirely we get something entirely green but we don't want to say we want to get something entirely green we want to get random different noise pattern so how do we get random different noise patterns well we get them through the open simplex noise get it through this open simplex noise object which we created an instance of by saying dot new we're basically going to say something a little bit scary so i'm going to trim it down a bit um so essentially what we're going to be saying is we're going to be getting open simplex noise dot get noise 2d and we're going to pass in x and y what's get noise 2d it's a function within open noise 2d itself which basically says when you pass in an x and y into this function we're going to give you a random negative one and one value well that's interesting a random negative a value from a random value from negative one to one and of course uh when i mean random it means like there's like a pattern to the randomness like there's a some sort of like terrain generation type a randomness it's not random like one zero one five four do you know what i mean so of course it doesn't go up to four and five but we can change that so now we're going to say take the absolute value because we don't want to get negative ones we don't have any tiles over here that have the negative one remember very well we only go from zero to three and with that being said if we're going from zero to three we wanna multiply this by well we actually wanna multiply it by three because it returns a value from negative one to one so let's say it returns one we get the let's say it returns negative one we get the absolute which means we get positive one because we just take away the negative sign and then we're going to multiply it by three so we can also get snow-capped mountains but then we also want to floor it because it returns a value from negative one to one it can include 0.5 0.005 whatever we want to floor it so we get a proper whole number integer which is exactly what is used by these tiles over here these are not 3.5 1.2 it's a specific whole number an integer so after that we just run the scene and you can see we get random maps now i don't want to keep closing and opening the scene just because that's a really tedious process so we can just add in this line of code here which says if event is action pressed which is uind which is the end button like you can see it's the button that lets me navigate these text lines and we're just gonna say at the get tree dot reload currency we're just going to refresh the scene and that's essentially it so we get you can see that we get these uh mountains and stuff or we get these land masses but you can see that we're not getting our mountains and stuff well why not because we actually have to increase this number a bit uh so i just found five works the best and remember if you go over a certain limit you're going to start getting grey splotches everywhere why um it's not because these are mountains or something i put in it's because uh there's no number greater than four over here so when is when you tell gordo hey can you please put in tile number four it goes i don't have that so i'm just gonna put in a default tile or a clear tile which is just the background and you can see that if i made the background and if you don't believe me uh if i made the default clear color to red or something you're gonna see red rooftops or whatever it's just the background default clear color that your project has now i did glaze over a couple facts which i'm going to go over now in the open simplex noise object class whatever you want to call it there are these values you have to be aware of which is octaves periods persistence uh and um lacrinarity let's just call it lacrinerity i'm not 100 sure how to pronounce it but these influence how your map is generated now i like i'm not sure about you but these mean very little to me even if i read the description it just says that we we use um the value provided to uh generate something or a lower frequency or whatever that means nothing to me so i'm gonna be right back so i actually made a second project entirely which actually helps you helps demonstrate these uh ideas so you can see period will just mess up and not mess with your project but it kind of zooms in and out your project if you want to think about it like that and that's what you would use it for uh remember um period is the top slider octave is this top slider persistence is this lighter lacrinerity is this slider so we can go to octave next and you can see octave kind of defines how sharp it is and of course i want to leave this project in on jhub you can play around with it yourself octave kind of defines how um strong the shapes are i guess you could say like these are really strong like well-defined clear edge rough shapes well these are more fluid and like circular uh we can see persistence over here it basically it looks like a mess but if you think about it it defines how how much value nearby affects a value uh i want to kind of think of persistence like how much a value uh nearby affects a value that's going to be generated for example you can see over here let's just say take this splotch of land so around this dark blue we have light blue and then around light blue we should only have yellow around yellow we should have green and around green we should have brown but we can't have brown next to blue because uh blue because that doesn't make a lot of sense and of course there are cases where this happened and i think i just saw it somewhere i'm not 100 sure but it does happen but think of persistence kind of like how um how how much of a value influences a value next to it kind of and lacrinerity you can see uh defines like um the smoothness of the shape kinda and of course uh these are just my um thoughts about it uh you have to go around play with this and try it out yourself and you can obviously come up with much better ideas about what these values really mean i know sebastian lag i'm pretty sure i'm pronouncing i'm not sure if i'm pronouncing that right but he has a very good videos detailing this as well and this is also the project where i use a bunch of other colors you can see that um these assets were actually smaller i went by a four by four square and i had a bunch of colors so i could see a bunch of other terrains and you can see white never came up it never came up once actually but we're using the exact same function and i'm using multiplying it by 12. let's say i multiply by 15. of course i'm gonna get much more a lot crazier terrain that you can now you can see there's some white um yeah and it just looks like a map kind of because that's what you're going for and yeah this is just the code i'm of course i'm going to leave everything on jhub i said i was going to show how to get a seed so you can just do export var so we can just say a ver we just make a variable called inseed and we set it equal to a string and then we're going to say if and seed is true which means if there is something there then we're going to set osan and that's why i use open simplex noise as an example of something else you can use to hash not has hash nc so it actually creates a value from the string we pass in so i'll actually show what that means so print a hash in seed because we want to see it right and else if there's no seed input in then we're just going to go with our random integer so we can see when i go over here to gamescene you can pass in a c of course i'm going to pass in nav labs and you can see this is the map nandlabs generates and that's the hash hash code and uh sorry it went blue because you put up here because the period is zero it doesn't make sense for the period to be zero so just like blank seven says i'm not doing anything so this is the hash of net labs uh in this uh video instance i'm running in this video game engine running and when i run it again you can see i get the exact same stuff um let's actually take like a i remember like this area was over here again these white these uh uh snow peak mountains over here and there's like one over here i can show you when i run it again i get the exact same thing let me just increase the thing uh let me just increase the period and you can see i got the white snow-capped mountains again and if i type in nat labs with an s i get something completely different um yeah you can see that there there was like a white snow cap mountain over here but it's totally different so yeah that's essentially what i want to show something you can implement in your own games and i'm going to leave both projects on github down below i might leave the open no open simplex noise slider on h i o maybe i'm not sure uh depends when i'm editing this uh whatever but that's all i have to say for this tutorial have an amazing day
Info
Channel: NAD LABS
Views: 5,611
Rating: undefined out of 5
Keywords: Godot, Opensimplex Noise, Procgen
Id: weOC_zpB5kA
Channel Id: undefined
Length: 13min 9sec (789 seconds)
Published: Thu Dec 10 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.