AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   [HL2DM] Sprite re-parenting offset issue (https://forums.alliedmods.net/showthread.php?t=334653)

AdRiAnIlloO 10-10-2021 19:12

[HL2DM] Sprite re-parenting offset issue
 
1 Attachment(s)
Hi people,

I have an overall working feature which puts sprites above a single client's head (among all connected players). To save performance / be efficient, when a different client is designated to have the sprite and it was already created, I simply teleport and re-parent it to the new owner (so instead of e.g. killing & re-creating the sprite). For context, this is for a match server.

Sometimes, there is the issue where the re-parented sprite becomes shifted (horizontally only) from the relative head point where it should be (must be horizontally centered to the player model, and some units above head), possibly when the new player was moving and/or jumping right before the sprite is re-parented to them. This problematic offset is either randomly small and unimportant or too big and therefore annoying for the gameplay, as the sprite is visible to owner and others.

Here's the function that gets called for re-parenting the sprite:

Code:

void ResetKingSprite()
{
        Entity_ClearParent(gKingSprite); // Just a convenient wrapper from SMLib off the expected procedure
        float origin[3], angles[3] = {0.0, 0.0, 0.0};
        GetClientAbsOrigin(gTopScorer, origin); // gTopScorer = new owner client index
        origin[2] += 80.0;
        TeleportEntity(gKingSprite, origin, angles, NULL_VECTOR);
        Entity_SetParent(gKingSprite, gTopScorer); // Just a convenient wrapper from SMLib off the expected procedure
}

A picture might explain better all of this case, though I don't have any where the issue is shown, so I attach one illustrating the ideal sprite placement along some explanation of the issue, for you to get the idea.

Would there be a master way to ensure the sprite is always correctly placed without having any offset? If possible, I'd like to hear from anyone who might dealed with the same concept (not exclusively, ofc).

Any help appreciated. Thanks.

Bacardi 10-11-2021 16:42

Re: [HL2DM] Sprite re-parenting offset issue
 
You are doing all in same frame.
Try request next frame after teleport, then parent or something. My wild guess.

AdRiAnIlloO 10-11-2021 16:52

Re: [HL2DM] Sprite re-parenting offset issue
 
Quote:

Originally Posted by Bacardi (Post 2760337)
You are doing all in same frame.
Try request next frame after teleport, then parent or something. My wild guess.

Thanks!

Casually, I had thought the same and already had switched to that approach before posting the thread, but I wanted to expose the original intended code state.

Code:

void ResetKingSprite()
{
        Entity_ClearParent(gKingSprite);
        float origin[3], angles[3] = {0.0, 0.0, 0.0};
        GetClientAbsOrigin(gTopScorer, origin);
        origin[2] += KING_SPRITE_Z_OFFSET;
        TeleportEntity(gKingSprite, origin, angles, NULL_VECTOR);
        RequestFrame(OnKingSpriteTeleported);
        SetKingSpriteVisibility(true);
}

void OnKingSpriteTeleported()
{
        if (gKingSprite != INVALID_ENT_REFERENCE)
        {
                Entity_SetParent(gKingSprite, gTopScorer);
        }
}

I need to play enough more on my server to confirm how it goes out. The problem is, by the obvious nature of this approach (1 frame delay), the resulting effect might already imply there will still be an offset, variable with client's movement/velocity as well, but which might be smaller at least.

Bacardi 10-12-2021 03:34

Re: [HL2DM] Sprite re-parenting offset issue
 
I don't have idea, how accurate are coordinates with client and sprite in same code sequence...
And perhaps 1 frame delay could be too short to fix it, next you could try create timer with 0.1 seconds. Recommend in Valve Wiki dev site in some documents.

AdRiAnIlloO 10-12-2021 21:00

Re: [HL2DM] Sprite re-parenting offset issue
 
This approach seems to be working flawlessly for now:

Code:

void StartResetKingSprite()
{
        Entity_ClearParent(gKingSprite);
        RequestFrame(EndResetKingSprite);
}

void EndResetKingSprite()
{
        if (gKingSprite != INVALID_ENT_REFERENCE)
        {
                float origin[3], angles[3] = {0.0, 0.0, 0.0};
                GetClientAbsOrigin(gTopScorer, origin);
                origin[2] += KING_SPRITE_Z_OFFSET;
                TeleportEntity(gKingSprite, origin, angles, NULL_VECTOR);
                SetKingSpriteVisibility(true);
                Entity_SetParent(gKingSprite, gTopScorer);
        }
}

So instead of delaying the parenting, I delay everything but the parent clear (by one frame). But I'll inform if it gets affected eventually, too.


All times are GMT -4. The time now is 08:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.