Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How function void:void() works?

Asked by
Ziffixture 6913 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

I’ve been studying a bit more on Lua API to strengthen my ability, and I came across this weird method of writing a function that I personally have never seen before, the formality look somewhat like this, function void:void(). Could someone explain this to me? Also is there a possible way to write this as a local function or variable, I.e local void:void = function()?

Thank you!

0
Why Lua C? Are you using it for exploits? User#24403 69 — 6y
0
Oh, dear no, I'm not a jerk, I mean API* Ziffixture 6913 — 6y
0
Since this is a modified version of Lua Ziffixture 6913 — 6y
0
Not sure if this is what you are looking for. `local Blueprint = {}; function Blueprint:void (){…};` AbstractionsReality 98 — 6y
View all comments (6 more)
0
I guess, but I want to know what the main formatting type is, how it works, what it does Ziffixture 6913 — 6y
0
the `:` syntax is used when you want to hide the self in the parameter. Ex, both the above code and this is the same. `local Blueprint = {}; function Blueprint.void(self, ...) {...};` AbstractionsReality 98 — 6y
0
Hey I updated a bit at the bottom of the answer. Check it out and see if you still don't understand. If not, let me know again and I will help :) The reason why you should understand this is because in ROBLOX, everything revolves around OOP. When you are creating new Instance, that is OOP. When you do GetChildren(), that is too. AbstractionsReality 98 — 6y
0
If you still don't understand, tell me the spots and I will re-edit the posts. AbstractionsReality 98 — 6y
0
Hey I updated the whole answer. Be sure to read the whole thing again. If you need help understanding bits, please ask me :) AbstractionsReality 98 — 6y

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

Updated the whole answer

Lua is not an Object Oriented Program, which is why we use tables and metatables to replicate one. Your line was as follows

function void:void ()

The first void is refered to the table that contains the function. Since void:void is pretty confusing, let me instead say Blueprint:void.

function Blueprint:void ().

The Blueprint table contains the function void. The interesting thing to note here is the :. Typically, when creating functions which go inside the table, we use ..

local Blueprint = {};

function Blueprint.void () {...};

is the same as

local Blueprint = {
    function void () {...};
};

as you may have know already

But the : is really unique, especially to Object Oriented Programming. You have used methods everywhere. game:GetService(), game:FindService(), Part:Destroy(), Object:GetChildren() and etc. They are all methods which uses the :. Why is that?

This is because function Tab:Func () is another way of saying function Tab.Func(Tab). When we use :, we do not need to reference itself again. When referencing the table the function contains, people generally use the variable self. Although it syntax highlights in ROBLOX, it is not a vairable.

Now lets see some practical usage of : in Programming.

local Class = {};
Class.__index = Class;

function Class.new ()
   local template = {};
   return setmetatable (template, Class);
end

function Class:coutMessage (message)
    print(message);
end


local zafirua = Class.new ();
zafirua:coutMessage("Hello I am Zafirua");

Let's talk about what this code does line by line!

First line, we created a main Blueprint called Class. It is a table and will hold all of our functions or values or metavalues. The next line, we use the __index metamethod since we want the object to inherit its methods and/or its properties.

'__index' is a really vast metamethod. It is almost used in every single OOP classes in Lua. When you want to access to a non-existing key inside a Lua table, the result is nil. For example,

local t = {};
print(t.a);

would print nil. Now you may assume that the reason it is printing nil is because it looked through the table t and did not find any key called a and it returned nil. Now while that is kinda right, it isn't. When it cannot find the key called a, it then checks to see if there is a metatable attached to it. If it exists, it then returns the key's value. Else, it returns nil. An obvious example is as follows.

local Class = {
    a = 1;
};

Class.__index = Class;

local template = {};
setmetatable(template, Class);

print(template.a);

It prints the Class.a's value which is 1!

The fourth line, we create a new function called new. What this does is that it allows us to create a new object that will inherit the properites of Class! The next line, we created a new table called Template. and using the setmetatable which simple assigns Metatable to another table, we attached Class to Template!

With this, we can actually reference all of Class's methods without actually calling the Class. What do I mean by that?

Say for example the Class table contained a function called printName. Since the Class allows inheitance because of __index, and the Class metatable is set to Template, we can do Template:printName() as opposed to Class:printName()!

Now, you will realize that we aren't actually using Template:printName() but zafirua:printName(). And the reason why is because zafirua is actually a reference to Template. So technically we kinda are doing Template:printName()

If this paragraph confused you, think of it like this.

local t1 = {1, 2, 3};
local t2 = t1;

-- and 

local Template = {--Contains all the methods from super class called "Class"};
local zafirua = Template;

Then we called the printName() method and it printed the message.

If you wanted to write function Class:printName(message) in different approach, you would do function Class.printName(self, message) where self means whichever object is calling this method. In our case, the object is called zafirua. So the parameter that is seen internally is function Class.printName(zafirua, "Hello I am Zafirua!");

I hope this made much more sense to you. If it did, please be sure to accept the answer. If not, please do not hesitate to ask for clarification.

0
Sorry, I wish i could but it’s still way to confusing, I have no idea what anything you’re saying means, what’s ‘self’ I see that in a lot of metamethods?, I’d just eather stay out of metamethods, I don’t see the importance of using them anyway, thanks though Ziffixture 6913 — 6y
0
I can't take a look at it now, but I will when I get the chance, it seems like it should be easier to understand so just hold on Ziffixture 6913 — 6y
0
I can't take a look at it now, but I will when I get the chance, it seems like it should be easier to understand so just hold on Ziffixture 6913 — 6y
0
It made a bit more sense Ziffixture 6913 — 6y
Ad

Answer this question