This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

Hello. With the beginning of 2020 I start a project of small 2D game in Godot 3.1. I set a tilemap and randomly generate 2D map. I would like to know how to add different variables to tiles. For examle I would like to mine iron from Tile01 and cut trees in Tile02 and mine gold in Tile03. Can you help me? I would like to know how to add such statistics to specific tiles and then use them as tileset to randomly generate 2D map.

in Engine by (15 points)

When you create tiles you can assign a name and godot automatically assigns each tile created starting from scratch. Through gdscript you can access each tile through its name or ID. Here are some examples:

extends Node2D
    onready var tilemap=$TileMap #the child tilemap 
    onready var tileset= tilemap.tile_set #the tileset of tilemap

    var tile_types = {}
    var resources = {}

    func _ready():
        #save the tiles ids and names in a dictionary, see the function declaration
        tile_types= make_dic_tiles()

        for i in tile_types:
            print("Tile dictionary keys: ", i) #print keys
        for i in tile_types.values():
            print("Tile dictionary values: ", i) #...values

        #print array of all tiles id:       
        print("Tile IDs: ", tileset.get_tiles_ids()) 

         #print id for name tile (-1 if no exist):
        print("Name tile if exist: ", tileset.find_tile_by_name("castle"))

        #print all names your tiles:
        for i in tileset.get_tiles_ids():
            print("Tiles names: ", tileset.tile_get_name(i)) 

        #print total tiles
        var total_tiles=tileset.get_tiles_ids().size()
        print("Total tiles: ", total_tiles)

        #print size tiles: 16x16, 16x32, etc:
        for i in tileset.get_tiles_ids():
            print (region_to_tiles(tileset.tile_get_region(i).size))    


    func _process(delta):
        #example: delete tiles using mouse. Using (world_to_map) convert global position to tile position
        if Input.is_mouse_button_pressed(BUTTON_LEFT):
            var x_t=tilemap.world_to_map (get_local_mouse_position()).x
            var y_t=tilemap.world_to_map (get_local_mouse_position()).y
            # -1 no tile:
            tilemap.set_cell( x_t, y_t, -1) 

        #example: get tile id whit the mouse position:  
        if Input.is_mouse_button_pressed(BUTTON_RIGHT):
        #if Input.is_action_just_pressed("right_mouse_click"):
            var tilepos=tilemap.world_to_map (get_local_mouse_position())
            print(tilemap.get_cell(tilepos.x, tilepos.y))       

    func make_dic_tiles(): #return dictionary all tiles {tileID, tileName}
        var temp_dict={}
        for i in tileset.get_tiles_ids().size():
            temp_dict[tileset.get_tiles_ids()[i]] = tileset.tile_get_name(i)
        return temp_dict

    #x and y= count tiles to start (no real position): (Alternative: use world_to_map)
    #the tiles can measure more than cell size, this function does not take that into account:
    func random_fill(x, y, width:int, heigth:int, tiles:Dictionary) -> void:
        for i in width:
            for j in heigth:
                #random int number...0 to total tiles id 
                var rnd=randi() % tiles.size()
                tilemap.set_cell(i, j, tiles.keys()[rnd])

    # if tile is 16x16 return 1,1, 32x16 return 1,2, etc:
    func region_to_tiles(tile_size):
            return Vector2(1 / tilemap.cell_size.x * tile_size.x, 1 / tilemap.cell_size.y * tile_size.y)

    func make_sector(resource):

    func make_chunk(size):


You can also create dictionaries or array of properties according to the name or id of the tile. I also saw that a script can be assigned directly to each tile, but I don't know how it is handled. There are several things to learn ...

1 Answer

+1 vote

Aside from the map of tiles you've generated, you could generate additional layers of maps over it, each containing different data for each tile. In this case, you'd generate a map containing the resources for the tiles.

by (190 points)

Ok. If I can add additional layer of generated map and add constants and variables to it why can't I add consatnts and variables to the first and existing generated 2D tilemap and every single tile? Why I need another layer? Wouldn't it be the same map twice?

Maybe my question is wrong. I would like to know how to add constatnts and variables to every single tile from tilemap.

Tile01 = 1 stone
Tile02 = 2 wood
Tile03 = 3 gold
TileN = n resources

Can I do this using Visual Scripting ar I need do this with GdScript?

Only now I've realized you're using Godot's TileMap. I don't know if it's possible to associate data with TileMap.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.