Showcasing a touch of Lua

Hello.
I seem to be jumping a little around the Source engine (Portal 2, Gmod Lua). After alot of searching, research, and studying, I came up with a little piece of code that, for some strange reason, I feel proud of. :slight_smile:

You can watch the results at http://www.edwardleuf.org/Source/gmod.01.mpg
or execute the following code at gm_contruct:



-- A small experiment that just grew.
-- I think this is as good as it gets.
-- What to try out for Experiment 2?
--
-- By Edward

function HoldCenterBone()
	-- To capture a specific Bone...
	local curphys = ent:GetPhysicsObjectNum(10)
	local bn = ent:TranslatePhysBoneToBone(10)
	-- Freeze specific Bone
	curphys:EnableMotion(false)
	curphys:Wake()

	local x = curphys:GetAngles()

	ent:SetFlexWeight(27,0)
	ent:SetFlexWeight(28,0)
	ent:SetFlexWeight(42,0.5)

	ent:ManipulateBoneScale(6,Vector(2,2,2))

	timer.Create("hold_pos",0.1,0,function()
		if(!IsValid(ent)) then
			timer.Stop("hold_pos")
			return
		end

		if(!IsValid(cam)) then
			player.GetByID(1):SetViewEntity(player.GetByID(1))
		end

		x.yaw=x.yaw+10
		if(x.yaw>360) then x.yaw=0 end
		curphys:SetAngles(x)

		-- Flex Weight sets FacePoser
		ent:SetFlexWeight(39,math.Rand(0.0,1.0))

		-- Old works. Sags the physics motion.
--		ent:SetRagdollPos(10,Vector(0,0,-100))
--		ent:RagdollUpdatePhysics(0)
--		ent:RagdollSolve()

	end)
end

function DelayedJumps()
	timer.Create("send_velocity",0.48,10,function()
		local velocity = Vector(0,0,1)
		velocity = velocity * 25000
		velocity = velocity + ( VectorRand() * 1000 )
		phys:ApplyForceCenter( velocity )
	end)
end
-- Make ragdoll
ent = ents.Create("prop_ragdoll")
if(!IsValid(ent)) then
	return
end
ent:SetModel("models/Humans/Group02/male_09.mdl")
--ent:SetPos(player.GetByID(1):EyePos())
ent:SetPos(Vector(0,0,-145))
--ent:SetAngles(player.GetByID(1):EyeAngles())
ent:SetAngles(Angle(0,0,0))
ent:Spawn()
cleanup.Add( player.GetByID(1), "props", ent )
	undo.Create( "prop" )
	undo.AddEntity( ent )
	undo.SetPlayer( player.GetByID(1) )
undo.Finish()
-- End Make ragdoll

-- Make Camera
cam = ents.Create("prop_dynamic")
if(!IsValid(cam)) then
	return
end
cam:SetModel("models/dav0r/camera.mdl")
cam:SetPos(Vector(0,-128,-100))
local entpos = ent:GetPos()
local campos = cam:GetPos()
-- Vector:Sub returned nil, doing this the "manual" way
local relpos = Vector(entpos.x-campos.x,entpos.y-campos.y,entpos.z-campos.z)
cam:SetAngles(relpos:Angle())
cam:Spawn()
cleanup.Add( player.GetByID(1), "props", cam )
	undo.Create( "prop" )
	undo.AddEntity( cam )
	undo.SetPlayer( player.GetByID(1) )
undo.Finish()
-- End make camera

--for i = 0,ent:GetBoneCount(),1 do
--	print(i..": "..ent:GetBoneName(i))
--end

phys = ent:GetPhysicsObject()
print(phys)
if(!IsValid(phys)) then
	ent:Remove()
	return
end

--for i = 0,ent:GetPhysicsObjectCount()-1 do
--	local curphys = ent:GetPhysicsObjectNum(i)
--	local bn = ent:TranslatePhysBoneToBone(i)
--	print(i..": "..ent:GetBoneName(bn))
--end

--for i = 0,ent:GetFlexNum()-1 do
--	print(i..": "..ent:GetFlexName(i))
--end

player.GetByID(1):SetViewEntity(cam)

HoldCenterBone()
--DelayedJumps()


/Edward
Ps. Who knew there’d be 3 extra functions to one action… :stuck_out_tongue:

The .mp4 isn’t displaying video.

Now that’s a shame… It was encoded as a MPEG-4 Visual Simple@L1 written with Lavc55.18.102.
Well, if you have Gmod, make a lua file in the lua folder and write in the console lua_openscript scriptname.lua

[editline]25th February 2015[/editline]

You know, scratch that. Changed format to a very basic MPEG.

what the hell

Not to worry, I’m getting better. :slight_smile:

You can use math.NormalizeAngle ( angle is -180 to 180 so you can simply use a % to achieve same goal )…

Example rotate function:


//
// Handles repetitive smooth rotational calculations into 1 line without the confusion and normalizes the angle ( -180 )
//
function math.rotate( _speed, _offset )
	return ( ( RealTime( ) * ( _speed || 180 ) + ( _offset || 0 ) ) % 360 ) - 180;
end
	

_speed is how fast it should rotate in revolutions per second, _offset should be used if you have multiple objects that rotate to prevent them from all being in sync unless you want that ( use math.random 0, 360 in ENT:Init so it’s only set once…

I’d recommend creating helper-functions for anything that ends up being used more than once or to simplify code-flow ( where it makes sense to do so ).

Also, you don’t need to use _ang.yaw, you can use _ang.y … I’m pretty sure any character after the first is chopped and not used so personal preference.

Towards the end the code becomes more tightly knit with not a lot of breathing room. Again, personal preferences.

I would, however, recommend that any code executed for dev purposes be put into a function, or console command. It makes things slightly easier to manage so they don’t load during auto-refresh, etc…

i’m more curious on what the hell kind of website this is
http://www.edwardleuf.org

A throwback to 1999

I know, I’m old fashioned… :wink:
Though, you should’ve seen my first attempt of plain HTML some 15 years ago. :stuck_out_tongue:

Btw, I am slowly getting the hang of functions. Last game scripting I did was a form of C where the only functions like this were sending messages between the scripts, and calling gotos.