Creating a Ragdoll, with bones in a specific place.

UTT I know. I’m basically wanting a way in which I can spawn a ragdoll, with all the bones position and angles already set. A bit like the way the duplicator works when you copy a ragdoll it saves all the bone positions and you can paste it again just as it was.

I’m wanting to be able to do that, though I have been having trouble how I would surpass my problem in Getting the bone information and then setting it back into a new ragdoll.

At first I checked out the Entitys functions on the wiki, and came accross This along with This. I was thinking this is what I needed, though there’s no information what so ever on the wiki about how I should use them. Now i’m beginning to think I shouldn’t be doing it by this method, so I had a look at the duplicators code in sandbox, but that says nothing to me on how it is done unless my other eye is blind as well.

Has anyone had experience with using a Ragdoll and setting bone angles/positions of them within lua? I’d really like to know how it is done and if it’s possible, which I have great confidence in it is. It’s just a matter of how do I get that Data and manipulate another ragdoll with it.


Cakescript has a nice example.


I think it was the earlier versions though.

GetBoneMatrix and SetBoneMatrix are used for animation purposes and other stuff like that (inflator, finger poser), they don’t affect the physics at all.

This is what you need.

I have a basic working script now. It gets the position of all the bones of a ragdoll you look at and then you can apply them to another ragdoll. I’ll post the script here once i’m done testing with it for others to use as a reference or whatever.

Having some trouble with this code I need help with.


function InitializedEnt()
hook.Add( “InitPostEntity”, “initializing”, InitializedEnt )

function CreateDoll( Sd )

print( "Test" )
print( type( Sd ) )

Pose = ents.Create( "prop_ragdoll" )
Pose:SetModel( "models/alyx.mdl" )

print( Pose )

local BonesRead = file.Read( "bones" .. Sd .. ".txt" )
local Bones = glon.decode( BonesRead )

if ( Pose:IsValid() ) then

	for i=0, Pose:GetPhysicsObjectCount() -1 do
		local physBone = Pose:GetPhysicsObjectNum( i )
		if ( physBone && physBone:IsValid() ) then
			print( i, physBone )					
			physBone:SetPos( Bones*.Pos )
			physBone:SetAngle( Bones*.Angle )
			if ( Bones*.Frozen == true ) then 
				physBone:EnableMotion( false ) 


note: Running in init.lua within a gamemode

Nothing errors, it just doesn’t work how I expect it to. The problem is when running the script, the console prints out;

Entity [50][prop_ragdoll]
0	PhysObject
1	PhysObject
2	PhysObject
3	PhysObject
4	PhysObject
5	PhysObject
6	PhysObject
7	PhysObject
8	PhysObject
9	PhysObject
10	PhysObject
11	PhysObject
12	PhysObject
13	PhysObject
14	PhysObject

Here is Bones1.txt Put it in a pastebin so formatting and special characters aren’t lost.

Which is correct, this should happen. And what should have happened is the ragdoll should have spawned, in the positions I set it up to from the Bones table. I checked to make sure that the table is giving the information like the Position and Angle of each bone, and it does.

What is odd though, is if I run lua_run CreateDoll(1) The code works perfectly fine. It spawns in the right pose at the right position, opposed to it not spawning at all. If you comment out line 21 to 44, restart the gamemode to take in effect of the new changes applied, the Ragdoll get’s spawned at Vector 0,0,0 at Angle 0,0,0 Which is correct as that’s what should happen when you don’t give a ent positions, but it does spawn (this is without lua_run).

I just can’t see why lua_run would have it work fine, but calling it through the gamemode files, it doesn’t spawn at all unless line 21 to 44 are taken out. But those lines are needed to setup the ragdolls position.

Can you see why this is happening?

You’re lucky because i am working on something like that and i can give code to You.

local rag = ents.Create("prop_ragdoll")
local speed = ply:GetVelocity()

for i=1,rag:GetPhysicsObjectCount()-1 do
local bone = rag:GetPhysicsObjectNum(i)
local bonepos, boneang = ply:GetBonePosition(rag:TranslatePhysBoneToBone(i))

Check out Post 5 Silverman, Also you should start iterating from 0 instead of 1 at line 9 in your code.

I’m 98% sure that it’s because you’re using InitPostEntity. Try spawning it sometime after InitPostEntity because I’m afraid that something like glon or another part of the engine isn’t quite ready yet.

I actually just found out last night, that the whole problem was because within Fretta gamemode base, A function was being called that runs game.CleanUpMap.

What I also found it is that some one messed up the Fretta Gamemode hook page on the wiki who doesn’t have a clue how it works and added loads of false shit to it which he thought was correct. This was also a pain in the ass and didn’t help my issues at all. But thanks for all the help anyway.

Also Gbps, using it in InitPostEntity works fine. Thank you for the help. And everyone else.

The script in Post 5 works just as I planned.

Aha! That would have been important info pertaining to if your gamemode was Fretta or sandbox etc. :wink: