Oh no, that would give an invalid access specifier error. Members must always be placed inside access specifiers. Try putting the function inside an access specifier, for example, Public like below:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClassPP = require(ReplicatedStorage["Class++"])
local class = ClassPP.class
local Car = class "Car" {
Public = {
Brand = "Lamborghini",
changeLicensePlate = function(self, plate: string) -- We put this function inside the Public Access Specifier, so it can be accessible from everywhere.
self.License_Plate = plate
end,
},
Private = {
License_Plate = "XXXX"
},
}
local newCar = Car.new()
newCar:changeLicensePlate("YYYY")
Check out the documentation for more information access specifiers! You should read the entire thing through, it would be very useful.
Improved performance on accessing an object’s Private and Protected access specifiers.
Updated the final and abstract functions to now accept multiple classes with a new syntax.
Util.inClassScope function now accepts a new optional defaultLevel value that determines the default call stack level the function will start doing the checks on.
A performance comparison picture between 1.1.0 and 1.2.0:
Classes can now have static members using this function, just like in Java and C++, they belong to the class, and will not replicate to the objects created from the class.
They’re global, and will be the same everywhere.
You can access the static members through the class object like: class.<memberName>.property or class.<memberName>.p for short.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local ClassPP = require(ReplicatedStorage["Class++"])
local class = ClassPP.class
local toolClass = class "Tool" {
constructor = function(self, tool)
if tool and tool:IsA("Tool") then
self.Tool = tool
self.Configuration = require(self.Tool.Config)
self.Tool.Equipped:Connect(function()
self.equipped = true
end)
self.Tool.Unequipped:Connect(function()
self.equipped = false
end)
end
end,
Public = {
equipped = false,
Tool = nil,
Configuration = {},
}
}
CollectionService:GetInstanceAddedSignal("Tool"):Connect(function(tool)
if tool:IsA("Tool") then
local newTool = toolClass.new(tool)
end
end)
This is the Error : {Class++}: This class has no member named "Tool".
I see what you mean now, this is a common mistake that people encounter when creating classes using my module.
This is mostly related on how dictionaries work in Luau, when you set a key to nil, you’re actually removing that key from the table. In this case, the key Tool is being set to nil in the Public access specifier, and since you’re setting the key Tool to nil, Lua automatically removes it from the table, which in the final classData, the Tool member does not exist.
To solve this issue, instead of setting it to nil, you can set it to false.
Here’s your updated code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local ClassPP = require(ReplicatedStorage["Class++"])
local class = ClassPP.class
local toolClass = class "Tool" {
constructor = function(self, tool)
if tool and tool:IsA("Tool") then
self.Tool = tool
self.Configuration = require(tool.Config)
self.Tool.Equipped:Connect(function()
self.equipped = true
end)
self.Tool.Unequipped:Connect(function()
self.equipped = false
end)
end
end,
Public = {
equipped = false,
Tool = false,
Configuration = {},
}
}
Class++ is finally complete and out of beta! I can’t really think of adding any new features, so this is going to be the last version for now.
Until I get a new idea or find an efficient method to implement certain ideas that I currently have, the only updates there will ever be are performance updates and bug fixes. (And maybe some internal changes)
Thanks for using my module!
Updated the source code to be more efficient and clean, and removed unnecessary comments from the code.
Updated certain internal functions to be more performant.
Added a new documentation page describing how to use type checking with Class++. (It’s very useful, check it out!)
Fixed certain parts of the documentation.
Removed final functions (using the final keyword with functions), unfortunately implementing final functions required some internal changes that weren’t very worth it for this feature, so I decided to remove it altogether, you can use other methods or just use final classes instead.
(The final keyword now works as it was in 1.3.1)
If you have any new feature ideas, or found bugs, please don’t hesitate to create a feature request or a bug form though!
Class++ objects now support type checking! This was an update that I was planning to do for a very long time, and it would not have been possible without @HugeCoolboy2007.
All of the created objects should now have type completion for the members stored in the Public access specifier.
Creating custom types is no longer needed, however, if you still want to use the full features of type checking in Luau, it’s still recommended that you use custom types for your classes.