'Is there any way to send gameobjects over the network? (Mirror)
I have a pathfinding script for my 2D multiplayer game (using Mirror) that returns available/walkable tiles in a list. The 'tiles' stored in this list are game objects which are generated over my grid in the scene. I have a function that makes a call to the server in order to move my character with a network transform.. That function checks the index of the list to see which tile to move to (notice path[0] referenced in this function. The index is a game object which is stored in my list of walkable tiles). If I try to use this function I'll get error "ArgumentOutOfRangeException" .. which I've gathered is because my 'tiles' are not actually being sent to the server.
My movement function:
[Command]
public void CmdMoveAlongPath()
{
var step = moveSpeed * Time.deltaTime;
var zIndex = path[0].transform.position.z;
character.transform.position = Vector2.MoveTowards(character.transform.position,
path[0].transform.position, step);
character.transform.position = new Vector3(character.transform.position.x, character.transform.position.y, zIndex);
if (Vector2.Distance(character.transform.position, path[0].transform.position) < 0.0001f)
{
PositionCharacterOnTile(path[0]);
path.RemoveAt(0);
}
}
I call this command from within my Movement script attached to my player.. For further context, startingTile and overlayTile variables shown here are the game objects that I pass to my pathfinding script which is shown here as pathFinder.FindPath(startingTile, overlayTile);
CmdMoveAlongPath is executed only when path.Count > 0
[ClientCallback]
private void LateUpdate()
{
if (!hasAuthority) { return; }
var focusedTileHit = GetFocusedOnTile();
var focusedOnTileFromPlayer = GetFocusedOnTileFromPlayer();
if (focusedOnTileFromPlayer.HasValue)
{
// Starting tile is the tile detected below player when the scene first loads.
startingTile = focusedOnTileFromPlayer.Value.collider.gameObject.GetComponent<OverlayTile>();
startingTile.GetComponent<OverlayTile>().ShowTile();
}
if (focusedTileHit.HasValue)
{
OverlayTile overlayTile = focusedTileHit.Value.collider.gameObject.GetComponent<OverlayTile>();
Vector2 touchPostion = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0) && overlayTile != null) //&& !treeDetected
{
if (activeTile == null)
{
path = pathFinder.FindPath(startingTile, overlayTile);
activeTile = startingTile;
}
else
{
path = pathFinder.FindPath(activeTile, overlayTile);
}
}
}
if (path.Count > 0)
{
CmdMoveAlongPath();
}
}
My Pathfinding script which calculates the tiles in between the starting tile and selected tile:
public class Pathfinder
{
public List<OverlayTile> FindPath(OverlayTile start, OverlayTile end)
{
List<OverlayTile> openList = new List<OverlayTile>();
List<OverlayTile> closedList = new List<OverlayTile>();
openList.Add(start);
while (openList.Count > 0)
{
OverlayTile currentOverlayTile = openList.OrderBy(x => x.F).First();
if (currentOverlayTile != null) {
openList.Remove(currentOverlayTile);
closedList.Add(currentOverlayTile);
}
if (currentOverlayTile == end)
{
return GetFinishedList(start, end);
}
var neighborTiles = GetNeighborTiles(currentOverlayTile);
foreach (var neighbor in neighborTiles)
{
if (neighbor.isBlocked || closedList.Contains(neighbor) || Mathf.Abs(currentOverlayTile.gridLocation.z - neighbor.gridLocation.z) > 1)
{
continue;
}
neighbor.G = GetManhattanDistance(start, neighbor);
neighbor.H = GetManhattanDistance(end, neighbor);
neighbor.previous = currentOverlayTile;
if (!openList.Contains(neighbor))
{
openList.Add(neighbor);
}
}
}
return new List<OverlayTile>();
}
My OverlayTile class .. I do have a gridLocation property on this class. Sending this to the server might be the way to go..
public class OverlayTile : MonoBehaviour
{
public int G;
public int H;
public int F { get { return G + H; } }
public bool isBlocked;
public OverlayTile previous;
public Vector3Int gridLocation;
}
From what I've read on other posts, it doesn't seem possible to send game objects like this over the network. But I also haven't found a solid answer on this anywhere yet. Seeing as how my pathfinder needs to check for game objects on the scene, I need to find a way to make this work. Should I just scrap this approach to my pathfinder using game objects?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
