Ive been working with Netcode for GameObjects and generally Unity Multiplayer for the first time for a few months now.
I am atm “stuck” on a pattern, and Id love to hear from more experienced ppl than me.
So, I have a coroutine to generate the content of my game.
Inside that routine, I have other coroutines for specific tasks.
The first routine inside it is to simply fade the screen to black.
[SerializeField]
private FadeElement _fadeElement;
private IEnumerator IGenerateContent()
{
yield return _fadeElement.IFade(false);
}
Now, in order for all players to have their screens fade I came up with a bit of a workaround.
I dont know if this is a valid way of solving this, but it seems to work, so I liked it.
public IEnumerator IFade(bool fadeIn)
{
_isFading = true;
FadeClientRpc(fadeIn);
while (_isFading)
yield return null;
}
[ClientRpc]
private void FadeClientRpc(bool fadeIn)
{
StartCoroutine(IRealFade(fadeIn));
}
private IEnumerator IRealFade(bool fadeIn)
{
}
The idea/problem Im solving here is that coroutines cannot be ClientRpc’s, so I just “cheat” my way around it, by having a coroutine that just calls an RPC and then idles until the “real” coroutine is done and then yields back to the actual Coroutine that is generating the rest of the stuff.
Like I said, I tested this and it worked.
Because of that, I thought “this is a very repeatable pattern” so I tried to make a class for it.
(dno why the class itself doesnt show up in the code block stuff, sry for that)
public class NetworkCoroutineTool : NetworkBehaviour
{
public IEnumerator INetworkCoroutine(IEnumerator toExecute)
{
ExecuteClientRpc(toExecute);
yield return null;
while (toExecute.MoveNext())
yield return null;
}
[ClientRpc]
private void ExecuteClientRpc(IEnumerator toExecute)
{
CoroutineSource.Instance.StartCoroutine(toExecute);
}
}
But here is where I got stuck.
A NetworkBehaviour class cannot deal with the IEnumerator as a variable. The error I got from that is:
Unity.Netcode.Editor.CodeGen.NetworkBehaviourILPP: Assets\Scripts\OnlineFunctionality\NetworkCoroutineTool.cs(23,9): error - ExeClientRpc - Don’t know how to serialize System.Collections.IEnumerator. RPC parameter types must either implement INetworkSerializeByMemcpy or INetworkSerializable. If this type is external and you are sure its memory layout makes it serializable by memcpy, you can replace System.Collections.IEnumerator with ForceNetworkSerializeByMemcpy`1<System.Collections.IEnumerator>, or you can create extension methods for FastBufferReader.ReadValueSafe(this FastBufferReader, out System.Collections.IEnumerator) and FastBufferWriter.WriteValueSafe(this FastBufferWriter, in System.Collections.IEnumerator) to define serialization for this type.
Which I tried to find good examples to solve for, but I didnt find anything super obvious (I likely didnt do a good job).
But that is where my real question comes in.
Is this a legitimate pattern? Does something like this already exist?
Or am I entirely approaching this problem in a wrong way?
Any input is appreciated. Like I said, I am not very experienced with Multiplayer.