@BluePrintRandom has based his very own existence on doing that in the BGE lol
But before he shims here, yes it is possible, but there are a lot of constraints to take into account.
The easiest way I think would be to have a script partitioning space in tiles, and evaluate which tiles need to be shown based on the position of the player. If your grid is a regular square pattern, then the look up can be made pretty easily manually, if someone talks about KDTrees just forgot about it here, really.
So once you are able to split space and lookup tiles, then you need to have your tiles be part of your simulation, not just a Python object in memory: At that point you can have a lot of “blocks” (meshes + materials) on an inactive layer, ready to be spawned. So a tile would be composed of a bunch of blocks arranged in some way, that you spawn proceduraly when you spawn your tile…
The process should be random-ish, but you don’t want it to be purely random. What you can use is perlin noise in that case, it is the same kind of noise that is used for some textures: instead of just outputting random values after each call to the random function, perlin noise always outputs the same value for a given (x, y, z) passed to the function. But the way that value evolves when you compute points around is smooth, but random.
Perlin noise example:
That’s just one way of obtaining random values for a 2d space (your infinite map).
BGE/Blender already has a module with some useful methods for randomness:
Or maybe you can simply use the Python standard library?
from random import Random
# tile fetching...
# a tuple can be hashed (to get a pseudo random value)
position_as_tuple = tuple(tile.worldPosition)
# we can seed the generator using the hash
generator = Random(hash(position_as_tuple))
# get a random value between 0 and 1
tile_random = generator.random()
# you are now free to process that value however you want!
Disclaimer: The Python script I showed you is just a snippet, some variable definitions are missing and it is up to you to fill the blank and make it work inside the BGE.
Overall it is not really hard to do, but there are some steps involved, such as creating a script that will:
- Split space in tiles and lookup nearby tiles around player
- Compute unique random value for each tile
- Generate landscape by spawning blocks in a coherent-ish way
- Delete tiles and spawned object when too far
Then it means you also need to create a building set of objects in order to populate your world.
If you feel overwhelmed, the first step can be to try to make a system that would spawn planes as if they were tiles and have a player move around and see how to add new tiles (planes) and remove the old ones.
If you manage to do that you already are on a good path