Jump to content

Should we check if the entity in AddDataHandler is the local player's entity?


Recommended Posts

Posted

Hello!

I'm invoking an event on the server from the client when pressing C on the keyboard.
Then on the server I set a SharedData value for the player entity that invoked the event.
Then on the client side, I listen for the data change like this:

Events.AddDataHandler("isCrouched", HandleCrouch);

I noticed, however, that the the isCrouched data handler triggered on all of the clients, even those who didn't press C to crouch.
So I wondered whether it's necessary to check in the data handler whether the client is the entity the data is changed for.

private void HandleCrouch(Entity entity, object arg, object oldArg)
{
    if (entity.Type == Type.Player)
    {
        Player player = (Player)entity;
        
        // Check if player who this data has changed for is not the local player. 
        // If not the local player, return.
        if (player != Player.LocalPlayer) return;

        bool crouch = (bool)arg;

        if (crouch)
        {
            player.SetMovementClipset(movementClipSet, clipSetSwitchTime);
            player.SetStrafeClipset(strafeClipSet);
            return;
        }

        player.ResetMovementClipset(clipSetSwitchTime);
        player.ResetStrafeClipset();
    }
}

Crouching works whether I have that check in there, or not, and that's what confuses me.
This should set the clipsets for the entity on client side for all clients.
But it still sets the clipsets even if I make sure to run this code only on the local player.

I printed a chat message when the DataHandler is invoked, to make sure:

Local player's view:

n9wvPcP.png
Remote player's view:

spacer.png

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...