Nonexistent function in base Tilemap

Godot Version

Godot4

Question

Hey, I am pretty new to Godot and have been working with a digging system in Godot where the player can right-click on a tile in a tilemap to change the cell and spawn an item yet when I attempt to convert the mouse position when clicked to a tile position I get told that world_to_map is a non-existent function in my base Tilemap. Its a confusing error that I haven’t been able to find a solution for. Sorry if this is a bit informal as this is my first question post.

Here is all of my code so far:

extends CharacterBody2D

var speed = 75
var currentLocation = Vector2()

@export var stream: AudioStream

@onready var animated_sprite = $AnimatedSprite2D
@onready var player_canvas_layer = $JustPickedUp
@onready var inventory_ui = $InventoryUI

@onready var tilemap = $"../TileMap"

@onready var items = $"../Items"

var groundlayer = 0
var collisionlayer = 1

var tilepos = null

var can_dig_custom_data = "can_dig"

var isdigging = false
var ismoving = false

func _ready():
	global.set_player_reference(self)
	player_canvas_layer.visible = false

func get_input():
	var input_direction = Vector2()
	if true == true:
		if Input.is_action_pressed("ui_left"):
			input_direction.x -= 1
		if Input.is_action_pressed("ui_right"):
			input_direction.x += 1
		if Input.is_action_pressed("ui_up"):
			input_direction.y -= 1
		if Input.is_action_pressed("ui_down"):
			input_direction.y += 1
		velocity = input_direction.normalized() * speed
	else:
		pass
	if Input.is_action_just_pressed("dig"):
		isdigging = true
		
		var mousepos = get_global_mouse_position()
		
		tilepos = tilemap.world_to_map(mousepos)
		
		var source_id = 0
		
		var Atlas : Vector2i = Vector2i(6, 1)
		
		var tileData : TileData = tilemap.get_cell_tile_data(groundlayer, tilepos)
		
		if tileData:
			var can_dig = tileData.get_custom_data(can_dig_custom_data)
			if can_dig:
				spawn_item(global.spawnableitems[randi() % global.spawnableitems.size()], mousepos)
				tilemap.set_cell(groundlayer, tilepos, source_id, Atlas)
				animated_sprite.play("Digging")
			else:
				print("can't dig here")
		else:
			print("no tile data")
	isdigging = false

func _physics_process(delta):
	get_input()
	move_and_slide()
	update_animations2()
	
func update_animations2():
	var lastanimation = ""
	var digging = false
	
	if velocity.length() > 0:  # Check if the character is moving
		if Input.is_action_pressed("ui_up"):
			if Input.is_action_pressed("ui_left"):
				animated_sprite.play("Up_Left")  # Play the diagonal up-left animation
				lastanimation = "Idle_Up_Left"
			elif Input.is_action_pressed("ui_right"):
				animated_sprite.play("Up_Right")  # Play the diagonal up-right animation
				lastanimation = "Idle_Up_Right"
		elif Input.is_action_pressed("ui_down"):
			if Input.is_action_pressed("ui_left"):
				animated_sprite.play("Down_Left")  # Play the diagonal down-left animation
				lastanimation = "Idle_Down_Left"
			elif Input.is_action_pressed("ui_right"):
				animated_sprite.play("Down_Right")  # Play the diagonal down-right animation
				lastanimation = "Idle_Down_Right"
	else:
		animated_sprite.stop()
		animated_sprite.play(lastanimation)  # Stop the animation if the character is not moving


func _input(event):
	if event.is_action_pressed("ui_inventory"):
		inventory_ui.visible = !inventory_ui.visible
		AudioManager.play_sound(stream)

func spawn_item(data, pos):
	var item_scene = load("res://Scenes/Inventory_Item.tscn")
	var item_instance = item_scene.instance()
	item_instance.initiate_items(data["item_type"], data["item_name"], data["item_rarity"], data["item_worth"], data["item_texture"])
	item_instance.global_position = pos
	items.add_child(item_instance)

There is no world_to_map function, you need to use tilemap.local_to_map(tilemap.to_local(mousepos)), the method was removed in 4.x, see here

Thank you so much this was it, I keep forgetting all of the things that changed with the jump from 3-4. Sorry if this wasted your time and your help was sincerely appreciated. Have a great rest of your day :slight_smile:

1 Like