How to overlay a sprite through a function and check which sprite is under it

Godot4.2.1, Gdscript, 2d

I want to make a city-planning simulator, but I don’t know how to put a sprite on another sprite through code and check whether it can be put there.

You can do this with layers or node2d like you have 2 node2d, interior and exterior, so add there sprites

how to do this through code, function? I don’t understand

Do you have created the nodes? And the sprite scenes you want to add? Do you want that if you press a sprite, then another sprite will spawn?

I want that when selecting a special GUI menu, you can select a specific building and place it. And it was possible to place buildings only on grass (there is also water in the world). I also wanted this to work through a tile map

Ok if you had setup the tilemap and the building UI, add a preview sprite of each buildings and do like this:

Button script:

extends Button
signal activated(_building_name)

@export var building_name: String = ""

func _on_pressed(): #connect this
    activated.emit(building_name)

Main script:

extends Node2D

var building_name = ""

func _on_button_activated(_building_name): #connect here all buttons 
   building_name = _building_name

func _physical_process(_delta):
    if building_name == "": return
    var building = get_node(building_name)
    building.position = get_global_mouse_position()
    #Rest of the codes, do that if left mouse is clicked, add your building in that preview sprite pos

you don’t understand, I want that when you press the left mouse button, for example, a building will spawn in a cell of the tile map, but if in the place where you left-clicked there is water, the building will not spawn

You can check out my tutorial as it showed how to create a infinite procedural world generation with objects and biomes + block placements that you want

1 Like

Does it show how to make it so that something cannot be spawned on the water?

Thanks for the tutorial, I think it will help me a lot

1 Like

Does tilemap have a function that simply draws a tile through code?

Yes, you can find in my tutorial

there is a small problem. I am not a native English speaker and use a translator. I am also new to Godot. It’s quite difficult for me to parse your code and find that very function

Can you please name it specifically?

Do you taking about this?

set_cell ( int layer, Vector2i coords, int source_id=-1, Vector2i atlas_coords=Vector2i(-1, -1), int alternative_tile=0 )
1 Like

Yes, thank you

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.