Search Unity

Network/Server primer for the beginner

Discussion in 'Multiplayer' started by stockhouse50, Jul 26, 2017.

  1. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    Hi, I was wondering if someone could answer these basic questions that I am confused about. Thanks in advance.

    1) If running a host/client type game, does the local client/host see the changes when executed on the server since it's technically a server? I understand the remote clients don't but the local client confuses me.

    2) Are the methods and code inside a script attached to a non-player object implicitly executed on the server or do all the clients execute it and therefore see it?
     
  2. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    1. They are litteraly one thing. Host / Server share scene, and everything. It's not two things running on the host PC.
    2. Both, it's litterarly just like normal MonoBehaviour. You can run server code on them with if(isServer). Or client code. Or both.
     
  3. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    Hi, thanks for the response. I just want to make sure I understand fully.

    1) so if I were to use the line if(isServer), the local client who is the Host/Server see all the changes that are done on the server without having to send the data back to the clients?
    2) As for the code inside a script attached to a non-player, if code is executed in it (without if(isServer) ) then all the clients will see the changes without using clientrpc's or syncvars?
     
  4. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    1. Yes, like. The client IS the server. It's not two things. If you go to editor. Start a Host mode. Then edit SyncVars. They will be updated to other clients.
    2. Eh, yes. But not recomended, here is why:
    Player 1 starts host.
    Player 2 connects.
    In their start method their health value gets set to 100.
    Player 1 shoots player 2. Player 2 now has 50 health.
    Player 3 joins and sets everyones health to 50 100. Now, we're out of sync

    So basically, if you need a gameManager that runs server code to manage things. You can do that even without a NetworkBehaviour. But you can also do it with. And if you only have server code on there. You can mark the NetworkIdentity with ServerOnly and the object wont even be visible on the clients. Hope this cleared it up. If not, get back to me.
     
    Last edited: Jul 27, 2017
  5. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    2. Could you explain to me why when Player 3 joins, it will set everyone's health to 50?
     
  6. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    Sorry, typo. It's ment to say: "Player 3 sets everyones health to 100". His start method.
     
  7. stockhouse50

    stockhouse50

    Joined:
    Apr 9, 2015
    Posts:
    14
    Ok, I see what you mean. I guess I might have used the wrong terminology. When I say non-player object I mean an object that isn't controlled by a local client or any remote client (like a bot or flag). With that being said, I might re-ask you the question from the first post.

    2) Are the methods and code inside a script attached to a non-player object (like a bot or flag) implicitly executed on the server or do all the clients execute it and therefore see it?
     
  8. TwoTen

    TwoTen

    Joined:
    May 25, 2016
    Posts:
    1,168
    I will stick wtih my first answer. Everyone gets it. But it's not recomended for Syncing stuff. The health thing was just an example. But yes, it's like ANY MonoBehaviour.