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.

multiplayer vs. single player mode

Discussion in 'Netcode for GameObjects' started by weldonxu, Jul 26, 2022.

  1. weldonxu

    weldonxu

    Joined:
    Jul 10, 2022
    Posts:
    3
    I want to build a game support both multiplayer and single player. In multiplayer human vs. human, in single player, human vs. AI. If single player, of course, network is not needed.

    How would I design the project? can the netcode for multiplayer be re-used for single player AI, or do I have a write an almost complete independent set of code for AI?
     
  2. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    74
    I would suggest the Component based way. Try to make everything as independent as possible. The goal should be to attach and remove your Component without an Error in your other Components. Like that you can remove your Network Component if needed and remove it if not.

    For example: You have the Components - Movement, Input, NPCBehaviour - and you got an Player and an NPC

    So both, Player and NPC, need the ability to walk. So we attach Movement to both of them
    Only the Player needs Input, so we attach the Input. The Input can send commands to the Movement.
    The NPC needs the NPCBehaviour. The NPCBehaviour controls your NPC and sends move commands to Movement.

    You can see, the only difference between Player and NPC are the two scripts Input and NPCBehaviour. So you can say that, for example, Character is your base but Player and NPCs are both Character with different attachments.

    So you could also say, your Game is the base but Online and Offline are both the same Game with different attachments.

    I hope you got the idea, i would suggest reading about Design Patterns and you will find a lot of useful Patterns that will help you in the future.
     
unityunity