shadmar Posted October 1, 2013 Share Posted October 1, 2013 Ok, I'm mostly interested in doing lua stuff for the sake of simplicity, but I also want some degree of structure. So here is my little exercise in lua I want a team of solders fighting, so I create two "classes" Person.lua Team.lua Since lua uses tables for everything they aren't really classes in the tradional way, but simple OO is still possible without any thirdparty tools or libs using plain vanilla lua : Person.lua - Here I add some stats and actions. --Register class module("Person", package.seeall) --Functions and methods function New(name,age) local object = {} object.name = name object.age = age object.ammo = 20 object.health = 100 function object:sayName() return object.name end function object:fireGun() if object.ammo > 0 then object.ammo=object.ammo - 1 end end function object:hitTaken() if object.health > 0 then object.health=object.health - 1 end end return object end Then I create the Team.lua where I include Person, and adding new team mebers + a team report --Register class module("Team", package.seeall) include('Person') --Functions and methods function New() local object = {} object.members = {} function object:AddMember(name,age) local nextmember = #object.members + 1 object.members[nextmember] = Person.New(name,age) end function object:FindTeamMember(name) for i=1,#object.members,1 do if name == object.members.name then return object.members end end end function object:Report() for i=1,#object.members,1 do print( "Name:"..object.members:sayName().. " Ammo:"..object.members.ammo.. " Health:"..object.members.health) end end return object end Now I move on to App.lua which is my "Main" At the very top I just added a little helper so I can use include for the other lua files + includes the Team "class" ------------------------------------------ -- Helpers ------------------------------------------ --home made include function include(name) require("Scripts/"..name..".lua") end ------------------------------------------ include('Team') In App:Start() I create my team and start shooting : .. --create team myTeam = Team:New() --recruit members myTeam:AddMember("Joe",20) myTeam:AddMember("Bill",30) myTeam:AddMember("Josh",33) --report myTeam:Report() --Lets have some action : local player1 = myTeam:FindTeamMember("Bill") local player2 = myTeam:FindTeamMember("Josh") player1:fireGun() player1:fireGun() player1:fireGun() player2:hitTaken() player2:hitTaken() --report myTeam:Report() ... The output will now be : Name:Joe Ammo:20 Health:100 Name:Bill Ammo:20 Health:100 Name:Josh Ammo:20 Health:100 (shooting is done) Name:Joe Ammo:20Health:100 Name:Bill Ammo:17 Health:100 Name:Josh Ammo:20 Health:98 Probably better ways and better tools for this stuff, but it works, and you can split stuff into smaller bits using multiple files. 5 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
Daimour Posted October 3, 2013 Share Posted October 3, 2013 Yes. It's the best and simplest way to define classes in LUA, in my opinion. Quote Link to comment Share on other sites More sharing options...
Aily Posted October 4, 2013 Share Posted October 4, 2013 For searching members by name better to use: membersByName={} ..... -- when creating new one memebersByName[name]=member -- when searching return memebersByName[name] it's much faster than "for" loop in Lua 1 Quote "Better" is big enemy of "good" Link to comment Share on other sites More sharing options...
shadmar Posted October 4, 2013 Author Share Posted October 4, 2013 Yes I agree, it was just a really quick class mockup Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB 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.