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. Dismiss Notice

Why do singleplayer games use a server

Discussion in 'Scripting' started by MikeyJY, Jan 5, 2022.

  1. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I noticed that a lot of singleplayer games start a localhost server on the player's machine even if it is just him on the server. What's the purpose of this?
     
  2. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Which games are you talking about? Are these multiplayer games with offline mode?
     
    Joe-Censored likes this.
  3. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    If the game also has a multiplayer mode, I imagine that it would be to allow the single player instance to use mainly the same code as when running in multiplayer mode.

    Otherwise, I have no idea, I haven't ever encountered a single player only game that does such a thing.
     
    Bunny83 and Joe-Censored like this.
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Are you talking about multiplayer games when the user chooses to play single player, or games where there is no multiplayer option? I know many multiplayer games will still create a local server in the background for single player mode, and this is so the developers don't have to essentially design two different games for the different modes.
     
    Lethn, Yoreki and Munchy2007 like this.
  5. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    I was thinking about games that have both MultiPlayer and SinglePlayer. So the reason is to use the same code for Multiplayer and Singleplayer they simulate a server behavior in on the local machine?
     
    Joe-Censored likes this.
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Pretty much.

    Why rewrite all that logic when you could just create the server and connect to it the same way you would any server. Networking logic doesn't honestly care where the server is located (for the most part... there's of course latency concerns that need to be coded in, but mostly you just working in a situation where that latency is low).
     
    Joe-Censored, MikeyJY and Lethn like this.
  7. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Sometimes you can experience latencies. And, I used to play a game on Steam and a glitch was occurring often where if you placed a building under you, the client placed the building, however the server didn't approve the placing because the building occupied the same space with the player and you saw the building for a second, then disappeared and you lose the items required for the building.
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    1) I said "low" latency

    2) That's not what I mean by latency. That's just the logic between the server and client being out of sync in some manner. It could be brought on by any number of issues. Such a glitch could easily happen in a none-server setting depending on how it was developed.

    For example... what if your logic is "spawn prefab, use its collider on spawn to test if it clips anything, delete if it does".

    Bad logic... but would cause a similar issue.

    Anyways, have you seen some of the programmers around here?

    Have you seen my code?

    lol
     
  9. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Don't know what do you mean. I said I played a game that has this issue. Nothing related to this forum. Plus I have 0 experience with networking.
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I was making a joke that bad logic is created by programmers being lazy and referenced how in this scripting forum full of programmers who post lazy/bad code. And then directed that at myself in a self-disparaging way.
     
    MikeyJY likes this.
  11. MikeyJY

    MikeyJY

    Joined:
    Mar 2, 2018
    Posts:
    530
    Sorry, English is not my native language... So I can't get all jokes and expressions.
     
  12. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    That'd be the problem with the dev not the technique.

    It's common that games built with mp and sp, route sp through a (local) host. It doesn't go online unless it's bad code though.