Hi. I am new to the forums but I am senior at Corona I was just wondering that if you are creating a “Terraria” or “2D Minecraft” block -like game, how/what is the easiest way to generate/create and save the world?
Hope you answer!
Hi, I’m going to assume you will basically be keeping a table of coordinates which return a texture index or similar (e.g. Input coordinates 1,1,1 and table returns “grass”). If you’re doing something along those lines, with a table storing all the data for the world the player is currently in, then I would suggest following through https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/ - there may be better / more efficient methods, but as I’ve only dealt with small tables, that has worked fine for me thus far. Even if you have multiple tables for a single world, you can use that method and save the whole set as separate files. Let me know if this isn’t what you were getting at. Cheers, Simon Dixon Court Entertainment
Hi. My idea was like; how/what is the “easiest” way to *generate* the world, should I use for function? Could you send me a “sample code” Thanks
Hi Akseli,
Ok, so just had a quick look at Terraria (having never played before), and I think I get what you’re trying to do - although there are still plenty of decisions to make before figuring out how to generate the world.
You’ve said you’re senior at Corona, so I’ll just roughly outline how I would approach it - unfortunately I don’t have time right now to write up some sample code.
You need the world to be endless - that means you can’t just generate it once and be done, but will need to procedurally generate the world as the player explores. I’ve assumed you’re limiting the world in the y direction (up and down), but not in the x direction. In this case, if you write a function that can generate, say, one “screen” width of world of whatever height you choose for the world, then you could call this function every time the player approaches one edge of what is already generated. (If you want to make the world unlimited in the y-direction as well, it is essentially the same process, since it’s the ‘ground level’ blocks of world that would be the more interesting parts, with above ground being mostly empty, and below ground being mostly variations on earth/rock/etc. with possible caves or whatever you choose to include, but you’d have to feed the function both an x and a y starting coordinate.)
Note: even if you want to limit the world in the x-direction, it’d still be sensible to generate blocks as the player explores, since if they never go beyond a few hundred blocks one way or the other it’s a waste to have generated the world beyond that.
You could implement coordinate system with x = 0 at the centre, where the player starts. If the screen is 21 blocks wide, write a function that generates a 21 block wide section of game world around whatever input integer you give it. At the very start, call that 3 times; once centred at x = 0, which would create the world from x = - 10 up to x = + 10, call it once at x = -21 and once at x = +21. (Or you could generate it five times, and do one at x = -42, and one at x = +42, depending on how far beyond what the player sees you’re comfortable doing.)
Then, whenever the player gets a certain distance from the edge of the currently-generated world call the function again to create some more world beyond them (e.g. if they can see the world to x = +21 on screen, then call the world gen function centred at x = +42).
In terms of the generation of the world within those blocks, you essentially need three pieces of info: 1. the height of the earth at that point, 2. whether anything is above ground (trees, buildings, etc.) and 3. whether there are any geological features below ground / what material the earth is made of (if variable). You’ll need to set your own rules for these parts; at the most basic level you could randomly set the earth height – more likely you want to set it to be either the same as, or one or two blocks higher or lower than the earth height at the neighbouring position. Then you can randomly determine if there’s a tree (or any other features you might have – boulders? buildings? animals? etc.). Finally, have a random depth of dirt, then clay or rock, or have the whole thing as dirt, with random patches of whatever materials you want them to be able to find.
Let me know if any of this is of help; if I have more time later, I might be able to spend a little longer - but I’d need a bit more info about what you want the world to look like. Otherwise perhaps you could have a go, and share what you get done, and we could work from that.
Cheers,
Simon
Dixon Court Entertainment
Hi, I’m going to assume you will basically be keeping a table of coordinates which return a texture index or similar (e.g. Input coordinates 1,1,1 and table returns “grass”). If you’re doing something along those lines, with a table storing all the data for the world the player is currently in, then I would suggest following through https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/ - there may be better / more efficient methods, but as I’ve only dealt with small tables, that has worked fine for me thus far. Even if you have multiple tables for a single world, you can use that method and save the whole set as separate files. Let me know if this isn’t what you were getting at. Cheers, Simon Dixon Court Entertainment
Hi. My idea was like; how/what is the “easiest” way to *generate* the world, should I use for function? Could you send me a “sample code” Thanks
Hi Akseli,
Ok, so just had a quick look at Terraria (having never played before), and I think I get what you’re trying to do - although there are still plenty of decisions to make before figuring out how to generate the world.
You’ve said you’re senior at Corona, so I’ll just roughly outline how I would approach it - unfortunately I don’t have time right now to write up some sample code.
You need the world to be endless - that means you can’t just generate it once and be done, but will need to procedurally generate the world as the player explores. I’ve assumed you’re limiting the world in the y direction (up and down), but not in the x direction. In this case, if you write a function that can generate, say, one “screen” width of world of whatever height you choose for the world, then you could call this function every time the player approaches one edge of what is already generated. (If you want to make the world unlimited in the y-direction as well, it is essentially the same process, since it’s the ‘ground level’ blocks of world that would be the more interesting parts, with above ground being mostly empty, and below ground being mostly variations on earth/rock/etc. with possible caves or whatever you choose to include, but you’d have to feed the function both an x and a y starting coordinate.)
Note: even if you want to limit the world in the x-direction, it’d still be sensible to generate blocks as the player explores, since if they never go beyond a few hundred blocks one way or the other it’s a waste to have generated the world beyond that.
You could implement coordinate system with x = 0 at the centre, where the player starts. If the screen is 21 blocks wide, write a function that generates a 21 block wide section of game world around whatever input integer you give it. At the very start, call that 3 times; once centred at x = 0, which would create the world from x = - 10 up to x = + 10, call it once at x = -21 and once at x = +21. (Or you could generate it five times, and do one at x = -42, and one at x = +42, depending on how far beyond what the player sees you’re comfortable doing.)
Then, whenever the player gets a certain distance from the edge of the currently-generated world call the function again to create some more world beyond them (e.g. if they can see the world to x = +21 on screen, then call the world gen function centred at x = +42).
In terms of the generation of the world within those blocks, you essentially need three pieces of info: 1. the height of the earth at that point, 2. whether anything is above ground (trees, buildings, etc.) and 3. whether there are any geological features below ground / what material the earth is made of (if variable). You’ll need to set your own rules for these parts; at the most basic level you could randomly set the earth height – more likely you want to set it to be either the same as, or one or two blocks higher or lower than the earth height at the neighbouring position. Then you can randomly determine if there’s a tree (or any other features you might have – boulders? buildings? animals? etc.). Finally, have a random depth of dirt, then clay or rock, or have the whole thing as dirt, with random patches of whatever materials you want them to be able to find.
Let me know if any of this is of help; if I have more time later, I might be able to spend a little longer - but I’d need a bit more info about what you want the world to look like. Otherwise perhaps you could have a go, and share what you get done, and we could work from that.
Cheers,
Simon
Dixon Court Entertainment