Rick Posted February 15, 2014 Share Posted February 15, 2014 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 Create a new lua file and name it the name of your class. Copy and paste this code into it 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) Add whatever variables you want to obj in the Create() function 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 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 1 Quote Link to comment Share on other sites More sharing options...
Rick Posted February 15, 2014 Author Share Posted February 15, 2014 Added to wiki also: http://leadwerks.wikidot.com/wiki:snippets-lua Quote Link to comment Share on other sites More sharing options...
bansama Posted February 17, 2014 Share Posted February 17, 2014 Is the code from Step 2 missing or is it the code you provided later in the post? Also, while I understand it's more work, I think a working example of this may be more helpful to those of us who are learning. Quote Link to comment Share on other sites More sharing options...
Rick Posted February 17, 2014 Author Share Posted February 17, 2014 Sorry, step 2 means copy the first code block below. That's the class template. Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted February 17, 2014 Share Posted February 17, 2014 @Rick, I believe shadmar also posted something like this: http://leadwerks.wikidot.com/wiki:simple-lua-oop http://www.leadwerks.com/werkspace/topic/7695-really-simple-lua-oop-exercise/ I haven't checked it out but for consistency, it might be worth looking in to. Quote Link to comment Share on other sites More sharing options...
Rick Posted February 17, 2014 Author Share Posted February 17, 2014 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.