Menu
Lumberyard
Developer Guide (Version 1.11)

Creating a New Entity Class

The Entity system is currently on a path to deprecation in favor of the Lumberyard Component Entity System.

The following example creates an entity class called Fan.

  • Create a new entity definition file with the extension ".ent", for example "GameSDK\Entities\Fan.ent". This file will expose the entity to the engine.

    Copy
    <Entity Name="Fan" Script="Scripts/Entities/Fan.lua" />
  • Create a new Lua script file, for example GameSDK\Entities\Scripts\Fan.lua. The Lua file will define the entity logic.

    Copy
    Fan = { type = "Fan", -- can be useful for scripting -- instance member variables minrotspeed = 0, maxrotspeed = 1300, acceleration = 300, currrotspeed = 0, changespeed = 0, currangle = 0, -- following entries become automatically exposed to the editor and serialized (load/save) -- type is defined by the prefix (for more prefix types, search for s_paramTypes in /Editor/Objects/EntityScript.cpp) Properties = { bName = 0, -- boolean example, 0/1 fName = 1.2, -- float example soundName = "", -- sound example fileModelName = "Objects/box.cgf", -- file model }, -- optional editor information Editor = { Model = "Editor/Objects/Particles.cgf", -- optional 3d object that represents this object in editor Icon = "Clouds.bmp", -- optional 2d icon that represents this object in editor }, } -- optional. Called only once on loading a level. -- Consider calling self:OnReset(not System.IsEditor()); here function Fan:OnInit() self:SetName( "Fan" ); self:LoadObject( "Objects/Indoor/Fan.cgf", 0, 0 ); self:DrawObject( 0, 1 ); end -- OnReset() is usually called only from the Editor, so we also need OnInit() -- Note the parameter function Fan:OnReset(bGameStarts) end -- optional. To start having this callback called, activate the entity: -- self:Activate(1); -- Turn on OnUpdate() callback function Fan:OnUpdate(dt) if ( self.changespeed == 0 ) then self.currrotspeed = self.currrotspeed - System.GetFrameTime() * self.acceleration; if ( self.currrotspeed < self.minrotspeed ) then self.currrotspeed = self.minrotspeed; end else self.currrotspeed = self.currrotspeed + System.GetFrameTime() * self.acceleration; if ( self.currrotspeed > self.maxrotspeed ) then self.currrotspeed = self.maxrotspeed; end end self.currangle = self.currangle + System.GetFrameTime() * self.currrotspeed; local a = { x=0, y=0, z=-self.currangle }; self:SetAngles( a ); end -- optional serialization function Fan:OnSave(tbl) tbl.currangle = self.currangle; end -- optional serialization function Fan:OnLoad(tbl) self.currangle = tbl.currangle; end -- optional function Fan:OnSpawn() end -- optional function Fan:OnDestroy() end -- optional function Fan:OnShutDown() end -- optional function Fan:OnActivate() self.changespeed = 1 - self.changespeed; end