playMaker

Author Topic: PUN2 - Sync moving object between clients  (Read 6704 times)

iradit

  • Playmaker Newbie
  • *
  • Posts: 12
PUN2 - Sync moving object between clients
« on: April 24, 2021, 03:40:51 PM »
Hi

I am trying to create a multiplayer game and to keep it simple let's just say I am trying to have a player passing a ball to another one on click.

I have my players correctly synced but I am having hard time on the ball.
I must admit that I've been trying few things and ended up with it working on the Master client (passing the ball) but not on the other clients as they don't see the ball moving at all.

Probably something related to Photon view or Photon transform view components. That being said, while reading the forum, I noticed it could be also related to the way I create my ball. Indeed, it's not really clear to me whether I should:
- create a simple Ball object.
- have the Master client create the ball with Pun instantiate action
- have the Master client create the ball with Photon network instantiate Scene object
- Use RPC to make the ball move from a client to the other?

So yeah.. it's a bit messed up in my head and the more I try stuff the less it's getting clearer.

Any hint on the direction to follow?

Thanks in advance

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: PUN2 - Sync moving object between clients
« Reply #1 on: April 26, 2021, 01:55:42 AM »
Hi,


- create a simple Ball object. NOP :)

- have the Master client create the ball with Pun instantiate action

yes, that works, but only the master client can move the ball

- have the Master client create the ball with Photon network instantiate Scene object

that's even better

- Use RPC to make the ball move from a client to the other? NOP :) a photonView is what you need here.

note that you can only instantiate the ball if you are the master client, it's probably what happens, that you try to instantiate a scene object, but you are not eligible for this. only the master client is.

try to have the ball in the scene already, you can show it hide it, but at least you don't need to instantiate it anymore.

Let me know how it goes.

Bye,

Jean

iradit

  • Playmaker Newbie
  • *
  • Posts: 12
Re: PUN2 - Sync moving object between clients
« Reply #2 on: April 26, 2021, 03:50:29 PM »
Thanks for the reply, Jean.

Ball creation seems fine. I made the master client instantiate the ball at scene load as a scene object.
Then I randomly assign a first player to send the ball to (Itween move to).

But I still have the issue of the ball doing proper behavior on master client but not moving from spawn point on the other client.

I did add a photon view to my ball, observing its own fsm.

One thing to note is that, when running the game both on Unity and on a built version I have a console error on the built version (when unity is master) but not displayed in unity's console. And no error displayed when built version is master (however, in that case master ball behavior is not working).
Code: [Select]
NullReferenceException: Object reference not set to an instance of an object.

I will try some more things tomorrow as I am not sure now where I am not referencing an instance of an object properly (the ball, I expect).

Probably few rookie mistakes. I must admit Pun2 info are not the easiest to find (even more when you don't have a proper developer background)

Thx again for the help. I love browsing the forum  :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: PUN2 - Sync moving object between clients
« Reply #3 on: April 27, 2021, 01:44:34 AM »
Hi,

 Are you sure you have put your ball prefab in a Resources folder?

Bye,

 Jean

iradit

  • Playmaker Newbie
  • *
  • Posts: 12
Re: PUN2 - Sync moving object between clients
« Reply #4 on: April 27, 2021, 02:27:12 AM »
Hi,

Yes my ball is in Resources folder, so is my player.
(I actually removed that part from the last message as I felt going in too much details).
« Last Edit: April 27, 2021, 02:34:57 AM by iradit »

iradit

  • Playmaker Newbie
  • *
  • Posts: 12
Re: PUN2 - Sync moving object between clients
« Reply #5 on: April 27, 2021, 03:00:41 AM »
Also, reading again your first reply, I am confused:

So I did make the MasterClient to instantiate the ball through a scene_manager. Then the ball is sent and parented to a random player.
Then the ball run a Parent CheckIsMine and can be sent to another player if ParentIsmine.

But your comment also says "only the MasterClient will be able to instantiate that ball". Once instantiated can the ball be moved by any of the clients?

Thinking about it, I was starting to think of hiding the ball once reaching the other player and have that player instantiate (or show another hidden ball) and basically have a ball by player, but only one visible.... but that doesn't seem clean to me.

« Last Edit: April 27, 2021, 03:24:59 AM by iradit »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: PUN2 - Sync moving object between clients
« Reply #6 on: May 10, 2021, 07:02:04 AM »
Hi,

only the owner of the network game object can move that object  ( when IsMine is true), that applied when the masterClient is the owner.

for another player to move that ball, it has to gain ownership, or be made as the new masterClient to gain control.


your solution isn't ideal indeed.

The master client could watch the player that is supposed to move the ball and move the ball on his behalf. that's one way do solve this.

Bye,

 Jean

iradit

  • Playmaker Newbie
  • *
  • Posts: 12
Re: PUN2 - Sync moving object between clients
« Reply #7 on: June 05, 2021, 03:40:56 PM »
Hi

Thanks for the answer.

I probably saw it a while ago but right now it is not clear to me how I should implement my master client to watch the player that is suppose to move the ball.

Could you expand a little bit?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: PUN2 - Sync moving object between clients
« Reply #8 on: July 26, 2021, 02:23:52 AM »
Hi,

on the master client side, you can move the ball freely because it belongs to the scene, so the masterClient has full control over it.

Each player can send an RPC to the masterClient when they want to catch the ball,
 the masterClient receive that RPC and now can move the ball together with that client, unttil another client request the ball or when he will kick it ( again, via  rpc to the masterClient)

 does that make more sense?

Bye,

 Jean

iradit

  • Playmaker Newbie
  • *
  • Posts: 12
Re: PUN2 - Sync moving object between clients
« Reply #9 on: August 05, 2021, 01:48:44 PM »
Hi, thanks for the reply.

I think it does make it clearer. I'll try it out when I am back on the project.

Thanks