From 29469232dbec01f7f03b5f34811878a2a6a517fd Mon Sep 17 00:00:00 2001 From: Logen Kain Date: Wed, 10 May 2017 15:04:18 -0700 Subject: [PATCH] Lua: added classes concept --- lua/classes/main.lua | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 lua/classes/main.lua diff --git a/lua/classes/main.lua b/lua/classes/main.lua new file mode 100644 index 0000000..c8facd0 --- /dev/null +++ b/lua/classes/main.lua @@ -0,0 +1,26 @@ + Player = {} +Player.__index = Player + +function Player:create(health, strength) + local player = {} -- Our object + setmetatable(player, Player) -- our handle lookup + player.health = health -- initializing or stuff + player.strength = strength + return player +end + +function Player:damage(damage) + self.health = self.health - damage +end + +--Create our main character +player_Jack = Player:create(100, 12) + +print ("Jack's current health is: "..player_Jack.health) + +print ("Jack's current strength is: "..player_Jack.strength) + +print ("Despite his mighty strength, Jack is still an idiot and got hit for 10 damage") +player_Jack:damage(10) + +print ("Well, if he keeps this up he is going to die, his health is now: "..player_Jack.health)