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

About the keyword 'self', how it works, its uses, and what's so useful about it?

Asked by
acid392 11
5 years ago

So I had this question for a very long time, and I wasn't able to find a good website/application/place or someone who has time to fully explain to me the uses of self, and if it's useful in some cases, and how would it look like when used properly, so here's a quick list of what I really need to know about the keyword 'self'.

  1. Is the keyword 'self' useful?, if so, how and when?
  2. When's it appropriate to use the keyword 'self'?
  3. Why was the keyword 'self' made for Lua?, and was it just made for Lua? These are my current questions, feel free to show me code examples, I don't mind at all.

I've been very frustrated about the keyword 'self', because I've found many different ways of configuring Lua code in many different methods, so that's why I'm asking/wondering why it's a thing in Lua.

0
I recommend reading the lua pil from chapter 16 to 16.2 https://www.lua.org/pil/16.html SirDerpyHerp 262 — 5y
0
it just makes code look better in OOP greatneil80 2647 — 5y
0
will do acid392 11 — 5y

1 answer

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

TL;DR: it's for object oriented programming and when you're saying 'self' you're referring to an instance/object created by the script, not the script itself.

I'm not the best at explaining so here's some tutorials on object oriented programming: ROBLOX Wiki Article

DevForum article

and watch some youtube tutorials as well. they're more focused on explaining what the concept is rather than how to do it on roblox probably.


1) Is the keyword 'self' useful?, if so, how and when?

yea. see question 2

2) When's it appropriate to use the keyword 'self'?

self is used in object oriented programming (OOP). in OOP, you have classes and objects. (objects are instances of the class whereas classes are a blueprint of what an object contains). an example you're probably familiar with is Instance.new()

When you say Instance.new("Part") you're creating a new part object from the Part class. A part has properties such as Part.BrickColor, Part.Transparency, etc. and it also has methods (functions) such as Part:Destroy() or part:Resize()

It even says that part is a CLASS in the api reference link !!


Anyway, a class would look something like this: (this isn't what the part class looks like it's just an example of OOP in lua)

Part = {} --This is a table where your part instance will go when it's created. 
Part.__index = Part --metamethod, that creates variables for you when you're referring to a variable and it doesn't exist. don't have to worry about what this line does really.

--by calling this function (called a constructor function), you will make a new part object/instance
call this function by saying: myPart = Part.new()
function Part.new(position)
    local newPart = {}
    setmetatable(newPart, Part)
    newPart.Transparency = 0
    newPart.Position = position --if you specified a position, you can set the part's position variable here

    return newPart --returns the part instance you just made into myPart variable 
end

--a method for the part class. you can call this by saying Part:SetTransparency(newTransparency)
function Part:SetTransparency(newTransparency)
    self.Transparency = newTransparency
    --This is where self comes in. the INSTANCE's transparency variable is set to the new transparency.
end

In the example above, there's a SetTransparency() function. The function uses the keyword self. If I was to call myPart:SetTransparency(.5) I'd be setting the part myPart's transparency to .5. When we use self, we are referring to the instance (aka myPart) and it's properties/functions, not referring to the script itself.


3) Why was the keyword 'self' made for Lua?, and was it just made for Lua? These are my current questions, feel free to show me code examples, I don't mind at all.

Most high level languages like lua have a keyword like self. The keyword varies per language, but usually it's self or this.

0
sorry if this doesn't make much sense i just got off a really long homework binge and i'm pooped out. royaltoe 5144 — 5y
0
I guess it's good, but i need something very very simple, pretty input and output, nothing to do with objects or services, just basic variabels, tables, functions, you get what i mean?, just something that a beginner would understand. acid392 11 — 5y
0
how well do u understand tables and do you want to talk on discord / in the site chat? royaltoe 5144 — 5y
0
Sure, why not acid392 11 — 5y
0
Confused Hamster#9512 acid392 11 — 5y
Ad

Answer this question