Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to implement getters and setters (accessors and mutators) in Lua Classes?

##Getters and Setters

I have actually heard about getters and setters in C++. I would love to implement them to my Lua classes.

The code below is in C++.

#include <iostream>

using namespace std; // Let's just say I was coding in CodeBlocks IDE

class myClass(){ // Pretty much a class
	private: // How to make values in a class private in Lua?
		string name;
 	public:
		// Incapsulation
		void setName(x){ // The setter
			name = x;
		}
		
		void getName(){ // The getter
			return example;
		}
}

int main(){
	myClass class; 
	class.setName("getters and setters in C++")
	cout << class.getName() << endl; // cout is equivalent to: print()
}

Lua Implemention

How can I implement something like that into Lua?

local dog = {}

dog.__index = dog -- Maybe to add getters and setters, I have to create classes differently?

function dog.new(dogBarkSound,dogName)
	-- The dog's class's properties are accessible using implicit self parameter in methods
	return setmetatable(
		-- I don't think there is a way to set something private unless using scope.
		{dogBark = dogBarkSound,name = dogName}, dog 
	)
end

function dog:barkSound()
	print(self.dogBark) -- Easily accessible by using self
end

Thank you for helping me in advance!

1 answer

Metatables aren't just for making Classes! They have a lot of other cool features that make them unqiue such as protection and operators.

They aren't the most documented piece in Lua so finding information can be difficult but the reference manual http://www.lua.org/manual/5.1/manual.html#2.8 is pretty good on the subject.

Below is how I usually format my Classes.

local dog = {}
dog.__index = dog

function dog:new(dogName, dogBarkSound)
	local dogData = {
		dogName = dogName,
	  	dogBarkSound = dogBarkSound,
	}
	-- I make a local table called dogData just because I don't want to fit
	-- a table inside the setmetatable function in case I add more variables
	-- making the setmetatable function kinda congested.
	return setmetatable(dogData, dog)
end

-- I just want to return the dog's name because I might just want the
-- name and not just print it. I'll do the same with the getBark function
function dog:getName()
	return self.dogName
end

function dog:getBark()
	return self.dogBarkSound
end

function dog:changeBark(newBarkSound)
	self.dogBarkSound = newBarkSound
	 -- It's nice to return values, but it totally optional
	return self.dogBarkSound
end

-- Lets make a new dog class and name him Spot and give him a loud bark
local spot = dog:new("Spot", "Loud")
-- Lets make a new dog class and name him Max and give him a quiet bark
local max = dog:new("Max", "Quiet")

-- Print Spot's data
print("The dog's name is: " .. spot:getName() .. " and he has a " .. spot:getBark() .. " barking sound!")
-- Print Max's data
print("The dog's name is: " .. max:getName() .. " and he has a " .. max:getBark() .. " barking sound!")

-- Change Max's bark to loud
max:changeBark("Loud")

print(max:getName() .. "'s new bark sound is: " .. max:getBark())

Good luck!

This discussion is preserved as it was. New answers, comments, and votes are closed.