Does lua have set and get like C #?

just curiosity and obviously the title says it all. Thanks.

1 Like

No, Luau doesn’t have Get and Set methods.

1 Like

I’m not really familiar with C#, so I’m not sure what this means - do you mind explaining? I’m sure that the fundamental concept, at least, is achievable here on Roblox.

We do have getters and setters but those terms refer to reading and writing data via functions and are natively accessible from services. I assume the same thing is here but since you haven’t provided a specific example I’m inclined to believe the get and set you’re looking for are globals in C#.

To explain it very shortly, Get and Set methods are used in OOP in C# for a thing called Encapsulation, which is just used to make sure that sensitive data is hidden from users, but since Lua doesn’t have built in OOP support, naturally Lua doesn’t have have methods like Get and Set.

1 Like

There aren’t but you can make your own ones using __index and __newindex metamethods and an extra table object.

I think that wouldn’t work because __index runs when you index something nil and __newindex runs when you add a new value to the table (not change an existing one) while get should run when you index anything in the table and set should run when you set anything in the table (correct me if I’m wrong). I’m basing this off of get and set in js.

Thanks for the explanation. Is private access really used to just hide data from users? Here on Roblox, we prefix things with an underscore to denote private access but it doesn’t actually do anything other than indicate that something shouldn’t be used outside of the place it was declared in or in a confined system using that code (e.g. another script should not use something prefixed with an underscore, but doesn’t mean it can’t). There’s no such thing as hiding from the user unless it’s on the server. It seems these concepts are the same, except prefixing with an underscore isn’t explicitly an access modifier.

2 Likes

You can have one table with the data and another table that is used for getting and setting values in the main table. You also need a metatable that has the index and newindex metamathods set as functions. This will be set as the metatable of the table that is used to set and get. The index metamethod will then get the data from the table that contains data and newindex will set data in that table, so the table used to get and set values will not be modified.

This doesn’t really do the same security thing, but it can be used to automatically fire custom events when a change in a value happens.

1 Like

I never thought of doing it like that, a few weeks ago I was making my own lua event system for tables based off of roblox’s event system for objects, I didn’t know how I would make a Changed event since the only metamethod I found that was similar to that was __index but it only ran when you index something that is nil. Now that I think about it I could have just used a loop. :thinking: Interesting.

I think Im going off topic

You can do something like this:
C#:

class Person
{
  private string name; // field
  public string Name   // property
  {
    get { return name; }
    set { name = value; }
  }
}

becomes this in Lua:

local Person = {
    name = ""
}
Person.__index = function(tbl, key)
    if key == "Name" then
        return rawget(tbl, "name")
    end
end

Person.__newindex = function(tbl, key, value)
    if key == "Name" then
        return rawset(tbl, "name", value)
    end
end

function Person:new(tbl)
	tbl = tbl or {}
	setmetatable(tbl, self)
	return tbl
end

I don’t recommend doing this though. If you really need encapsulation do it like it was done on the official documentation. Programming in Lua : 16.4

1 Like

I didn’t explain it to you very well or I didn’t understand your message very well so I’m gonna give you a very simple example.

Let’s say you have an object with a property named “Month” that stores the month in a number ranging from 1 to 12. Now obviously at some point, a user will definitely try to set that property value to something other than it shouldn’t be and you will have to write a code to deal with that which is where Get and Set methods come in and here how it works:

You declare 2 variables(One private, One public) and use private one to store the actual value inside your class structure, then you add get;set blocks to public one to determine what to do when an user tries to read data or write data to that variable. They kinda work like metamethods with functions attached to them in a metatable. The function inside Get fres when user tries to read the property, set fires when user tries to edit it.
This is just a simple example, there are more things you can do with it.

public class Month
{
    private int monthnumber = 7;

    public int MonthNumber
    {
        get
        {
            return monthnumber;
        };
        set
        {
            if ((value > 0) && (value < 13))
            {
                monthnumber = value;
            }
            else
            {
                monthnumber = 12;
            }
        }
    }
}

Well, I’ve been thinking about it, but it’s quite verbose. My main purpose is for easy event control.

I have seen your lua documentation. In fact, I thought about it before I even read it.

generally setmeta() and something similar I’ve only been using it recently so I’m not sure yet.