Hey there,
I dont really know if I'm doing this right but I'm trying to create my own table in a way that is object oriented. I create a new table with:
cEntity = {}
cEntity.__index = cEntity
function cEntity:new(etype, model, x, y, z, mat)
local newEnt = {
entityType = etype,
model = model,
x = x or 0,
y = y or 0,
z = z or -445,
material = mat or "",
prop = CreateNewProp(model, material, x, y, z), -- returns csent made with ClientsideModel
update = false
}
local cEnt = setmetatable(newEnt, cEntities)
return cEnt
end
and I am trying to call
function cEntity:GetXYZ()
local pos = {}
pos.x = self.x
pos.y = self.y
pos.z = self.z
return pos
end
in a different client script which has
include('cEnt.lua')
local cEnt = cEntity:new(0, model, v.gmX, v.gmY, v.gmZ)
local pos = cEnt:GetXYZ()
but getting the error:
attempt to call method 'GetXYZ' (a nil value)
Anyone know what I'm missing?
Thank you
Is your first code only serverside? Because if you define these functions only on the serverside you won't be able to use them on the clientside.
Everything is clientside
In the first block of code in the setmetatable function I see the second argument is CEntities but I don't see it defined any where. Looks like it needs to be set the the CEntity table.
Lmao stupid me. Thank you both so much.
I have a bonus coin for whomever can answer me this.
With the meta tables above i would like to inherit said properties and apply different functions/variables depending on the "class" I choose to create.
Is there a way to do this with the above code?
For simple function interference just set the __index meta method of the class to the class you want to inherent from.
e.g.
newClass = setmetatable({}, {__index = baseClass})
and I could then do:
local bleh = newClass:new(0, model, x, y, z)
function newClass:Otherfunction()
...
end
bleh:Otherfunction()
and it would affect and it wouldnt affect any other objects from "baseClass"?
yeah, though you still need to set `newClass.__index = newClass`
Sorry, you need to Log In to post a reply to this thread.