I don’t really understand what you mean by this, could you provide some example code? Again I am still just trying to mess around with this style of coding.
Yes.
local myObject = {}
local myObjectMetatable = {__index = myObject}
-- do this instead of myObject.__index = myObject
--later in the script when you create a new object from the constructor
setmetatable(myNewObject, myObjectMetatable)
I believe the issue is that you’re setting the metatable before doing Pickaxe.__index
.
Like this? if so I am still having the same issue
local Pickaxe = {}
local PickaxeMetaTable = {__index = Pickaxe}
function Pickaxe.new()
local NewPickaxe = {}
setmetatable(Pickaxe, PickaxeMetaTable)
return NewPickaxe
end
return Pickaxe```
Interesting. I believe it has something to do with the inheritance, but I’m not really good at inheriting systems. Here’s a guide on how to do it, but I’m not sure if I could help much further. If you find an answer, post it here.
(read post 18 for inheritance)
It doesn’t look to me like you’re setting up your metatables incorrectly. Are you sure you’re requiring the right module (the pickaxe one)? I copied yours 1:1 and am not getting any issues.
Hey I apologize for the delayed response, I’m not really sure of anything. Could you perhaps send me EXACTLY what you have copied? I will see if this works for me as well and note any differences.
Even when using the exact code I provided 1:1 but changing out, adding etc the requires it still doesn’t work, could you provide me an example with the main script you used? Currently im using the following:
local CreateTool = require(script.Parent.CreateTool)
local Pickaxe = require(script.Parent.Pickaxe)
local NewPickaxe = Pickaxe.new()
NewPickaxe:METHODHERE
the problem is it has no methods for some reason ( functions? methods? same thing right? )
After doing a little bit more testing, I think the issue may have to do with the constructor of subclass ( pickaxe ) or my main script not properly working, when trying to pass print in my constructor, nothing happens implying nothing has been constructed which would make sense why there are no functions?
Yeah.
Pickaxe:
local CreateTool = require(script.Parent.Tool)
local Pickaxe = {}
setmetatable(Pickaxe, CreateTool)
Pickaxe.__index = Pickaxe
function Pickaxe.new(owner, durability, damage)
local NewPickaxe = {}
setmetatable(NewPickaxe, Pickaxe)
return NewPickaxe
end
function Pickaxe:CreatePart()
print(3)
end
function Pickaxe:TestPart()
print(4)
end
return Pickaxe
Tool:
local Tool = {}
Tool.__index = Tool
function Tool.new(owner, durability, damage)
local NewTool = {}
setmetatable(NewTool, Tool)
NewTool.Owner = owner
NewTool.Durabiltiy = durability
NewTool.Damage = damage
return NewTool
end
function Tool:UseTool()
print(5)
end
function Tool:ChangeValue(val)
self.Owner = val
print(self.Owner)
end
return Tool
Test script:
local pick = require(script.Pick)
local newPick = pick.new()
pick:CreatePart() -- ok
pick:ChangeValue(1) -- ok
I think the subclass script is written wrong anyway, I’ve fixed it but again am still getting the same error.
local Pickaxe = {}
Pickaxe.__index = Pickaxe
setmetatable(Pickaxe, CreateTool)
function Pickaxe.new(owner, durability, damage, heat)
local Object = CreateTool.new(owner, durability, damage)
setmetatable(Object, Pickaxe)
Object.Heat = heat
return Object
end
function Pickaxe:CreatePart()
print(self.Owner)
end
function Pickaxe:TestPart()
print(4)
end
return Pickaxe```
the above should be correct as per the tutorial I was following.
also are you sure this is working correctly? I’ve tried to use it to test it myself and still encountering error? the three scripts are almost definitely requiring the right modules, with the current setup you just sent I can only use functions from within the tool module, none from the pickaxe module even though I am creating a pickaxe object which should have both right?
Any chance it’s a commit failure or something? I’m certain it’s working on my end. I’ll attach my setup
Script.rbxm (1.7 KB)
Okay another update maybe it is how I am requiring it? I’ve literally just renamed them to exactly how you have it setup and moved them into the same order and now it’s working, the way I had it setup was that I had my main script > Tool( Module ) > Pickaxe ( Module ) would this indented method cause issue?
also I am not 100% sure if it is fixed, as using pick.new() then enacting change on that is fine, but shouldnt I really be enacting change on newpick rather than pick.new?
Oh whoops you’re right, oversight on my end.
But even so, namecalling using newPick doesn’t create any errors.
local pick = require(script.Pick)
local newPick = pick.new()
newPick:CreatePart() -- ok
newPick:ChangeValue(1) -- ok
I just want to be certain- you’re talking about it erroring at runtime and not that autocomplete doesn’t show up for the methods, right?
Well I am mainly talking about auto complete but I was receiving errors during runtime originally, I will test again to ensure this is working. I found a correlation between it not showing the auto complete and an error.
Testing this confirms my prediction anyway, while I didn’t receive an error this time I just didn’t get any output from the following:
local pick = require(script.Pick)
local newPick = pick.new()
newPick:CreatePart()
newPick:UseTool()
pick:CreatePart()
pick:UseTool()
Error on my behalf - I should probably test more before commenting, had the script in the wrong area anyway which explains the lack of output. I am receiving outputs, will test further and update you shortly.
Works! I appreciate the help a ton and this has given me insight into a major error of thinking when it comes to using the auto complete, testing my code and actually writing oop. I really appreciate the help. Marking solution now :)) @7z99
( I am still unsure why the modules wouldn’t require properly the way they were setup, but I’m sure it’s probably something simple i overlooked. )
By the way, autocomplete gets confused with metatable chaining at the moment, so you can get autocomplete back by creating a union type that is a theoretical metatable that includes properties in the table, and methods in the metatable, and then whichever class it inherits.
It also might be a good idea to call CreateTool.new() for your object to inherit properties as well.
Final result looks like:
Pickaxe:
local CreateTool = require(script.Parent.Tool)
local Pickaxe = {}
setmetatable(Pickaxe, CreateTool)
Pickaxe.__index = Pickaxe
function Pickaxe.new(owner, durability, damage)
local NewPickaxe = CreateTool.new(owner, durability, damage)
setmetatable(NewPickaxe, Pickaxe)
return NewPickaxe :: Pickaxe
end
function Pickaxe:CreatePart()
print(3)
end
function Pickaxe:TestPart()
print(4)
end
export type Pickaxe = typeof(setmetatable({} :: {
-- list properties that aren't in Tool here
}, {} :: { __index: {
-- list methods here (class methods called with a colon have self passed implicitly, in this case it would be the Pickaxe type)
CreatePart: (self: Pickaxe) -> ();
TestPart: (self: Pickaxe) -> ();
}})) & CreateTool.Tool
return Pickaxe
Tool:
local Tool = {}
Tool.__index = Tool
function Tool.new(owner, durability, damage)
local NewTool = {}
setmetatable(NewTool, Tool)
NewTool.Owner = owner
NewTool.Durabiltiy = durability
NewTool.Damage = damage
return NewTool
end
function Tool:UseTool()
print(5)
end
function Tool:ChangeValue(val)
self.Owner = val
print(self.Owner)
end
export type Tool = typeof(Tool.new(Instance.new('Player'), 1, 1)) -- this will inherit Tool methods as well as properties
return Tool
Then suppose we have a third even less primitive class (let’s say DiamondPickaxe), you could implement its class and type like so:
local Pick = require(script.Parent.Pick)
local DiamondPick = {}
DiamondPick.__index = DiamondPick
setmetatable(DiamondPick, Pick)
function DiamondPick.new(...)
local newDiamondPick = Pick.new(...)
setmetatable(newDiamondPick, DiamondPick)
newDiamondPick.Durable = math.random(0, 1) == 1
return newDiamondPick :: DiamondPick
end
function DiamondPick:IsDurable()
return self.Durable --> true or false depending on whether or not it's durable
end
export type DiamondPick = typeof(setmetatable({} :: {
-- list properties that aren't in Pickaxe here
Durable: boolean
}, {} :: { __index: {
-- list methods here (class methods called with a colon have self passed implicitly, in this case it would be the Pickaxe type)
IsDurable: (self: DiamondPick) -> boolean;
}})) & Pick.Pickaxe
return DiamondPick
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.