Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | BlackFenix06 |
Hi, I have a question, hoping for your help. I didn’t find much looking for, but maybe I missed something.
I am building the game world map and it is made up of different areas where the terrain is made of different materials (for example, areas of dirt, rock, grass, water, etc.) In practice, what I would like is that each zone is made up of its own tiles chosen randomly. Or to better explain, if I create an area of grass in building the map, for example, I want the tiles in that area to be chosen randomly from the group used for that area.
This question arises from the fact that the map I’m creating is very huge, and having to manually randomize the tiles in a certain area becomes very long. If instead there is the possibility to say for example, in that area I put a dirt zone and the tiles are randomly chosen from the group of dirt tiles, it would make the job much easier and faster. Also, since the aforementioned areas are background only (so there is no contact or interaction between the tiles and the player), every time you restart the game, the landscape changes every time.
EDIT:
I tried playing with gdscript, looking around. I created this script to try, and almost everything works, except it doesn’t change the tile to the one chosen randomly.
extends TileMap
func _ready():
for array in get_used_cells_by_id(484):
var water_0_group_tile = [484, 543, 544, 545]
var randomft = randi()%100+0
if randomft <= 40:
var randomsd = randi()%100+0
if randomsd <= 20:
var randomth = randi()%3+0
var size = get_cell_size()
var tile = Vector2(array[0]*size[0], array[1]*size[1])
set_cell(tile.x, tile.y, choose_tile_id[randomth])
How do you generate the areas? In the past, I’ve generated areas by filling grids with numbers, and substituting tiles for those numbers. Each tile can be selected from a random array of tiles (the array could contain a reference to the tile number in a tile set). Some of the code could look like this:
# Some arrays of random tiles. You do the shuffling in the _ready() function.
stone_grid.shuffle()
# These make matching the numbers in the grid easier.
const var STONE = 2
const var ROAD = 3
for tile in tile_grid:
if tile == STONE:
var stone_tile = stone_grid.pop_front()
This is just some boilerplate code, but I hope it points you in the right direction.
Ertain | 2021-02-01 00:50
I added a new part to my question
BlackFenix06 | 2021-02-01 08:24