Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to call methods in player from gameManager

Discussion in 'Multiplayer' started by Smikis, Apr 18, 2016.

  1. Smikis

    Smikis

    Joined:
    May 3, 2015
    Posts:
    7
    This question might sound bit daft, but its driving me insane. Whole unet system is so overcomplicated that its hard to wrap my head around it, everytime I feel like I understand how it works, it just wont work.

    I have GameManager class which is supposed to overseer the game, technically it cant modify other players, which is pain in the ass by design.
    So I moved methods that should be in controller to player class, and all I want to do is to trigger them, yet on remote client they wont, no matter how I try to call them.

    Unity asset store network lobby example has calls from manager to client for ui, but it doesnt modify any variables and rather still use gameManager static variables, is it actually possible to modify variables from one class to another in unet, either on each client and then sync them, or better, have host/server determinate values and pass them to clients.

    What I want is ability to pick random game mode on server/host, pass it to clients, after round ends, pick another, move players to start positions and so on. I've got all of that working on local client/host , but remote clients just doesnt receive that info.

    How can I approach this? Are there any examples that does something like this ?
     
  2. SaphiBlue

    SaphiBlue

    Joined:
    Apr 18, 2016
    Posts:
    43
    You can do something like:

    Have a Script In you player GameObject wich refers to you Server Code the Script (on the player Object) needs a RPC and a Command Method.

    The behavior can be like something:

    Player Calls CmdHelloServer("test") -> CmdHelloServer is running on the server and somewhere in the Server Code is RpcHelloPlayer("test return") called on the player script by the server.

    You Need:

    • PlayerObject
      • PlayerScript
        • Reference To ServerCode
        • Command Method
        • RPC Method
    • ServerObject
      • ServerCode
        • Reference To PlayerScripts (List or Array)
    Keep in Mind: a Player an only send commands from its own objects

    even better example: http://docs.unity3d.com/Manual/UNetActions.html
     
  3. Smikis

    Smikis

    Joined:
    May 3, 2015
    Posts:
    7
    sadly doesnt work and why would client (player) would first need to call server, so server would pass a variable to it? Server should know when to pass variable to players, anyway no mater how many different ways I attempt , remove client still never receives value passed to it, is that's even possible with the way unet is setup?
    That you cannot influence other player objects, but how on earth are you supposed to have global variables on all client ?
     
  4. SaphiBlue

    SaphiBlue

    Joined:
    Apr 18, 2016
    Posts:
    43
    you do not need to send a Command by the client first.
    the server can call an RPC at any time on client objects.

    This tutorials
    (have a look at the other videos form this guy) helped me a lot with Unet
     
  5. Smikis

    Smikis

    Joined:
    May 3, 2015
    Posts:
    7
    I've tried using RPC many times, but it never works for remote client, already seen and follower that tutorial and quite few others, none of them deal with global variables set by host/server :/
     
  6. moco2k

    moco2k

    Joined:
    Apr 29, 2015
    Posts:
    294
    So, you have a game manager. It needs to be a networked object with network identity and it needs to be present on both server and clients ("server only" flag must not be checked). On the server, you set your game mode, e.g. store it as an integer, and then sync this value to your clients (either with SyncVar or ClientRPC). At which point is the problem exactly? Maybe you should start with this basic functionality and post some code so we can see what's wrong.

    If you want to pass references of networked objects across server/clients you can always use NetworkeInstanceId to find the same networked object on the clients as on the server (have a look at ClientScene.FindLocalObject). So, for example, on the server you can retrieve the netId of the player object of interest, pass this id via RPC, and on the client side, you can retrieve the very same player object with this ID and then execute particular code.
     
    Last edited: Apr 19, 2016