In my game every player can spawn a bomb, but they need to ask to the server, its like a bomberman game, where if you place a bomb on the ground you need to wait for it to explode so you can place another one, I’m doing this using an coroutine…
Checks for the input and the position, and ask to the server
private void Update
{
CanPlaceBombServerRpc(x, y, z);
....
}
[Rpc(SendTo.Server)]
private void CanPlaceBombServerRpc(int bombsRemaining, bool hasPressedAttack, Vector2 position)
{
(int x, int y) = ConvertPositionToGrid(position);
bool hasBomb = _manageDrops.CheckBombs(x, y);
if(bombsRemaining > 0 && hasPressedAttack && !hasBomb)
StartCoroutine(WaitBomb(position, RpcTarget.Single(OwnerClientId, RpcTargetUse.Temp)));
}
(Notice that in the CanPlaceBomb method I check for the number of bombs my client can use…)
And then the server spawn the bomb for the client, the bomb is being spawned and after a time it explode and destroy perfectly
private IEnumerator WaitBomb(Vector2 position, RpcParams rpcParams)
{
ulong clientId = rpcParams.Receive.SenderClientId;
var clientObj = NetworkManager.Singleton.ConnectedClients[clientId].PlayerObject;
PlayerBomb clientScript = clientObj.GetComponent<PlayerBomb>();
clientScript._bombsRemaining.Value--;
SpawnBombServerRpc(position);
yield return new WaitForSeconds(_bombFuseTime);
StartExplosionServerRpc(position, _explosionRadius);
DestroyBombServerRpc(position);
clientScript._bombsRemaining.Value++;
}
The problem is that I’m not being able to change the client _bombsremaining variable inside the coroutine, Im doing a debug.log and it seems that the clientId is always 0 for the client and host.
At the moment the client can spawn infinite bombs because its variable never changes
How can I change the client variable in the correct way?
Ah, I dont want to check every frame if I can spawn a bomb, is there any other way to not check every single time?