OOP private functions and variables

I’ve been using OOP as of late and i find it really useful and makes my code more organized. My issue is that when i build classes, variables are all public meaning the person using the class can change some essential variables. Someone said “why don’t you make variables with self and some with local”? The problem with that is that the methods wont get that variable because its scope is the .new only.

Ill show example of how i create basic classes.

local module = {}
module.__index = module

function module.new() 
 local self = setmetatable({}, module) 

 self.varible = 10

 return self
end


return module
2 Likes

there arent private functions perse, but for functions you don’t want any other scripts using, you could just put an underscore like _PrivateFunction
if you want private variables since that isn’t a native feature of lua you could just do _PrivateVariable
also uh, you’re setting the indexes of the metatable… not the table inside of the metatable…

here’s a tutorial on it for more detail: All about Object Oriented Programming

2 Likes
local module = {}
module.__index = module

function module.new() 
 local self = setmetatable({}, module) 

 local variable = 10

 function self.getVariable()
   print(variable)
   return variable
 end

 return self
end


return module

This’ll let you get the variable, but not change them.

local moduleNew = require(module).new()
local var = mod.getVariable()

As @syylvi said, there aren’t any built-in ways to code in private variables and functions, the only way is by using an underscore to emulate and interpret a specific variable as a private variable. However, I’ve made a resource to allow such cases and many more to aid you in your day-to-day OOP shenanigans: Class.

I’ve never understood why private functions are essential to OOP. But if you need them, you can probably set a private interface, use it in your module code, and have its __index set to the object.

You can use metamethods for exactly this. __index and __newindex. They allow you to control how keys are accessed and set in your table. Though it would probably be much easier to just designate your private variables with a _, as mentioned above, such as _PrivateVariable.

https://www.lua.org/pil/13.4.1.html

Alternatively you can make a type for private functions, which the internal object will use, and another one for the public and exposed ones and set it to the object you will return.

type privateObject = {
   privateFunc: () -> void
}
type publicObject = {
   var: number
}
local object = {}
object.__index = object
function object.new()
   return setmetatable({var = 5}, object) :: publicObject
end
return object

(example, will not actually work)

Why does everyone forget #help-and-feedback:scripting-support exists istg this is like the tenth post

Security for the functions. Think of it like this:

You have a module script on the client that controls player hitbox stuff with a function for adjusting size that you need on the client. If you were to implement this idea in OOP without any privatization any exploiter injecting scripts can now call this function from the module, rendering pretty much any security measures useless. You could fix this by making use of security/sanity checks, but it would be far more efficient to just disallow access to every script except for the ones that need access. Saves a lot of headaches as well.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.