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

Is this a bad way to handle game updates from a network

Discussion in 'Scripting' started by emobe_, Jul 9, 2022.

  1. emobe_

    emobe_

    Joined:
    Dec 1, 2021
    Posts:
    3
    I have a few ideas on how to handle actions passed from the server to a client and how to interact with them in the world but I am not sure what would be the best.

    I currently have a `NetworkManager` and a `GameManager`. Both classes have static instances and would interact like this example

                
    server.On("player_joined", (string payload) => {
    GameManager.spawnPlayer(payload);
    });


    My issue here, it's a bit hard to explain but I am worried this isn't abstract enough to handle lots of things happening other than just a player spawning and I am wondering should have a `GameState` instead which the `Game/WorldManager` listens to and the state emits events when changes happen.

    I've tried looking for different code examples but I can't find much.

    I appreciate it is pretty vague but it's one of those problems I am finding hard to articulate. I will gladly answer any questions and I'll update this post if I find a better way of explaining.
     
  2. bgulanowski

    bgulanowski

    Joined:
    Feb 9, 2018
    Posts:
    35
    Synchronization between different computers is usually labour-intensive. You will always have to define a lot of mappings between events coming from sources to updates on the destination. At the very minimum, you would have a table of events, and an automation script that generates functions to call on receipt. You could maybe automate writing some functions, too, such as those that create and delete objects, if they have similar structures, and even update functions, if they don't have custom side-effects. But writing tools that auto-generate code is non-trivial in itself.

    Other than that, you can break your code into layers, or find some other way to divide up responsibilities. But it is best to do that iteratively, in response to actual design challenges, instead of trying to predict what you will need, before you need it. Early on, it's enough to imagine the kinds of events that you will receive, the kinds of tasks that will have to happen in response to those events (verification, validation, processing, returning results, whatever), and then write code that is as consistent as possible. When you start to see too much duplication, or classes getting burdened with too much responsibility, pause and consider how to refactor, to make them more manageable.

    If you don't use revision control software (git), then do so, conscientiously, as it will give you confidence to make changes that you can revert if things go wrong.
     
  3. emobe_

    emobe_

    Joined:
    Dec 1, 2021
    Posts:
    3
    You're right. I imagine I am just overthinking and should just adjust the code as needed.