How do I synchronize destructible tiles across clients.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Insignificant

I’m making a multiplayer tank game, where you are able to shoot projectiles at certain tiles, which results in the individual tile getting changed. It seems to work for my authority, but I can’t get it to synchronize on the clients. Tiles don’t get destroyed. My bullet is responsible for sending the “cell” value at the point of contact, which works. Here’s my code for the TileMap.

@export var tile : Vector2i = Vector2i.ZERO
@export var tile_id : int = 0
@export var destructing : bool = false

func destruct(cell):
	tile = local_to_map(cell)
	tile_id = get_cell_source_id(0, tile)
	if tile_id <= 2:
		destructing = true

func _process(delta):
	if destructing == true:
		set_cell(0, tile, tile_id - 1, get_cell_atlas_coords(0, tile))
		destructing = false

the variables (tile, tile_id, and destructing) are all assigned in my MultiplayerSynchronizer node, which is the only child node of my TileMap scene. Despite not knowing what it does, I’ve also added the script to the MultiplayerSynchronizer out of desperation. That doesn’t work either.

:bust_in_silhouette: Reply From: Insignificant

I figured it out.

@export var tile : Vector2i = Vector2i.ZERO
@export var tile_id : int = 0
@export var destructing : bool = false

func destruct(cell: Vector2):

if not multiplayer.is_server():
	return

tile = local_to_map(cell - global_position)
tile_id = get_cell_source_id(0, tile)
if tile_id <= 2:
	destructing = true

func _process(delta):
if destructing == true:
	set_cell(0, tile, tile_id - 1, get_cell_atlas_coords(0, tile))
	destructing = false

I added the if not multiplayer.is_server(): to make sure that only the server can change the variables. I’m still new to multiplayer, but I believe what is going on is the server is detecting when a tile should be destroyed, then it changes variables to tell the clients which tile to destroy for themselves.

I’ve removed the script from the MultiplayerSynchronizer’s replication, as for what I can tell, it did nothing.

Note: I also made the cell subtract the global_position to account for when the tile map is not at position (0, 0).