Search code examples
lualove2d

Lua class objects?


I'm new to Lua and I'm wondering if there's a way to make many class objects to make different items in my case like in a OOP language like C# or Java. An example of what I'm saying is a class like this in Lua...

weapon = {}

function weapon.load()
{
    weapon.name = "CHASE'S BUG"
    weapon.damage = 1
    weapon.rare = "Diet Valley Cheez"
    weapon.hottexture = love.graphics.newImage("/ledata/invalid.png")
    weapong.playtexture = love.graphics.newImage("/ledata/invalid.png")
    weapon.dura = 1
    weapon.type = "swing"
}

But in a main class you could make new objects of that class which would be done like so in something like C#

weapon Dagger = new weapon();
Dagger.name = "Dagger of Some Mountain"
...

Is there a way to do that in Lua?


Solution

  • Lua is object oriented, but it's not like Java/C++/C#/Ruby, etc, there's no native class, the only way to create new object is to clone an existing object. That's why it's called prototype language(like JavaScript).

    Read Programming in Lua Chapter 16. You can mock normal OOP using metatable.