Facepunch / garrysmod-requests

Feature requests for Garry's Mod
86 stars 24 forks source link

Allow constraint.Keepupright to work on custom entities. #2303

Closed Grocel closed 9 months ago

Grocel commented 9 months ago

About

Please allow constraint.Keepupright to work on custom entities. At the moment the function is hard coded to only work on prop_physics or on prop_ragdoll entities. In a local test I did (bypassed the check), I have seen that the KeepUpright constraint works just fine on custom scripted entities.

Wiki for context

https://wiki.facepunch.com/gmod/constraint.Keepupright

Code for context

Affected code: lua/includes/modules/constraint.lua:617 (dev branch)

--[[----------------------------------------------------------------------
    Keepupright( ... )
    Creates a KeepUpright constraint
------------------------------------------------------------------------]]
function Keepupright( Ent, Ang, Bone, angularlimit )

    if ( !CanConstrain( Ent, Bone ) ) then return false end
    if ( Ent:GetClass() != "prop_physics" && Ent:GetClass() != "prop_ragdoll" ) then return false end
    if ( !angularlimit or angularlimit < 0 ) then return end

    local Phys = Ent:GetPhysicsObjectNum( Bone )

    -- Remove any KU's already on entity
    RemoveConstraints( Ent, "Keepupright" )

    onStartConstraint( Ent )

        local Constraint = ents.Create( "phys_keepupright" )
        ConstraintCreated( Constraint )
        Constraint:SetAngles( Ang )
        Constraint:SetKeyValue( "angularlimit", angularlimit )
        Constraint:SetPhysConstraintObjects( Phys, Phys )
        Constraint:Spawn()
        Constraint:Activate()

    onFinishConstraint( Ent )
    AddConstraintTable( Ent, Constraint )

    local ctable = {
        Type = "Keepupright",
        Ent1 = Ent,
        Ang = Ang,
        Bone = Bone,
        angularlimit = angularlimit
    }
    Constraint:SetTable( ctable )

    --
    -- This is a hack to keep the KeepUpright context menu in sync..
    --
    Ent:SetNWBool( "IsUpright", true )

    return Constraint

end

Requested change

Please remove the class check if possible.

Alternatively add an additional entity parameter such as entity.AllowKeepupright that will also pass that check. This would work as a static property for SENTs and also work as a dynamic property for every kind of entity. Implementation details are up to you, the property can be named differently if needed.

    -- ...
    if ( !CanConstrain( Ent, Bone ) ) then return false end
    if ( !Ent.AllowKeepupright && Ent:GetClass() != "prop_physics" && Ent:GetClass() != "prop_ragdoll" ) then return false end
    if ( !angularlimit or angularlimit < 0 ) then return end
    -- ...
robotboy655 commented 9 months ago

Done, while still disallowing players and the world entity just in case.