So I am making a quiz kind of game. I want the clients to spectate and NOT see what the host sees. The host for example should have a screen where they can type a question and a correct answer and also some wrong answers. After they are done, the clients that are spectating on another screen/canvas/scene should get the question and the answers and can then decide which answer they think is correct.
This would obviously not work if the clients see the same screen as the host and see what the host is typing in or even worse what answer the host defines as the correct answer. But after googling for a while I only found tutorials in which Host and Client(s) see the exact same things ingame. Probably because in most games you want them to be in the same world, seeing the same things. But I dont. And it is frustrating that it seems there is no way to do this…
I also tried some things inside Unity but it never worked like I wanted it to. Which is why Im here asking you kind souls to hopefully help me out on what I can try to achieve this.
This isn‘t too hard actually. First, consider that the UI should not be networked at all. Simply instantiate local prefabs for the UI of each client and a different for the host. Now when the host enters a question, you‘ll make a „send“ button that calls a ClientRpc with the question string as a parameter. You can use a non visible (empty) networked game object as the sender receiver. On the client side this object updates the client UI whenever a ClientRpc is received. It‘s as simple as that.
Ultimately all you would do is instantiate your object normally. Then do MatchCondition.AddToMatch(clientHost) and network spawn the object. Now only host can see the object. When you want to add rest of the players you can also add them to the match the same way. Players can be removed from matches as well.
The match condition isn’t too difficult to use but is the most flexible one so if you have any questions feel free to ask.
Writing your own condition is an easy option too and could be a lot easier to use. Let me know.
I actually went ahead and made a custom condition to show you what it would look like. There’s also a feature coming in the next release where you can toggle the state on conditions, which would allow you to deactive this condition for all clients to see the object.
I know you haven’t announced your networking solution yet but your question gave me some creative insight into a new feature and I wanted to share the results on how it could potentially help.
[CreateAssetMenu(menuName = "FishNet/Observers/Host Only Condition", fileName = "New Host Only Condition")]
public class HostOnlyCondition : ObserverCondition
{
public override bool ConditionMet(NetworkConnection connection, bool currentlyAdded, out bool notProcessed)
{
notProcessed = false;
/* Only return true if connection is the local client.
* This check only runs on the server, so if local client
* is true then they must also be the server (clientHost). */
return (base.NetworkObject.ClientManager.Connection == connection);
}
public override bool Timed()
{
return false;
}
public override ObserverCondition Clone()
{
HostOnlyCondition copy = ScriptableObject.CreateInstance<HostOnlyCondition>();
return copy;
}
}
To clarify, I am using Netcode because if I read correctly it is by Unity itself so probably has the best support and integration. But currently I have a lot of issues with it so I might try out Fish-Networking if it actually is that much easier. Also I think I abandon my project for now and create a new one just to learn how to properly use Multiplayer in Unity before I get back to the quiz game.
Again, thank you for your answers. I will try them out