Basic Scripted NPC Broken?

I’ve tried 100 different ways to get scripted NPCs working, is it possible i’m missing something?

The best I can get is a NPC to face your direction.

I understand states, tasks, schedules I just can’t seem to get anything to work.

Anything you can help me with would be awesome.

… What exactly do you need help with?

Here’s my init.lua, this results in the NPC being spawned and constantly called schdChase but it never does anything in game.





AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

ENT.m_iClass = CLASS_COMBINE




function ENT:SpawnFunction( ply, tr )

	Msg("Entering Spawn
")
	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 20
	
	local ent = ents.Create( "npc_mercenary" )
	
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	
	Msg("Exiting Spawn
")
	
	return ent
	
end

function ENT:Initialize()

	Msg("Entering Init
")

	self:SetModel( "models/Combine_Soldier_PrisonGuard.mdl" )

	self:SetHullType( HULL_HUMAN )  
    self:SetHullSizeNormal()  
       
    self:SetSolid( SOLID_BBOX )    
    self:SetMoveType( MOVETYPE_STEP )   
       
    self:CapabilitiesAdd( CAP_MOVE_GROUND | CAP_OPEN_DOORS | CAP_ANIMATEDFACE | CAP_TURN_HEAD | CAP_USE_SHOT_REGULATOR | CAP_AIM_GUN )   
       
    self:SetMaxYawSpeed( 500 )   
       
    self:SetHealth( 100 )
	self:Give("weapon_smg1")
	
	Msg("Exiting Init
")

end

function ENT:OnTakeDamage( dmg )   
  
    local NewHealth = self:Health() - dmg:GetDamage()  
  
    if NewHealth <= 0 then  
        self:SetNPCState( NPC_STATE_DEAD ) -- We have to do this or else SCHED_DIE_RAGDOLL won't work  
        self:SetSchedule( SCHED_DIE_RAGDOLL )  
    end  
      
    self:SetHealth( NewHealth )  
      
end   

function ENT:SelectSchedule()
	local schdChase = ai_schedule.New( "AIFighter Chase" ) //creates the schedule used for this npc
  
	// Run away randomly (first objective in task)
	schdChase:EngTask( "TASK_GET_PATH_TO_RANDOM_NODE", 	128 )
	schdChase:EngTask( "TASK_RUN_PATH", 				0 )
	schdChase:EngTask( "TASK_WAIT_FOR_MOVEMENT", 	0 )
	schdChase:AddTask( "PlaySequence", 				{ Name = "cheer1", Speed = 1 } )
 
	// Find an enemy and run to it (second objectives in task)
	schdChase:AddTask( "FindEnemy", 		{ Class = "player", Radius = 2000 } )
	schdChase:EngTask( "TASK_GET_PATH_TO_RANGE_ENEMY_LKP_LOS", 	0 )
	schdChase:EngTask( "TASK_RUN_PATH", 				0 )
	schdChase:EngTask( "TASK_WAIT_FOR_MOVEMENT", 	0 )
 
	// Shoot it (third objective in task)
	schdChase:EngTask( "TASK_STOP_MOVING", 			0 )
	schdChase:EngTask( "TASK_FACE_ENEMY", 			0 )
	schdChase:EngTask( "TASK_ANNOUNCE_ATTACK", 		0 )
	schdChase:EngTask( "TASK_RANGE_ATTACK1", 		0 )
	schdChase:EngTask( "TASK_RELOAD", 				0 )
	//schedule is looped till you give it a different schedule
	
	-- Msg("Entering SelectSchedule()
")
	Msg("State:" .. self:GetNPCState() .. "
")
	
	if self:GetNPCState() == 0 then
		self:SetNPCState(1)
	end
	
	if self:GetNPCState() == 3 then
		Msg("Running schdChase
")
		self:StartSchedule(schdChase)
	end
	
	self:SetSchedule(SCHED_IDLE_WANDER)
	
	-- Msg("Exiting SelectSchedule()
")
	
end