Help assigning different spawned players to different clients

Greetings to the team! Been playing around for the first time with Coherence, having made a local multiplayer game for 2 players on GGJ and now trying to convert it to online. I’ve been wondering if you could provide some help on the right logic to follow when trying to assign a different character to each client (ie. 1st person who connects instantiates and controls Player 1 prefab spawning on SpawnPoint1). I’ve created a Coherence Game Manager that is handling the Client Connections to communicate what will be happening inside the game to all players following that ExampleGameManager script from the documentation. With it I am running a check on the client counts, if it becomes 1 then I spawn Player 1, if it is 2 I spawn Player 2, which results in 4 characters being active on scene once both players are connected (2 being controlled by each one). Guess I am having some trouble on what I do need to think (and write) regarding ownership, cause I understand that I want to have the first player take ownership of that 1st prefab (which I am not currently doing by script, I just spawn a prefab that has a PlayerController and a CoherenceSync) and then the second owning the 2nd. Could you point to me where or what I should look for?

2 Likes

Hi @itsLevi0sa! Great to see you here! Let’s see if I can help shed some light on this. I recognise that the concept of authority can throw first-time users off the path, but once you grasp it, it makes a lot of sense.

As you experienced, with online games you have game logic happening multiple times on different machines (clients). So it’s not enough to just let each machine run the whole logic, or you get duplication of efforts.

You can do character spawning in a couple of ways. You can either:

  1. Elect one of the clients connected to be the lead, and take charge. They would orchestrate gameplay, spawn players, etc. while the other clients only get the result of it. This is also the case when in competitive games, all of the game logic is happening on the server (which in coherence lingo is what we call Simulator). Doing things this way probably requires you to then manually handle authority of each character to the respective client. A bit cumbersome!

  2. Or, for simplicity, let each player manage only their part. Each client spawns only their own character and then, by coherence magic, they get the other characters thanks to something we call network instantiation. When they do it this way, they automatically get authority on their character only.

The second method is what we use in our tutorial, First Steps, and it’s explained in the first scene. Please give it a read and let us know if it’s clear enough!

As a side note: for “game managers” stuff though, like keeping the score of the game, that are one for the whole game and not per-player, you have to do something like in the first case. We also have a small example of this in the last scene of the tutorial, where the flowers counter has to be unique for all players. Check that out later on.

Let me know if you have further doubts! :crossed_fingers:

1 Like

Thank you for the fast reply! I think my case is a little different than the one displayed on that First steps tutorial, which was the first thing I saw from Coherence and very helpful by the way (along with those GGJ Twitch streams)! It’s not that I have difficulty to have each client control and sync a single character. That was astonishingly easy to do in a couple of minutes thanks to all the material you provided (with animations and all!) :slight_smile: In my case though, I have 2 different player prefabs with different controls sitting in 2 neighboring gardens (like I wanted to have 2 different Player Handlers? :S ). So I’d like to have the first person who connects become Player 1 spawning on the Left Garden, and the next person become Player 2 on the Right Garden (I’ve set my Sample UI to be in Rooms btw) . How could I have a client know if they are the first ones to connect or not in order to spawn and control the analogous prefab on the correct location? (This is why I thought I needed a central manager to handle this)
|
|
Solution:
Turns out, taking from that PlayerHandler in the First Steps example, if I add an array of possible different prefabs to spawn (2 in my case) and have the network manager tell the client to spawn the prefab whose array number is corresponding to the current client count, it works!

Feedback: I think a future demo with more of a “Rooms” logic and a “Character selection” feature would be nice , if you are not already planning it :slight_smile: Everything looks so promising though, it’s very user friendly and I am having a great experience so far!

2 Likes

Hi @itsLevi0sa (not leviosA), so glad to see you figured out a solution :slight_smile: , looking forward to seeing your progress.

Since you have a good suggestion, maybe you want to wingardium over to Suggestions and post it there?

Have a lovely Wednesday!
And thanks @ciro for jumping in.

2 Likes

Ah sorry, I misunderstood! Good that you found a solution on your own!

Indeed, the idea of all players using the same prefab is only step one. But it makes a lot of sense to explore the case of different players entering the game with different needs, roles, and/or prefabs. Which will probably required a synced manager. I’ll keep it in mind for another tutorial!

1 Like

No worries, I can see it was a bit confusing. I wonder what a better title would be in case others find it confusing as well (should have asked ChatGPT beforehand :smiley: ). But you helped me figure it out with this

so thank you!

This forum also looks great by the way, I know its irrelevant but I like that “Solution+post” tag and the general “welcome” to it!

3 Likes

Appreciate the feedback; keep it coming! Especially now I think the tag situation is sorted :slight_smile:

1 Like

I’ve made this into a FAQ, here.

Hahahaha Liviosaaaaah