How do you make tiles interactive in Godot?

why dont you just add a scene with area2d to the interactable tile that detects if player in there, then it can be interacted?
or simply just make a scene with sprite2d that is interactable ?

the midiphony idea should work, it basically mean that when you pressed the respond key, it will get the current tile the player is at and get the tile behind it. If the tile is interactable, then something will happen

problem is i dont know why i see you put the “idea” at process frame, because there no need to do checking tile and get tile custom data on everywhere and at every frame

The scene idea actually doesn’t sound bad, I just thought that maybe there is a more efficient way of doing it. By having this done, it seems like it backfired.

The problem with the idea from midiphony is that after trying it in different settings, either I don’t get any output or the game crashes after input.
I might also be stupid and not understand the approach, which is more likely.

I initially put it into the process method as I need to check the input for it, completely forgetting that I could add another method (with the tile checks) that is being done wity every input, my bad.

Thank you for your message. Maybe what I was trying to do is just not efficient to do, compared to your mentioned ideas.

the BasicHallway is a level container? i no idea why it’s area2d, but i guess it works too
if i understand correctly from midiphony, the “idea” code should be from player script, not the main script of BasicHallway.

Oh, that makes more sense, I will try that out.

First of all, you should use the returned value of tile_data.get_custom_data("is_interactive"), otherwise it doesn’t do anything. You have to check if the data returned by this function is true or false.

I’m not sure why you are trying to compare the player’s global_position to the tile_data ?? Anyway, the debugger is trying to help you : you are trying to compare an Object with a Vector2 : this doesn’t make sense.

Here’s a potential solution :

func get_tile_coords_from_global_position(some_global_position : Vector2) -> Vector2i:
	return tilemap.local_to_map(tilemap.to_local(some_global_position))

func _process(_delta):	
	if Input.is_action_just_pressed("ui_accept"):
		# Converting the player's global position to its coordinates on the tile map
		var player_tile_coords = get_tile_coords_from_global_position(player.global_position)
		
		# Getting the tile data at the player tile coordinates
		var tile_data = tilemap.get_cell_tile_data(0, player_tile_coords)
		
		# If the tile is marked "is_interactive" with true...
		if tile_data.get_custom_data("is_interactive"):
			# ... then do whatever here
			print("WORKS : is interactive")
		else:
			# otherwise do nothing or whatever
			print("Not interactive")


@ zdrmlpzdrmlp is right, there’s no need to check for the tile data every frame : you only need to do it when the user tries to interact.

Take the time to understand what the function does, and what is returned by those functions :sweat_smile:
You can easily see the classes and methods documentation by Ctrl+clicking them, or by searching them with F1

Hello,
after looking into the code for quite some time, I think i now understand it. Thank you so much for the help, it really is appreciated.
Anyway, have a nice day =)

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