PGE NPC-AI Skeleton class
Jump to navigation
Jump to search
Each NPC Controller coded as class with events and functions. This is a skeleton class provides all possible functions and events which you can use with your NPC. Please don't forget to remove unused "OPTIONAL" events or functions. You always can save this skeleton to your disk and use it again.
-- REQUIRED: Let's define NPC-AI controller class
class 'SkeletonNPCAI'
-- OPTIONAL: you can define one or multiple shared variables (counters, shared objects, common constants, etc.)
-- which will be available for all copies of this NPC
local my_shared_variable = 0
-- RECOMMENDED: Custom function which sets initial properties. It's recommended to use
-- each time when your NPC going offscreen and when appears on screen again
function SkeletonNPCAI:initProps()
self.somevar = self.def_somevar
end
-- REQUIRED: __init(BaseNPC) event. Triggering once when NPC was been spawned. Sets defaults and initial properties
-- and settings of NPC.
-- 1) (BaseNPC)npc_obj argument - pointer to hardcoded NPC class which needed to control NPC
function SkeletonNPCAI:__init(npc_obj)
-- You must store this pointer into variable to control NPC at side
self.npc_obj = npc_obj
-- Optionally you can define one or multiple internal constants with internal values
self.def_somevar = 0
-- Let's initialize common preferences with calling this function
self:initProps()
end
-- REQUIRED: onActivated() event. Triggering on each appearing on screen if NPC was not activated before
function SkeletonNPCAI:onActivated()
-- Initialize properties of NPC
self:initProps()
end
-- OPTIONAL (if your NPC will don't use loops in algorithm, but if you wish use detectors,
-- you should use it to catch positive detector state and run required code for it):
-- onLoop(int) event calling each game logic iteration
-- 1) (int)tickTime argument contains time of current tick in milliseconds.
-- You can use it to manipulate with real time based features, animations, logic, etc.
function SkeletonNPCAI:onLoop(tickTime)
-- here you can write your code which will manupulate with your NPC
end
-- OPTIONAL: onHarm(int, int)
-- 1) (int)damage argument, it's a damage level given by player or by another NPC
-- 2) (int)damageReason argument gives reason code:
-- NPC_DAMAGE_NOREASON - this reason would be returned manually by lua code from another NPC
-- NPC_DAMAGE_STOMPED - if player stomped this NPC to head
-- NPC_DAMAGE_BY_KICK - if player kicked this NPC at side or at bottom kick under floor
-- NPC_DAMAGE_BY_PLAYER_ATTACK - if player given damage itself (by sword, by baseball bet, by fists, etc.)
-- NPC_DAMAGE_TAKEN - if this NPC is a coin, power-up, takable weapon, special bonus,
-- etc., this reason will be returned on contact with player
-- NPC_DAMAGE_CUSTOM_REASON - this reason would be returned manually by lua code from another NPC.
-- Also would be used NPC_DAMAGE_CUSTOM_REASON+1, NPC_DAMAGE_CUSTOM_REASON+2, etc.
-- reason codes dependent to various of weapons, attack types, dangers, etc. (for example, various magic spells)
function SkeletonNPCAI:onHarm(damage, damageReason)
-- you can do some code here to make answer on damage
-- Callback class coming soon to allow communication with a player or NPC who attacked this NPC
end
-- OPTIONAL: onKill(int)
-- 1) (int)damageReason argument gives reason code - same as for onHarm(int, int) event
function SkeletonNPCAI:onKill(reason)
-- Your code as answer to death of this NPC
end
-- OPTIONAL
-- 1) (long)NPC-ID argument gives target NPC-ID which will be after this loop instead of current NPC-ID
function SkeletonNPCAI:onTransform(targetID)
-- your reaction to transform of this NPC. For example, reduce counters of NPC's with this ID,
-- or set moving speeds to 0
end
-- REQUIRED: This string should be at end of your lua file
return SkeletonNPCAI