Jump to content

Lua table/class template


Rick
 Share

Recommended Posts

I'm adding this because as people newer start getting more into programming, they start to see that putting everything inside entity scripts can get complicated and hard to follow. You can end up having a ton of unrelated variables and even run into variable naming issues.

 

Below is a template that can be used to create "classes" in lua. Classes are a nice way to group an "object" or common functionality. Examples of what makes a good class would be:

  • UI controls like a button, or dropdown
  • A timer
  • Inventory

  1. Create a new lua file and name it the name of your class.
     
  2. Copy and paste this code into it
     
  3. Do a find/replace all on ClassName and change it to the name you want your class to be (generally the name of your lua file)
     
  4. Add whatever variables you want to obj in the Create() function
     
  5. Add whatever functions. Generally you'll have an Update() that you'll call yourself each frame from an entity script and PostRender() if you need 2D drawing
     
  6. In the entity script (or App.lua) import this script and use it

-- if this lua file gets included in multiple lua scripts this revents it from being recreated over and over again
if ClassName ~= nil then return end

-- define your class name
ClassName = {}

-- this is where you init things about the class
function ClassName:Create()
-- create a table that we'll return from this class so the user can work with it
local obj = {}

-- example of how to define class/table variables
obj.test = 5

-- this assigns all the functions defined below to this table object above
local k,v
for k,v in pairs(ClassName) do
	obj[k] = v
end

-- return the table so the player can call the functions and access variables
return obj
end

-- example of how to define a "class"/table function
function ClassName:Update()
end

-- another example of a table/class function
function ClassName:PostRender(context)
end

 

Usage example from an entity script:

 

-- be sure you have the right path to where you saved it
import "Scripts/MyClassScriptNameHere.lua"

function Script:Start()
-- create an instance of this class
self.test = MyClassNameHere:Create()

-- get a variable that was defined in this class
local what = self.test.classVariableName
end

function Script:UpdateWorld()
-- call a function that was defined in this class
self.test:Update()
end
  • Upvote 1
Link to comment
Share on other sites

Yeah, the hard part is what should we be consistent with? Josh does it (via AnimationManager) the way I have above and I was consistent with what he's doing. Shadmar's method works as well but it's not consistent with how Josh does it with the scripts that comes with LE. So I had to pick :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...