random world generation (just need a little guidance on how to spawn chunks)

OK, so after scrambling up my tiny little limited knowledge of python I managed to come up with a python script that generates a random chunk for a player to walk around :evilgrin: Now I’m trying to find out how I could get my script to generate new chunks around my player once he gets to the edges of the current one… Here is the blend I’ve worked on (note this isn’t for any serious project just a bit of mucking around on my part so I haven’t really given mind to a lot of things)

It would be great if anyone could help or at least point me in the right direction. :eyebrowlift:


RandomWorldGen_BlenderArtist.blend (643 KB)

I was thinking, make a bunch of chunks, with “target faces” above connection points, that are all based on a unit size,

so you start with a random shape, like a “X” " T " " W" with doors at each end of the “letter shape”

and then then have the new shapes “Tetris” in and if they do not hit the “ground plane” they are deleted, and if the door does not line up so it can draw a “bridge” easily it is deleted,


Okay, so.

  1. It’s probably not a good idea to use GameLogic anymore, as I would imagine it will be deprecated at some point. Use the bge.logic module as the API uses instead.

  2. Using empties to spawn the ground pieces is a bit crude, since you would have to place thousands upon thousands of empties to cover a world. It’d be easier to use Python to place the cubes at even intervals. This ties in with the next point:

  3. Each cube in your example is a separate object. You can do things this way if you have a limited playfield (i.e. a small number of cubes on screen at any given time), or if you’re going for a simple level of interactivity on the ground. However, I don’t think you’ll be able to get very far before the BGE starts choking on drawing them all if you want more cubes.

You mention that you want to load up new ‘chunks’ when you get to the edge of the current one, right? I assume by ‘generate new chunks when he gets to the edges of the current one’, you mean a block of land (composed of several cubes), not to just load up more cubes when you get to the edges of one (or a row) of them, right?

If you want to go for the simple route:

I would recommend rewriting the spawn code to work with Python for simplicity to start. Otherwise, you would have to deal with spawning in empties that spawn in the cubes at specific positions in the game.

If you want to go for the more complex route:

I think it would be better to make an object that represents a whole chunk of land, rather than just spawning in several small sections (cubes) of it. I would recommend using a plane or even combining several cubes into one whole object, and then dragging the vertices of each cube in the chunk mesh up and down to form the land. To move the cube vertices evenly, you can try writing a simple function to loop through all vertices in the mesh and figure out which vertices belong to which cubes so that all vertices in a cube are moved together.

You could also try using my (somewhat buggy) Flatten module to ‘flatten’ several objects (the cube) into a single one that the BGE would use. They wouldn’t be able to be interacted with anymore without un-flattening them, but you would be able to keep the FPS relatively high. The general idea would be that you would spawn the cubes to form the world, and then flatten the cubes (or portions of them) to chunk meshes (which hold the faces for the cubes).

Basically, you would just spawn the cubes from a single script, store their references (that the scene’s addObject() function returns) into a list, and then use the Flatten function on the chunk object with the list. Something to keep in mind in the future if you need speed-ups.

Once you’re done, you should be able to recalculate the physics mesh of the object, and have the land update. This way, the BGE draws the land in actual chunks (i.e. 5x5 grid spaces), and you can easily spawn or make visible the chunks of land adjacent to the one the player is currently on. To add in a unique instance of a mesh (a chunk that you can alter), you can use the LibNew Python command. The physics mesh should be able to be updated correctly for each mesh. I recall there was a bug for this, but I’d hope it’s been fixed.

Okay, so.

If you need an example of your current spawning system in Python, just say so, and I’ll see if I can whip one up that’s pretty similar.

oh wow :smiley: thanks for such an in depth reply :slight_smile: ok so the method that i have is really sloppy as it involves a really large number of empties and would be very hardware intensive as more cubes get rendered… so I’m assuming that through the ‘easy’ python way I should be able to mash together a basic terrain generator… I think that is still within my capability zones but it would still be rather ‘bad’ in that it creates cubes individually. I would like to mess around with the chunk and flatten method you said as it seems like the right way to go in terms of speed and performance but my knowledge with python isn’t exactly glorious… I can bash out a script or two but nothing of excellent quality so I think I could take up your offer on rebuilding my current spawning system in python (that is if your willing to do it and its not wasting your time :P) just so I could dig around and start working around getting a chunk system working.

Cheers :slight_smile:

Alright, here’s the example file. It should basically do exactly the same thing as before, though you can tweak the world size list to change how many blocks are spawned now.

P.S. Nice simple lighting setup. I’m doing something similar with slightly blue lights for a more tinted feel.

PythonWorldGen.blend (576 KB)

thanks for that :smiley: I guess its about time I raised my python skills above the bar so I can step away from logic bricks and all the ‘kiddy’ stuff, but I seriously must say you must be getting close to god levels with python XD to write out something like this would at least take me a whole day of thinking and a good solid couple hours of programming :stuck_out_tongue: so again thanks :smiley:

How would you do this with LOGIC BRICKS?

have each chunk spawned have a delay sensor and a random state

when a state is chosen it tries to spawn a new chunk (test with a sensor object first -> if sensor does not collide
(probably want py for this)

after it tries to place the chunk if it fails -> delete sensor and set state in spawning object back to 0 (roll again)

have some sort of system that each successful added object adds to to decide when to terminate the maze.

this is the same thing using py.