How does a Nail entity work , lua wise.

For instance I’d like to create an entity that would act something like glue , but without actually needed a toolgun. I tried looking at the nail entity from sandbox but that didn’t give me any clues , so does anybody know or have any idea how the nail entity work?

http://luabin.foszor.com/code/gamemodes/sandbox/entities/weapons/gmod_tool/stools/nail.lua
Source.

[lua]
TOOL.Category = “Constraints”
TOOL.Name = “#Nail
TOOL.Command = nil
TOOL.ConfigName = nil

TOOL.ClientConVar[“forcelimit”] = “0”

TOOL.RightClickAutomatic = true

cleanup.Register( “nails” )

function TOOL:LeftClick( trace )

    // Bail if we hit world or a player
    if (  !trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end
   
    // If there's no physics object then we can't constraint it!
    if ( SERVER && !util.IsValidPhysicsObject( trace.Entity, trace.PhysicsBone ) ) then return false end
   
    local tr = {}
            tr.start = trace.HitPos
            tr.endpos = trace.HitPos + (self:GetOwner():GetAimVector() * 16.0)
            tr.filter = { self:GetOwner(), trace.Entity }
    local trTwo = util.TraceLine( tr )
   
    if ( trTwo.Hit && !trTwo.Entity:IsPlayer() ) then

            // Get client's CVars
            local forcelimit = self:GetClientNumber( "forcelimit" )

            // Client can bail now
            if ( CLIENT ) then return true end

            local vOrigin = trace.HitPos - (self:GetOwner():GetAimVector() * 8.0)
            local vDirection = self:GetOwner():GetAimVector():Angle()

            vOrigin = trace.Entity:WorldToLocal( vOrigin )

            // Weld them!
            local constraint, nail = MakeNail( trace.Entity, trTwo.Entity, trace.PhysicsBone, trTwo.PhysicsBone, forcelimit, vOrigin, vDirection )
            if !constraint:IsValid() then return end

            undo.Create("Nail")
            undo.AddEntity( constraint )
            undo.AddEntity( nail )
            undo.SetPlayer( self:GetOwner() )
            undo.Finish()

            self:GetOwner():AddCleanup( "nails", constraint )              
            self:GetOwner():AddCleanup( "nails", nail )

            return true

    end

end

function MakeNail( Ent1, Ent2, Bone1, Bone2, forcelimit, Pos, Ang )

    local constraint = constraint.Weld( Ent1, Ent2, Bone1, Bone2, forcelimit, false )
   
    constraint.Type = "Nail"
    constraint.Pos = Pos
    constraint.Ang = Ang

    Pos = Ent1:LocalToWorld( Pos )

    local nail = ents.Create( "gmod_nail" )
            nail:SetPos( Pos )
            nail:SetAngles( Ang )
            nail:SetParentPhysNum( Bone1 )
            nail:SetParent( Ent1 )

    nail:Spawn()
    nail:Activate()

    constraint:DeleteOnRemove( nail )

    return constraint, nail

end

duplicator.RegisterConstraint( “Nail”, MakeNail, “Ent1”, “Ent2”, “Bone1”, “Bone2”, “forcelimit”, “Pos”, “Ang” )

function TOOL:RightClick( trace )

    self:GetWeapon():SetNextSecondaryFire( CurTime() + 0.2 )
    return self:LeftClick( trace )

end

function TOOL:Reload( trace )

    if (!trace.Entity:IsValid() || trace.Entity:IsPlayer() ) then return false end
    if ( CLIENT ) then return true end
   
    local  bool = constraint.RemoveConstraints( trace.Entity, "Nail" )
    return bool

end[/lua]

The nail tool is basicly just a weld, with an added nail detailentity. You see the magic happen in the “MakeNail” function, it’s where the two props gets welded, and the nail gets put inbetween them.
At line 21 it makes a trace between the first prop, and into the next prop, that’s where it declares if another prop is near enough to make a nail to it.

Could you clarify what you mean with “glue”?

The nail its self is just for visuals it works by just welding the props

I already realized most of this :stuck_out_tongue: , that’s what my second post was for. Thanks anyways for the will to help.