Feedback on my NPC system and how to handle animation

Greetings if you know me I did post a few days ago on the topic of sending massive amount of NPC data over a unreliable event.
The solution: Bit packing + buffers :smiley:

Ultimately I shrinked it down to 5 bytes per NPC which is massive considering 2 NPC’s before took over 517 bytes!!!

The NPCs:


Working well so far :D!
Ignore the NPC’s looking up into the sky I am currently debugging the issue with the CFrame calculation but nevertheless the NPC’s are working well and accurate towards the server’s simulation.

Now for my question.

How should I handle animations?
First, basic movement like walking is already inbuilt to the NPC when the rig is cloned to the client! However things like weapon animation and such can be hard to sync with delay (server tick is 0.3 seconds) as the server handles the damage counter while the visuals are instantly perceived by the client which can look awkward if the animation is outta place.

Secondly, how should I handle sending what animation to play to tell the client to replicate this onto their cloned rig without sacrificing network bandwidth?

Any suggestions/insight is appreciated.

2 Likes

I’m assuming by this you mean if I hit an NPC the health won’t update until the next server tick? If that’s the case apply the health visual change instantly on client. The way I do this for my game is giving each health change an identifier. Say you do a swing and deal 10 damage to the NPC, assign that “10 damage” an ID and store it in a table. When you send the damage event to the server send 10 damage and the ID. Now apply the local health changes until they are resolved. Say on the next tick the server still hasn’t applied the health change. If the server sends the NPC is at 100 health locally the client will subtract 10 as there is a stored damage identifier corresponding to -10 health that hasn’t been resolved yet. Say on the next tick the server applies the damage. Now the server sends 90 as the health and in a different field sends the ID that corresponds to the local damage value in the client table. Now the client sees the ID, removes the 10 damage from the table, now there is 90 health - 0 unresolved damage so the NPC health is 90.

This system is a bit overhaul and only really worth it if you plan on having a lot of damage per second on the NPC and also need the health to not jump back up for a tick or two if the player is on high ping. If you are okay with the health locally showing higher for a small period of time in edge cases just apply the health change locally then on next server tick set it to be whatever the server sends. Given your ticks are 0.3 seconds you’d need a ping of >300 to be consistently experiencing the health jumping back up.

You have to sacrifice network bandwidth here but you can do it efficiently. I’d recommend making your NPC logic a state machine. State machines are an entirely different topic, look them up if you aren’t familiar but it effectively means your NPC behavior can be broken down into states such as “running”, “walking”, “idle”, “attacking”, etc. and all you have to do is pass a number that corresponds to the NPCs state. For my game I never will need >16 states so I only need 4 bits. If the client receives 1 it runs the walking anim, 2 it runs running, 3 idle, etc.

3 Likes

I see so 1 byte can prob store 16 states (corresponding to 4 bits) conversely 8 bits can store 256 states.

In my game each weapon has a unique animation (holding, firing/swinging, reloading) which can be complicated to only have 256 states as later in the future that may impose problems and specificalities. One thing I can do is bit packing the tool serial number eg. 1 = pistol, 2 = sword. for the first 6 binaries leading to 64 weapons possible with 4 different states as the leftover bits is 2 (2 to power of 2 = 4); so, 4 states per weapon which seems enough as ranged weapons will be split by holding, firing, and reloading, while melees will have holding + 2-3 unique swing animations.

Thank you for this insight it is much appreciated.

You explained state machines very well to me, thank you! (I’ve only up to 2 days ago gotten into the art of bitpacking, binary, and network bandwidth since I’ve never really simulated server side stuff onto the client of this degree) :sob:

As for the health updating part, I will see what I can do, your suggestion seems very promising.

1 Like

Ah I’ve never ran into this issue because my system sends what type of NPC the NPC is on spawn so the client inherently knows what animations to apply. Each of my NPCs can have a max of 16 states but I can have infinite NPC models.

If you are wanting to allow the NPC to swap weapons try sending a single remote event that isn’t a part of your tick data on weapon swap. You could probably pack all the weapon types into a single byte (allows for 256 different weapons) or two bytes (65,535 weapon possibilities) and it doesn’t really matter because you are sending them so infrequently. The packet sent on NPC spawn in my game is equivalent to roughly 8x the size of a tick packet it just doesn’t matter because it’s only sent once

1 Like

Interesting. The main thing that the client does for my NPC system is replicating visually what the server is simulating of the NPC behavior.

Essentially the client does the visual effects of bullets, NPC movement, Orientations.
Of course the server is simulating orientation and movements but isn’t inherently replicating to clients as one of the main reasons for lag/network issues is the expensive natures of humanoid replication.

In doing so I just strip the extra information that usually is replicated to the client in two formats:
A calculated angle [ranges: -180 to 180] – of course the value that’s sent ranges -90 to 90 which is a compressed version of orientation (loses some degree of accuracy, but maintains network bandwidth and retains the visual appeal) this is used to adjust orientation and CFrames using only one singular value (costing only a mere 1 byte!!!).

And the second value which is 4 bytes (32-bit) is an unsigned value containing the X, Y, Z coordinates. The Y values is extremely compressed as the npc will rarely climb elevations to like 500 meters, but will laterally travel much farther distances so X, Z value is less compressed. (ALL these values are rounded which loses some precision, but nothing too significant as its only like .5 meter difference typically).

However your framework may seem as the more optimal choice. One proposed thing I can do is send a tick packet by using an invoke function.
Framework:

→ Client spawns in (player)

→ server sends out new tick updating the client to spawn a new NPC

→ client then requests server for the packet for a serial number (using invoke function)

→ the server recognizes the serial number which can be any number (0, 1, 7823) as it has its own dictionary corresponding to each NPC

→ tick packet is sent and updates NPC’s animations it needs from replicated storage

I can also use the visual system to detect when the NPC is firing based on if the visual effects is invoked which can solve animations not matching visuals.

I’d love to hear your thoughts on this.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.