Jump to content

.sleepstatepending gets cleared too late, making it unreliable to check for it in some cases


hoxi
  • Pending
function OnEntitySleep(guid)
    local inst = Ents[guid]
    if inst then
        inst.sleepstatepending = nil -- it should be here!

        inst:PushEvent("entitysleep")

        if inst.OnEntitySleep then
            inst:OnEntitySleep()
        end

        inst:_DisableBrain_Internal()

        if inst.sg then
            SGManager:Hibernate(inst.sg)
        end

        --inst.sleepstatepending = nil -- not here!

        if inst.emitter then
            EmitterManager:Hibernate(inst.emitter)
        end

        for k,v in pairs(inst.components) do
            if v.OnEntitySleep then
                v:OnEntitySleep()
            end
        end
    end
end

Same applies to OnEntityWake.

 

The way it's done right now results in OnEntitySleep and OnEntityWake functions on an inst, as well as "entitysleep" and "entitywake" events, to be unreliable to properly check for it. This can mainly be an issue with making common functions or common functionality that might check for that boolean.

 

On a related note..

function EntityScript:IsAsleep()
    return not self.entity:IsAwake()
end

Is there a reason this function doesn't check for .sleepstatepending?

If something checks for entity sleep, then you don't want it to go through during entity construction either (where entity:IsAwake() returns true as a false positive, but the entity is basically sleep and at 0, 0, 0), and there's plenty of cases like that do it anyway.

In some cases, what is done because of it gets immediately undone right after on receiving sleep status, so they just waste performance, in some other cases, worse, it doesn't get undone and can result in unintended behavior.

 

You can try logging if inst:IsAsleep() is called when .sleepstatepending is still true, and you'll see what I mean.

Keep in mind that there are some weird edge cases where entity:IsAwake() is checked instead of inst:IsAsleep(). Those should be updated to use the latter instead.


Steps to Reproduce

Self-explanatory.

  • Like 1



User Feedback


There are no comments to display.



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

×
  • Create New...