What are getters and setters?

In a bid to make my code more efficient, I wanted to use metamethods instead of a while true do loop to check if a table has something inside before firing a function. However, this function has a wait() which causes the metamethod to output an error: attempt to yield across metamethod/C-call boundary. I was looking through a DevForum article on the problem and once again, came across the three familiar words that I have no clue about: Getters and Setters, mentioned by @Crazyman32

I have a couple of questions on the current problem I am facing:

  1. Is there a solution to what I want to achieve (mentioned in the first paragraph) without using a while true do loop?
  2. How do getters and setters work? Can they solve the problem that I have? (I have found no Developer Hub article on them)

Thanks!

Given your original question, I’ll assume you’re not familiar with object oriented programming (OOP), but that’s what this term is post commonly used in reference to. In their most basic form a getter is just a function which returns a value generally taken directly from a variable. A setter is just a function that sets the value of a variable. These are usually prefixed with get and set, respectively. So what I think Crazyman32 is recommending is that you write a module with functions such as getBoardName(), etc. It’s pretty much as simple as this:

TrelloModule = {}

function TrelloModule.getBoardName()
    -- Code to get board name
end

return TrelloModule

Then you could invoke it in the API fashion you described by doing this

local Trello = require(path.to.TrelloModule)

Trello.getBoardName()

Depending on how your setting this up, you might want to make a class out of this but that’s not really much different.

1 Like

What’s so special about getters and setters though? Can’t I do it all in one function?

If you’re asking why we do object.getProperty() as opposed to object.property directly then the answer is that’s just the way the paradigm is built. In a proper OOP setup all variables of a class should be completely private and only accessible through getters and setters. You don’t have to do this but a lot of people think it makes your code clearer, at the cost of length. I can’t speak to the overhead of this method but I’m sure it’s insignificant.

Ah, got it. Thanks for the clarification. I still have the problem of the metamethods yielding though. Is there any solution to that?

lol, this entire thing is meant to prevent you from using metamethods in this way. It’s actually really interesting that you started developing your own primitive OOP setup by yourself. But if you really really want to yield metamethods (which you should not do please dont) you could use tick() and and have a while loop checking if tick has progressed x milliseconds and breaking if it has. Please, don’t do that. But again, it’s significantly easier to make a table of getters.

Spare some consideration for the newbie in programming here (me). You said it yourself that I am not very familiar with OOP and now you indirectly implied to me that OOP is the solution and I should have known it all along. I think it would be better if you could elaborate on how do you make a table of getters or link me to some sources you find useful, again, for a newbie like me.

Getters and setters are also known as accessors and mutators.

This link explains it all. I know it’s in Java, a class-based programming language, however the concept is still the same in Lua.

2 Likes

Sorry, didn’t mean to do that. Crazyman32 actually included code for a simple OOP class. Making a class out of it may not be necessary, depending on what you’re doing. But assuming you’re only planning to access 1 Trello board it’s as simple as the code I gave you earlier. Here’s some resources on OOP if you’re trying to do something more complicated.

1 Like

I have read through many of the articles given by @sjr04 and @oreoollie. They are very good introductions but definitely still pretty technical. I still do not understand OOP very well so I can relate very well to Ripull back in 2014 who said:

I guess I’ll get back to learning about it again next time.

For now, I think I will do the best workaround I can think of by creating a separate script to act as the function and firing a bindable event over to that script so that the metamethod will end.

There’s no need to feel below us. Some programming concepts are harder to learn than others but the important thing to remember is that everyone went through it. And I find that some concepts are better learned when taught by a person, rather than a proverbial cookie cutter. Add my Discord, I’ll happily explain it to you. Nitrousoxide#3667

1 Like

Hey, thanks for your encouragement. Maybe I’ll take it up some day. Right now, I think I will move on with my programming and strengthen my foundations before moving up to the harder stuff. Thanks for the offer, I will consider it!

1 Like