Multiplayer NPC's. How do they function when a player isn't nearby?

Hello everyone,

I just want to find out some information in general on how multiplayer things work.

Now lets say its an open world game meaning the map will be quite big and there will be areas with no players in it at all.

*Does the server calculate movements for NPC's and other moving objects even when there are no players nearby?
*Does the server randomly place characters everytime a player gets nearby and only then calculate their movements until the player moves away far enough?
*Lets say you have a game with AI that progress. So when you first come to a area with lets say "cows" there is only 5 and next time you come back there must be 5 cows + 5 cow babies. How would the server calculate that?

Sorry if its confusing but I just want to know basically if a server just lets AI behave normally how they are scripted to 24/7 or does it spawn / despawn them with calculations based on how much time has passed?

Comments

  • edited
    I'm not a "real" programmer, so I could be pretty far off.

    But I'd expect that when no players are around, NPCs become dormant. When a player leaves the NPC's area, some variables are saved (if the number of cows is important, then it'd save that). When a player enters the NPC's area, the server could check when the last time was that a player left, see if that time was great enough for variables to have changed significantly, and update them (add more cows). I imagine servers (and developers) want to get the best result with the least work. (If the NPC behaviour was critical for some reason, then that'd change things though.)

    There was a racing game that was demoed at a meetup a few years ago, where the developer said that the AI was turned off the moment the cars were out of your camera view. The cars instead just snapped to a spline and followed the track with whatever speed they should, and when they came close enough to the player/player camera, they'd respawn and resume their AI. That way, the AI cars wouldn't get stuck on the track somewhere and be unable to recover (which is pretty hard to do); they'd always recover from their crashes and stuff the moment you couldn't see them anymore, and that improved the game. (It'd probably have made the game worse if the game encouraged you to turn around, drive backwards, and save the crashed cars, say. So it's pretty dependent on the game what you do.)
    Thanked by 2garethf Zaphire
  • edited
    Well what we have done is to completely prevent the AI logic from ticking over when there are no players in their zone. Based on the number of AI in a Zone (i.e. a mob), we will have a zone that covers the whole mob and as soon as a player enters the zone, the mob members will start checking if the player is in sight and start turning on their AI functions.

    If the mob member does not see the player it remains in a semi-dormant state that will keep checking for a player nearby.

    If the AI is not a mob and is just one AI, then it uses a zone that specifically covers it.

    The zones are linked to the mob members / individual members and move with them and the distance for the zone can be tweaked for performance as and where needed.

    This may not be the best way to do it, but it works for us and we have had good success with it. The problem you have is that if 30 players are on an area and they all activate their own mobs ... you will get a performance hit ... we are still working on this.
    Thanked by 1Zaphire
  • Thanks for the input guys :)

    What I have thought of is to have the following.
    AI Zones will have a trigger called IsPlayerInArea and once the first player enters the area it spawns the AI. If all the players leave the zone then the AI will despawn.

    If AI is allowed to breed etc then I will have a estimate time it takes for them to breed 1 extra. Then when a player enters the area again it checks the time last entered and the current time and calculates the amount of extra AI it should spawn. Then I save that variable and repeat the process everytime.

    Will test it and hopefully it will work effectively :)
    Thanked by 1garethf
  • I've tried your approach before (although not in a multiplayer game) and there was one issue I ran into. Watch out for the amount of gameobjects you need to spawn/despawn on trigger enter/exit. If the number is too high you could see a performance hit (or worse crashes). Maybe cap the amount of instantiates/destroys per zone (especially since you will increase over time). Just an idea though.
    Thanked by 1garethf
  • edited
    Hey Fanie,

    Ye I thought of limiting the amount of NPC's per area.
    Here is a very basic example of a Zone that I have in mind.
    Min NPC's = 2
    Max NPC's = 15
    NPC respawn time = 30min
    Last time entered = 2015-07-31 08:00AM
    New time entered = 2015-07-31 10:00AM
    Last number of NPC's = 4
    New number of NPC's = 8

    The spawning system will then make sure there is always 2 NPC's in an area and won't generate more than 15.
    I will then just spawn them randomly so that it looks like they have moved around in the area etc.
  • I encountered something like this when I was following Derek Yu's Spelunky:

    image

    The outline is the game screen, whereas everything in the big blue area is frozen.
    Depending on how your game is layed out, I'd imagine you'd have to take some kind of similar approach where items not in view/out of range are frozen.

    Source: spelunky.wikia.com/wiki/Frozen_Region
    Thanked by 1Zaphire
  • Thanks for that @duncanbellsa

    I think what I would have done different to that tho is unspawn certain objects that go into the frozen areas such as bullets.
  • edited
    Zaphire said:
    Hello everyone,

    I just want to find out some information in general on how multiplayer things work.

    Now lets say its an open world game meaning the map will be quite big and there will be areas with no players in it at all.

    *Does the server calculate movements for NPC's and other moving objects even when there are no players nearby?
    *Does the server randomly place characters everytime a player gets nearby and only then calculate their movements until the player moves away far enough?
    *Lets say you have a game with AI that progress. So when you first come to a area with lets say "cows" there is only 5 and next time you come back there must be 5 cows + 5 cow babies. How would the server calculate that?

    Sorry if its confusing but I just want to know basically if a server just lets AI behave normally how they are scripted to 24/7 or does it spawn / despawn them with calculations based on how much time has passed?
    I am not a great programmer (still at the beginners level), but I'm guessing it works in states?

    Let me try break it down like this.

    So you are in a 3D environment (X,Y,Z) and the AI is far away from you. So may there's a state where if the AI is a certain position (of X, Y and Z), it does not notice you and does a state where it patrols and does its routine.

    and say if you get into a position where it now detects you, it goes into a different state where it will become alerted and look at you.

    and say if you get closer, it will go into an attack state where it would well.. attack you lol.

    then from there it you could eliminate the AI which will change its state to dead or you could run away from its field of view (field of view would be its designed X, Y and z coordinates) and it will switch back to its paroling state?

    I could be completely wrong with this, but its just my idea of it lol.
  • Hey lloydizle,

    I understand those states but this isn't really about the AI's behavior but more on how to prevent the game from becoming really slow due to too many AI calculations happening at once.

    Its more like having World of Warcraft server with 1 player on it and 100 000 AI characters are moving around and patrolling but there is only 10-50 visible to the player. So what I am interested in is disabling the other 999 950 when they aren't visible to the player which will allow the server to perform much better and also cause less network traffic.
Sign In or Register to comment.