At what circumstance constraint.Weld will return false?
5 replies, posted
I have two pieces and I want to weld them by constraint.Weld. And after first welding, I wish I could change their relative position. Therefore, I want to remove the first welding constraint, move one part to new position, and build welding constraint again. Here is my code
[lua]
self.constraint1=constraint.Weld(ent1,ent2,0,0,0,true);
Msg(self.constraint1)
self.constraint1:Remove()
self.constraint1=nil
MovePosition(ent1,ent2)-- position move function
self.constraint1=constraint.Weld(ent1,ent2,0,0,0,true);
Msg(self.constraint1)
[/lua]
The first time the Msg shows: [0][phys_constraint]
But the second time it shows: false
I am wondering at what circumstance constraint.Weld (or other constraint function) will return false?
Thanks a lot!
It will return false if;
* The two props are already welded
* Either of the two entities are false or nil
* Either of the two entities are world entities or invalid
* Either of the two entities bones are invalid
* You attempt to weld an entity to itself or a bone to itself
[editline]26th March 2013[/editline]
[lua]
--[[----------------------------------------------------------------------
Weld( ... )
Creates a solid weld constraint
------------------------------------------------------------------------]]
function Weld( Ent1, Ent2, Bone1, Bone2, forcelimit, nocollide, deleteonbreak )
if ( !CanConstrain( Ent1, Bone1 ) ) then return false end
if ( !CanConstrain( Ent2, Bone2 ) ) then return false end
if ( Find( Ent1, Ent2, "Weld", Bone1, Bone2 ) ) then
-- A weld already exists between these two physics objects.
-- There's totally no point in re-creating it. It doesn't make
-- the weld any stronger - that's just an urban ledgend.
return false
end
local Phys1 = Ent1:GetPhysicsObjectNum( Bone1 )
local Phys2 = Ent2:GetPhysicsObjectNum( Bone2 )
if (Ent1 == Ent2 && Bone1 == Bone2) then return false end
onStartConstraint( Ent1, Ent2 )
-- Create the constraint
local Constraint = ents.Create( "phys_constraint" )
if ( forcelimit) then Constraint:SetKeyValue( "forcelimit", forcelimit ) end
if ( nocollide ) then Constraint:SetKeyValue( "spawnflags", 1 ) end
Constraint:SetPhysConstraintObjects( Phys2, Phys1 )
Constraint:Spawn()
Constraint:Activate()
onFinishConstraint( Ent1, Ent2 )
AddConstraintTable( Ent1, Constraint, Ent2 )
-- Optionally delete Ent1 when the weld is broken
-- This is to fix bug #310
if ( deleteonbreak ) then
Ent2:DeleteOnRemove( Ent1 )
end
-- Make a constraints table
local ctable =
{
Type = "Weld",
Ent1 = Ent1, Ent2 = Ent2,
Bone1 = Bone1, Bone2 = Bone2,
forcelimit = forcelimit,
nocollide = nocollide,
deleteonbreak = deleteonbreak
}
Constraint:SetTable( ctable )
Phys1:Wake()
Phys2:Wake()
return Constraint
end
[/lua]
If you're sure everything is valid, you could try making the second weld on a 0.1 second simple timer.
[editline]26th March 2013[/editline]
Either that or your MovePosition function is somehow removing one of the entities
Thanks! It seems that [lua]constraint1:Remove()[/lua] will remove the constraint itself but will not remove the entity table's record of such constraint.
The piece of code I try
[lua]
for k,v in pairs(ent1:GetTable().Constraints)do
Msg("\ent1 has constraints "..tostring(v));
end
[/lua]
indicates that the welding constraint is still there. However, how can I delete it especially I have one entity welded with two different entities (i.e. I have two [0][phys_constraint] on the entity's table and I only want to remove one of them.
Is there any webpage or wiki introduce the so called "entity's table" which can be derived from: ent1:GetTable()
It's just the table of that entity, in other words it just shows you the functions and variables associated with the entity.
e.g.
When you call Entity:SetPos() you are accessing the function SetPos() belonging to the table Entity.
[QUOTE=jaooe;40046267]It's just the table of that entity, in other words it just shows you the functions and variables associated with the entity.
e.g.
When you call Entity:SetPos() you are accessing the function SetPos() belonging to the table Entity.[/QUOTE]
Thanks a lot!
I have remove the constraint record on both entities before the constraint itself is removed. Everything is fine now.
I made such as small code piece to solve.
[lua]
--Remove a specific constraint; constr should be in constraint type
constraint.RemoveOne=function(constr)
local Entity1=constr:GetTable().Ent1;
local Entity2=constr:GetTable().Ent2;
for k,v in pairs(Entity1.Constraints)do
if(v==constr)then
table.remove(Entity1.Constraints,k)
end
end
for k,v in pairs(Entity2.Constraints)do
if(v==constr)then
table.remove(Entity1.Constraints,k)
end
end
constr:Remove();
end
[/lua]
Sorry, you need to Log In to post a reply to this thread.