What really is an object?

Will hopefully give you a better understanding.

by ScripterGo

Author Avatar

Prerequisites

*An understanding of tables

So what do we consider an object?

A table in lua is essentially an object. Just like objects tables have an "identity" thats used to tell them

apart. Two tables(objects) with the same values are diffrent objects, an object can have diffrent values at

diffrent times but they will still be the same object. So you could say that tables(objects) are independant

of their values.

These objects can have their own functions or more commonly called methods. There are a few ways that you

could use to give your objects methods. One of those "ways" would be to use metatables and another would

be to directly set keys of the object equal to functions.

How do we give our objects methods?

As mentioned one of the ways would be to use metatables, it could look something like this:

local metatable = {}
metatable.__index = metatable

function metatable:AddedFunction()
    print("Yeah it works!")
end

local Object = {}
setmetatable(Object, metatable)
Object:AddedFunction()

Another approach would be to directly set the keys of the object to equal a function like so:

local Object = {}
function Object:AddedFunction()
    print("Yeah it works!")
end
Object:AddedFunction()

What about properties?

A property of a zombie object could be zombies health and maxhealth. Im again going to cover two diffrent ways, that you could give your object properties on.

This approach uses metatables, however you should only do this with constants(values that wont be manipulated or changed)

local metatable = {}
metatable.__index = metatable
metatable.MaxHealth = 200

local Object = {}
setmetatable(Object, metatable)
print(Object.MaxHealth)

Yes, you guessed it. In the second way we are going to directly set the keys equal to a value.

local Object = {}
Object.MaxHealth = 200
print(Object.MaxHealth)

I hope this gave some clarification when it comes to objects in lua:)

View in-game to comment, award, and more!