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

Explain networking like I'm five

Discussion in 'Multiplayer' started by zluu, Oct 9, 2015.

  1. zluu

    zluu

    Joined:
    Aug 29, 2015
    Posts:
    2
    hello, I'm trying to get my head around the basics of network scripting but even after reading the docs, watching tutorials it's still very confusing... old system, new system, syncing, spawning, registering, commands... lots of concepts are being thrown around and I still don't really get them.

    let's take a very simple case, a top down card game:

    - to start with I want to spawn one card ( a scaled cube gameobject) at the bottom of the screen for player one, one card at the top of the screen for player two.
    that's the first problem, I know how to spawn player objects, but not how to spawn them at a coordinate I choose. and I don't know how to spawn several objects each belonging to each players.

    - player one clicks on the card, it moves to the center of the screen, player 2 does the same.
    I roughly know how to do that (onmousedown, islocalplayer)

    - card one attacks, takes off 3 HP from player 2, card two takes off 5 hp from player one.
    that's where I'm stuck, how do I store variables like "HP" or "attack", where do I store them (prefab?) and how do I access them.

    A full explanation would be great but I'd really appreciate even some pointers or well expained tutorials. I'm not concerned about how to do it visually, purely how stuff like spawning and storing/using variables work over the network.

    Thanks!
     
  2. Capn_Andy

    Capn_Andy

    Joined:
    Nov 20, 2013
    Posts:
    80
    I turned my response into a blog post, but I'll get a bit more specific:

    - For spawning cards in, you probably want to "Spawn with Client Authority". Check out the docs at the bottom of this page: http://docs.unity3d.com/Manual/UNetSpawning.html (was new in 5.2 I think). Spawning is pretty straightforward; here's some pseudocode with arguments not set right:

    Code (CSharp):
    1. var card = Instantiate(cardPrefab);
    2. card.transform.position = whateverPositionYouWant;
    3. NetworkServer.SpawnWithClientAuthority(card, cardOwner);
    4.  
    If you throw that into a loop you can spawn several cards at once there.

    - In each of the cards, make sure you have a NetworkTransform to track the card positions. When the client moves the card to the center of the screen, that should update correctly (good job checking isLocalPlayer, that's important).

    - You'll need to have a concept of a "player" gameobject. Visualize it as a gameObject or make it part of the HUD or just a silent invisible gameObject in the world. These "players" will have variables for Health, and [Cmd]s for Attack and the like. Put their health into SyncVars and they'll update. So for example,
    Code (CSharp):
    1. void OnMouseDown() {
    2.   MoveCardToMiddle();
    3.   CmdCardPlacedInMiddle();
    4. }
    5.  
    6. [Cmd] CmdCardPlacedInMiddle() {
    7.   // only run on server
    8.   if (numCardsInMiddle == 2) {
    9.     foreach(card in cardsInMiddle) {
    10.       foreach(player in bothPlayers) {
    11.         // player.health is a syncvar; updating it here on the server will auto propagate the value:
    12.         if (card.owner != player) player.health -= card.damage;
    13.       }
    14.     }
    15.   } // else other player hasn't put theirs in yet
    16. }
    Sorry it's untested pseudocode, but hopefully that gives you the gist of it.
     
  3. zluu

    zluu

    Joined:
    Aug 29, 2015
    Posts:
    2
    Thanks! that helps a lot, I ended up doing an "offline" version first with what I grasped of networking in mind (spawning prefabs etc) and convert to multiplayer later. Your answer definitely clarifies a lot of things, helps to have an example fitting with the project you're working on.