Jump to content

Really simple lua OOP exercise.


shadmar
 Share

Recommended Posts

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.

  • Upvote 5

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

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

  • Upvote 1

"Better" is big enemy of "good"

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...