Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local new = function(type)
- local classes = {
- fighter = {
- health = 100, --default health
- takeDamage = function(self, amount)
- self.health = self.health - amount >= 1 and self.health - amount or 0
- end
- }
- }
- return classes[type:lower()]
- end
- local enemy = new("fighter")
- print(enemy.health) --this will print 100, because the default health is 100
- enemy:takeDamage(50) --make the enemy take 50 damage
- print(enemy.health) --this will print 50 because the health changed
- local x = new("fighter")
- print(x.health)
Advertisement
Add Comment
Please, Sign In to add comment