Facepunch / sbox-issues

175 stars 12 forks source link

`SceneTrace.Ray` + `Tags` filters don't work with Tags dynamically added #4172

Closed PolSpock closed 9 months ago

PolSpock commented 9 months ago

Describe the bug

Hello,

In a FPS context, i'm trying to shoot from my localplayer EyePosition to Forward with SceneTrace.Ray()

However with UseHitboxes(), i always get my localplayer Hitboxes even if i add some Tags conditions like below :

var trace = Scene.Trace.Ray( start, end )
    .UseHitboxes( true )
    .WithoutTags( "localplayer" );

Where localplayer tags have been added through code only on my localplayer my Player.cs with a beautiful dirty logic :

protected override void OnUpdate()
{
    if ( Owner.Network.IsOwner )
    {
        foreach ( var component in Owner.GameObject.Components.GetAll() )
        {
            component.GameObject.Tags.Add( "localplayer" );
        }
    }
}

So it looks like any Tags filters like WithoutTags / WithAnyTags etc don't work for Tags that have been added dynamically.

After investigation, here's reproduce steps:

To Reproduce

1) Open hitbox scene

2) Edit CameraPhysicsDebug.cs from https://github.com/Facepunch/sbox-scenestaging/blob/main/code/ExampleComponents/CameraPhysicsDebug.cs

Add any tags filters to :

if ( TraceType == TraceTypes.Ray )
{
    t = Scene.Trace
        .Ray( start, end )
        .UseHitboxes( Hitboxes )
        .WithoutTags("localplayer") // Add your favorite tags filter
        .Run();
}

3) Create a simple Component:

public class SimpleTagToComponent : Component
{
    protected override void OnStart()
    {
        GameObject.Tags.Add( "localplayer" );
    }
}

4) Add the created SimpleTagToComponent Component to one of Terry Body from the scene

5) Play: your Terry Body still gets Trace even if the localplayer tag filtering

Expected behavior

Trace filtering must work even if Tags are added lately

Media/Files

My reproduce steps in video:

https://github.com/sboxgame/issues/assets/5229571/45e2e69f-9fa0-4b3d-adad-02c822543556

Additional context

No response

garrynewman commented 9 months ago

This should work now

PolSpock commented 9 months ago

looks great, thx

https://github.com/Facepunch/sbox-issues/assets/5229571/c95c2184-ce4e-4c51-abf7-e7b0da40ec3c