#Ghost Mode Switching script not working

1 messages · Page 1 of 1 (latest)

tacit escarp
#

so im following a tutorial thats mildly out of date and so have had to switch entities.foreach loops to systemapi.query loops. For some reason my code which to me seems like it should work, just isnt.

#
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial class ClientSwitchPredictionSystem : SystemBase
{
    private float predictionRadius = 10;
    private float predictionRadiusMargin = 5;
    private NativeList<Entity> _toPredicted;
    private NativeList<Entity> _toInterpolated;

    protected override void OnCreate()
    {
        RequireForUpdate<SoccerBall>();
        _toPredicted = new NativeList<Entity>(16, Allocator.Persistent);
        _toInterpolated = new NativeList<Entity>(16, Allocator.Persistent);
    }

    protected override void OnDestroy()
    {
        _toPredicted.Dispose();
        _toInterpolated.Dispose();
    }

    protected override void OnUpdate()
    {
        ref var ghostPredictionSwitchingQueues = ref SystemAPI.GetSingletonRW<GhostPredictionSwitchingQueues>().ValueRW;
        var toPredicted = _toPredicted;
        var toInterpolated = _toInterpolated;

        for (int i = 0; i < toPredicted.Length; ++i)
        {
            if (EntityManager.HasComponent<GhostInstance>(toPredicted[i]))
            {
#
ghostPredictionSwitchingQueues.ConvertToPredictedQueue.Enqueue(new ConvertPredictionEntry
                {
                    TargetEntity = toPredicted[i],
                    TransitionDurationSeconds = 1.0f
                });
            }
        }

        for (int i = 0; i < toInterpolated.Length; ++i)
        {
            if (EntityManager.HasComponent<GhostInstance>(toInterpolated[i]))
            {
                ghostPredictionSwitchingQueues.ConvertToInterpolatedQueue.Enqueue(new ConvertPredictionEntry
                {
                    TargetEntity = toInterpolated[i],
                    TransitionDurationSeconds = 1.0f
                });
            }
        }

        toPredicted.Clear();
        toInterpolated.Clear();

        if (!SystemAPI.TryGetSingletonEntity<PlayerData>(out var playerEntity) || !EntityManager.HasComponent<LocalTransform>(playerEntity))
        {
            return;
        }

        var toPredictedRadius = predictionRadius;
        var playerPosition = EntityManager.GetComponentData<LocalTransform>(playerEntity).Position;


        //Handling conversions to predicted
        foreach(var (transform, entity) in SystemAPI.Query<RefRO<LocalTransform>>().WithNone<PredictedGhost>().WithAll<SoccerBallData>().WithEntityAccess())
        {
            if (math.distancesq(playerPosition, transform.ValueRO.Position) < toPredictedRadius)
            {
                //Convert to predicted
                toPredicted.Add(entity);
            }
        }

        var toInterpolatedRadius = predictionRadius + predictionRadiusMargin;
#
        //Handling conversions to interpolated
        foreach(var (transform, entity) in SystemAPI.Query<RefRO<LocalTransform>>().WithAll<PredictedGhost>().WithAll<SoccerBallData>().WithEntityAccess())
        {
            if (math.distancesq(playerPosition, transform.ValueRO.Position) > toInterpolatedRadius)
            {
                //Convert to predicted
                toInterpolated.Add(entity);
            }
        }
    }
}
#

to clarify, the entity in question does have the soccerballdata tag

clear shore
#

@tacit escarp I see that you have TransitionDurationSeconds = 1.0f, so the transition will take 1 second. Since you only have component checks in your code, you are creating mode change requests every frame, possibly resetting the internal timer before the system can switch modes and change the ghost's components and archetype. I think if you set TransitionDurationSeconds to 0, everything should work as the mode change will occur instantly.

mellow marsh
#

Is there any particular reason you are saving the entities in a native list instead of enqueuing them in the distance check?

tacit escarp
#

that

#

is a fair point

#

ive been following a tutorial but had to rejig it cos it used entities.foreach