#Godot - Spawn an item above a tile

1 messages · Page 1 of 1 (latest)

tender crater
#

Godot v4.4.1

Question
I am making a survival game platformer style, and I wanted ores to spawn randomly above specific dirt, moss and sand tiles. They will spawn at different probabilities. I need them to check if there is no tile above that tile, and then instantiate to spawn an ore there. I am a bit confused on how to do this though, this is my progress so far:

extends Node

@export var terrain_layer := 0
@export var spawn_parent: NodePath
@export var total_ores = 30

@onready var tilemap = $“../Terrain/TileMapLayer”

const valid_tile_ids = [0, 1, 3]

var ore_scenes := {
“granite” : preload(“res://Scenes/Ores/granite.tscn”),
“obsidian” : preload(“res://Scenes/Ores/obsidian.tscn”),
“quartz” : preload(“res://Scenes/Ores/quartz.tscn”),
“amethyst” : preload(“res://Scenes/Ores/amethyst.tscn”),
“ruby” : preload(“res://Scenes/Ores/ruby.tscn”)
}

var spawn_weights := {
“granite” : 0.52,
“obsidian”: 0.3,
“quartz” : 0.12,
“amethyst” : 0.05,
“ruby” : 0.01
}

long horizon
#

you can use tilemap.get_used_cells() to loop through all painter cells

#

what I recommend is adding a custom data to the tiles you want the ores to spawn

#

then you can do something like

for cell in tilemap.get_used_cells():
    var data = tilemap.get_cell_tile_data(cell)
    if data.has_custom_data():
        var custom_data = data.get_custom_data("YOU_CUSTOM_DATA_LAYER")
        ...

the res will depend on how you set the custom data