Custom Tasks

From GMod Wiki

Jump to: navigation, search
Lua: Custom Tasks
Page white text.png Description:Custom schedules
link=User:elspin Original Author:elspin
Calendar.png Created:12th August 2007

Contents

Opening Note

Most of the stuff here is going to be taken directly from garry's blog, the original page can be found here: http://www.garry.tv/?p=285


NEVER, EVER TRY TO CHANGE SCHEDULES IN A TASK WITH NPC:SetSchedule(), it will just royal fuck the task. Always use NPC:StartSchedule

How Tasks Work

As they're part of schedules, tasks define what the schedule actually does, for example:

 
local schdChase = ai_schedule.New( "AIFighter Chase" )
 
	schdChase:EngTask( "TASK_TARGET_PLAYER", 		0 )
	schdChase:EngTask( "TASK_FACE_TARGET", 			0 )
	schdChase:AddTask( "PlaySequence", 				{ Name = "deathpose_front", Speed = 0.5 } )
 

The first task is TASK_TARGET_PLAYER, what it does is find the closest player, simple enough. The next one, faces the target. The third task will play the death sequence, making this SNPC somewhat of an actor because schedules repeat themself, and it'll get back up. Anyways, schedules will use all the tasks then repeat themself. The second argument, "0" just means there is no argument. In TASK_WAIT you'd replace 0 with how long it waits for, it's task-specific.

So where does lua come in here?

In the above example, AddTask is not something that's already in the source engine like EngTask, it's a special task created with lua! So here's how you can make one.

 
function ENT:TaskStart_TASKNAME( data )
 
//this task is called when the task starts
 
end
 
 
function ENT:Task_TASKNAME( data ) 
 
        //this function is run as a think, until you run the below
 
	self:TaskComplete() //call to end the task
 
end
 
 

So there you have it, that's a custom lua task. You'd implement it into an addtask like this:

 
schdChase:AddTask( "TASKNAME",            0)
 


Example Custom lua task, by garry

This task is implemented into the above schedule we saw, it's name is PlaySequence.

 
/*---------------------------------------------------------
   Task: PlaySequence
---------------------------------------------------------*/
function ENT:TaskStart_PlaySequence( data )
 
	local SequenceID = data.ID
 
	if ( data.Name ) then SequenceID = self:LookupSequence( data.Name )	end
 
	self:ResetSequence( SequenceID )
	self:SetNPCState( NPC_STATE_SCRIPT )
 
	local Duration = self:SequenceDuration()
 
	if ( data.Speed && data.Speed > 0 ) then 
 
		SequenceID = self:SetPlaybackRate( data.Speed )
		Duration = Duration / data.Speed
 
	end
 
	self.TaskSequenceEnd = CurTime() + Duration
 
end
/*---------------------------------------------------------*/
function ENT:Task_PlaySequence( data )
 
	// Wait until sequence is finished
	if ( CurTime() < self.TaskSequenceEnd ) then return end
 
	self:TaskComplete()
	self:SetNPCState( NPC_STATE_NONE )
 
	// Clean up
	self.TaskSequenceEnd = nil
 
end
 
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox