Need help learning entity animation

I’m trying to teach myself how to animate models in game, and it’s not going well. I’ve been fooling around with http://wiki.garrysmod.com/page/NextBot/StartActivity and http://wiki.garrysmod.com/page/Entity/SetSequence, but it doesn’t work reliably.

I’ve tried a little googling for guides or tutorials but most of the results are all about either animating models in an editor or the Garry’s Mod LUA Animation Editor. I’m more after learning how to make them play the animations they’ve already got, like the ceiling turret coming down, or a combine playing it’s death animation.

Does anyone know any good tips or beginners guides for animating entities using, or some examples I can look at?

Ok, I’ve kept on googling. If anyone gets as lost as I am, here’s what I’ve found to work.



ENT.AutomaticFrameAdvance = true -- Usually true by default, but just to be sure

if SERVER then
	-- Code to make the entity animate
	-- Returns the time that the animation will have finished
	-- Server Side
	
	function ENT:DoMyAnimationThing( SequenceName, PlaybackRate )
		PlaybackRate = PlaybackRate or 1 -- Use the default of 1 if no playback rate is provided
		local sequenceID, sequenceDuration = self:LookupSequence( SequenceName )
		if (sequenceID != -1) then -- a sequence ID of -1 after a lookup means it couldn't find a sequence with that name
			
			self:ResetSequence(sequenceID) --Sets the animation sequence
			self:SetPlaybackRate(PlaybackRate) --Sets how fast it animates
			
			--Might be needed, but didn't make any difference that I saw
			--self:ResetSequenceInfo()
			--self:SetCycle(0)
			
			-- Return the time at which the animation will finish
			-- Useful for adding loging to make the entity wait until the animation is done before it does anything else
			-- The current time + (how long the sequence is / how fast it's playing)
			return CurTime() + sequenceDuration * (1 / PlaybackRate) 
		else
			--Didn't find a sequence by that name, just return the current time
			MsgN("ERROR: Didn't find a sequence by the name of ", SequenceName)
			return CurTime()
		end
	end
end

-- This next part is really important, stops the animation from being jerky or choppy
function ENT:Think()
	
	-- Put your other think stuff in here. Know that it'll be run REALLY quickly and very often
	
	if SERVER then
		self:NextThink(CurTime()) --Make this entity 'think' as quickly as possible
	end
end


Another really good example can be found in

Entity:ResetSequence

From was I can tell;

A sequence is a single animation.
A gesture is a special partial animation that somehow gets blended into the current animation (e.g. a waving right arm that doesn’t interrupt the animation of legs running)
An activity I’m still a bit fuzzy on, but it only looks to be applicable to NPC’s and NextBots, and has something to do with telling AIs what sequence to run. (e.g. ACT_IDLE means keep playing the idle sequence in a loop)

[editline]14th September 2017[/editline]

Sorry, I messed up that Think function, and it wouldn’t let me edit my post.

It must have the “return true” in it, otherwise the self:NextThink() doesn’t work.



-- This next part is really important, stops the animation from being jerky or choppy
function ENT:Think()

	-- Put your other think stuff in here. Know that it'll be run REALLY quickly and very often
	
	if SERVER then
		self:NextThink(CurTime()) --Make this entity 'think' as quickly as possible
		return true
	end
end


Activities are mapped to a sequence or multiple sequences. Ideally, you should always use an activity over a sequence so that the model can handle its progression/transition/random sequence selection independently, but there are cases where you need to set the sequence manually. If you can read C++, you can kind of see how the engine translates activities into sequences for weapons here: https://github.com/LestaD/SourceEngine2007/blob/master/se2007/game/shared/basecombatweapon_shared.cpp#L2119