I have an gamemanager that has a script attached to it. This script fill 2 multidimensional arrays with classes. I need these classes to check for objects in specific positions, each position in the array is equivalent to a position in my game.
When Im filling these arrays I can do 2 ways.
Using send to everyone
[Rpc(SendTo.Everyone)]
public void CreateTilesServerRpc()
{
//Create the tiles for all the grid, and set the isUsable variable to be true or false
for (int i = 0; i < _size.x - 1; i++)
for (int j = 0; j < _size.y; j++)
{
Vector3Int _pos = new Vector3Int(i + origin.x, j + origin.y, origin.z);
_walls.gridArray[i, j] = new BackgroundTile(UsableTile(_pos), false, null, null, _pos.x, _pos.y);
_bombs.gridArray[i, j] = new BackgroundBomb(UsableTile(_pos), false, null, _pos.x, _pos.y);
DebugGrid.CreateWorldText(_isUsableParent, UsableTile(_pos) ? "1" : "0", _pos, 10, Color.white, TextAnchor.MiddleCenter);
//Debug.Log(i + "," + j);
}
}
or using send to server
[Rpc(SendTo.Server)]
public void CreateTilesServerRpc()
{
//Create the tiles for all the grid, and set the isUsable variable to be true or false
for (int i = 0; i < _size.x - 1; i++)
for (int j = 0; j < _size.y; j++)
{
Vector3Int _pos = new Vector3Int(i + origin.x, j + origin.y, origin.z);
_walls.gridArray[i, j] = new BackgroundTile(UsableTile(_pos), false, null, null, _pos.x, _pos.y);
_bombs.gridArray[i, j] = new BackgroundBomb(UsableTile(_pos), false, null, _pos.x, _pos.y);
DebugGrid.CreateWorldText(_isUsableParent, UsableTile(_pos) ? "1" : "0", _pos, 10, Color.white, TextAnchor.MiddleCenter);
//Debug.Log(i + "," + j);
}
}
The first one seems weird but at the same time it works. Sometimes the client need to check for these arrays, and it works fine.
The second one looks more correct though. It makes sense to only the server create this thing, but at the same time I always need to call serverRpcs to look at the information that It should exist to the client too.
if I do a debug.Log asking to the client to print the components to the array it says ânullâ to the server it returns âbackgroundBombâ, so only the server created and only the server have the information, how could I create something in the server but that shares to everyone?
Its weird because I think the client and the host should have access to the same GameManager, but if I say to the server to create an array only the server has access to this array. If I say to everyone to create an array looks like everyone has a reference to a different array. Idk maybe Im over complicating things.