#Placing tiles in multiplayer

1 messages · Page 1 of 1 (latest)

rich arch
#

I've just added multiplayer to my game and blocks won't place anymore after trying to get them to show up for both players. The indentical code structure worked for erasing cells on both screens so I don't get what has happened.

In player's physics process:

 private void HandleMouseInput()
 {
     if (Input.IsActionPressed("mb_left") /*&& tileTimer.IsStopped()*/)
     {
         Vector2I tile = tileMap.LocalToMap(GetGlobalMousePosition());

         if (!CheckTile(tile))
             return;

         tileMap.Rpc("SetTile", tile, atlasCoords);
     }

     else if (Input.IsActionPressed("mb_right"))
     {
         Vector2I tile = tileMap.LocalToMap(GetGlobalMousePosition());
         var tileData = tileMap.GetCellTileData(0, tile);
         if (gameManager.Instance.TileMap.Buildables.ContainsKey(tile) || tileData != null)
         {
             bool isBedrock = false;

             if (tileData != null)
                 isBedrock = tileData.GetCustomData("isBedrock").As<bool>();

             if (!IsTileInRange(tile) || isBedrock)
                 return;

             tileMap.Rpc("CellEraser", 0, tile);
             blocks++;
         }
     }
 }```
#

In tilemap:

[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void SetTile(Vector2I coords, Vector2I atlasCoords)
{
    GD.Print("Method Found");
    SetCell(0, coords, -1, atlasCoords);
    GD.Print("Set cell");
}

[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void CellEraser(int layer, Vector2I cell)
{
    bool isBuildable = false;

    foreach (var scene in Buildables)
    {
        if (cell == scene.Key)
        {
            scene.Value.QueueFree();
            Buildables.Remove(scene.Key);
            isBuildable = true;
        }
    }

    foreach (List<Vector2I> blockList in gameManager.Instance.blockLists)
    {
        if (blockList.Contains(cell))
            blockList.Remove(cell);
    }

    if (!isBuildable)
    {
        if (GetCellAtlasCoords(layer, cell) == new Vector2I(-1, -1))
            return;

        var tileData = GetCellTileData(0, cell);
        var isBedrock = tileData.GetCustomData("isBedrock").As<bool>();
        if (isBedrock)
            return;
    }

    EraseCell(layer, cell);
}```
#

Even more annoying is that placing scene tiles works fine

#

in character, the class that player inherits from: ```cs
public void PlaceSpikes(Vector2 targetPos, string action = null)
{
if (this is not player || Input.IsActionPressed(action))
{
Rpc("Spikes", targetPos);
}
}

[Rpc(MultiplayerApi.RpcMode.AnyPeer, CallLocal = true)]
public void Spikes(Vector2 targetPos)
{
var tilemap = gameManager.Instance.TileMap;

var tile = tilemap.LocalToMap(targetPos);

if (!CheckTile(tile))
    return;

tilemap.BuildBuildable(0, tile, 1, this, 1);

}```

#

Janky I know but it works

#
public void BuildBuildable(int layer, Vector2I cell, int sourceID, character caster, int buildID)
 {
     string name = "n";

     switch (buildID)
     {
         case 1: name = "spike"; break;

         case 2: name = "bumper"; break;

         case 3: name = "turret"; break;
     }

     SetCell(layer, cell, sourceID, new Vector2I(0, 0), buildID);

     CallDeferred(nameof(List), cell, name, caster);
 }    ```
#

also janky but still, it works