Object-Oriented Programming & Inheritance - Lua Tutorial (2021)
Вставка
- Опубліковано 27 лис 2024
- This video explains OOP and inheritance in Lua and how it can be used to create classes.
Socials:
Discord server: / discord
Twitter: bit.ly/TwitterWMG
Instagram: bit.ly/InstaWMG
Our Games: bit.ly/ItchWMG
Text Editors: Atom: bit.ly/2WRikj8
[Music] - Daniel Docherty www.danieldoch...
We now have a DISCORD server: discord.gg/mUaVASvG4z
Thank you for watching, and hope to see you there :)
Finally
I think it's fair to say that your persistency had an effect ;)
Thank you. After looking up many Lua OOP tutorials, it was never really explained to me WHY we write the whole "__index" and "setmetatable" thing. You explained it very well here!
Cheers! That's my main goal, that people learn something, not just memorize it :)
7 minute video
I learn for half an hour
But it was worth it
Finally a good presenter of this topic...all the others speak unprofessionally in a way that is hard to follow. Excellent coverage, thanks!
Cleanest video yet that I've watched on Lua OOP.
Very happy to hear that, thank you! :)
Best tutorial I've found in my 1 year of coding in lua you are the best at explaining it! props to you my man.
Wow, thanks!
As always, a very easy to understand video thats edited well. Making it super useful for average experienced Lua users like me. Thank you!
That's awesome to hear! :)
better than any of those roblox lua tutorials out right now. thank you!
Cool, I actually didn’t know about the overshadowing thing.
Thank you for making this video.
Happy to help! ☜(゚ヮ゚☜)
Great thing about lua is you can make objects with only as much overhead as they really need. If you need inheritance, you add it in, if not, you can just leave it out. You're not stuck with a class feature that always brings all the baggage with it.
Pretty nice video. I've been using metatables on an intermediate level for a few years, but I have to admit some stuff still confuses me. This helped me visualize things better and reorganize my thoughts. If you do more videos about it I'll be watching them. :)
Thanks.
Oh yea, I agree 100%, I love the “do it yourself” approach that Lua has. Happy to hear that you liked the video as well :)
Good grief, You are awesome. Make some more OOP tutorials I would love to watch them.
Thank you! Will for sure make more OOP related videos :)
Omg you don’t know how much this helped me
Been looking for something this easy for a long time...
What an absolutely amazing tutorial!!! Made it super easy to understand!
Glad you liked it! :D
That was the best example and explanation I have seen. Thanks
Awesome to hear, thank you! :)
Thank you for this easy to understand video, this really help me.
Nice video. I looked at some material on how to achieve this OOP-like behavior, but the examples made looked kinda complicated and scary. This approach seems really simple, even if the end result is the same as the exampled I've seen.
Great to hear! The goal was to try to make it as easy to understand as possible, because it can really seem complicated when you don't know how it works :)
Very clear explanation. Thank you
Very helpful tutorial. Thank you!
Awesome videos bro but too difficult to watch it in mobile devices because of too small fonts in the editor.
Lua is really nice!
It's an amazing language, I personally really like how it balances simplicity and power :)
Another hot video! Keep 'em coming :D
Thanks, Will do! ☜(゚ヮ゚☜)
Nice nice tutorial
Thanks! :)
Thank you so much!
Animations are awesome
Ok, I still don't understand why do you need the __index line that sets a metatable which is what setmetatable does too?
Animal = {}
Fish = {}
Animal.__index = Animal
setmetatable(Fish, Animal)
Why setmetatable is not enough on it's own? Like this:
Animal = {}
Fish = {} setmetatable(Fish, Animal)
Or, alternatively?
Animal = {}
Fish = {}
Fish.__index = Animal
This is because a "metatable" can include a lot of different Metamethods, not only the __index. One could for example only want to set the __add, __sub, __div and __mul metamethods, which are used for operator overloading.
So setmetatable(), sets a table to be the metatable. __index does not set a metatable, all it does is provide a location to search for a key.
So in short: A metatable can contain a variety of different metamethods. It's used for a lot of other things apart from emulating "inheritance" as shown in this video. __index is a specific metamethod which is used to tell where the program is supposed to search for a key that it initially failed to find.
I intend to cover other metamethods in the near future :)
@@DevJeeper From the above explanation I conclude that:
1) the "Fish.__index = Animal" should not work (as it does not include metaeverything, only __index) - or at least not work fully (just for the keys),
2) but setmetatable() used alone should be perfectly enough, because it sets metatable and metatable includes many metamethods (not only (but also?!) __index). That's what you wrote. :)
PS. If the remark about next videos was a subscription bait then it worked. ;)
@@erykczajkowski8226 Almost!
1. is sort of correct, simply setting the index without assigning the table as a metatable, does not accomplish much.
2. Using setmetatable with a table which does not have an .__index is like giving a blank map. You got a map but no instruction on where to go. This is why you need to both set the .__index (add instructions) and call setmetatable (hand over the map).
What I meant is that a metatable CAN have multiple metamethods, but you need to add them. Does that make sense?
The remark about the video was more to let you know that I agree that this is a topic which could use further videos and that I am working on it, but I appreciate the sub :)
@@DevJeeper My understanding of metatable is that is a kind of prototype and therefore it should have everything on it available if you set it. I see no sense (from logical perspective) in splitting it to separate operations if each one achieves completely nothing on its own. It sounds like Lua is really poorly designed? So what will setmetatable() without __index behave like and what will setting __index without setmetatable() behave like?
@@erykczajkowski8226
"My understanding of metatable is that is a kind of prototype and therefore it should have everything on it available if you set it."
That's not correct, let me try to explain it;
there are multiple different metamethods. __index is only one of them. So the simple reason to why you need to set __index is that you have cases where you do not wish to set it.
setmetatable() simply assigns a table to be the metatable of another table.
" It sounds like Lua is really poorly designed?"
Lua's core design principle is to allow you to decide what and how you implement things. Personally I really like that approach, but it's not for everyone.
"So what will setmetatable() without __index behave like "
Depends on what other metamethods you have set. If you haven't set anything, then it wont do anything.
"what will setting __index without setmetatable() behave like?"
It will be as useful as a variable which you declared, but never used.
Underrated
This comment is underrated! ;D
Does this mean that I have to re-assign any properties to the children classes that the parent class does not have? (since the child classes seem to only inherent methods, and not properties)?
How would I dynamically reference animal1, animal2, animal3, etc in your example?
Like if I wanted to print the following:
animal1:displayname
animal1:displayweight
animal1:displayspeed
animal2:displayname
animal2:displayweight
animal2:displayspeed
etc...
How is that animal number made dynamic?
You could have a table for example:
local animals = {}
Then you can do:
table.insert(animals, Animal.new())
The first animal will be inserted in animal[1], the second animal[2], etc
And how do we call a parent method in an overloaded child method?
Hi, Thanks for this video. I like how Lua handles OOP. I would like to know how difficult is to use win32 api functions in Lua. Is it easy like ctypes in Python ?
Good video, but the font size is too small. 😕
Hello, I hope someone can help me with Lua: I am writing split() function that uses following gmatch pattern %f[%S].-%f[%s]. But this only matches words surrounded with spaces, IMO this should also include first and the last word in the string. Can you explain why this is not working?
Out of curiosity, what's your keyboard layout size?
I want to know how much I can shave off the 100% as a programmer.
I personally prefer using a 100% keyboard when I program, the more I can use the keyboard to do stuff and the less I need the mouse; the faster I code. I have a 60% keyboard for games though :)
@@DevJeeper
Thanks! I guess the Inner Conflict between the Gamer vs. Programmer/Productivity Sides is actually real.
I'm going with my plan then:
TKL + Numpad
I recently want to delve into the Small Keyboard thing and I think this is a good place to start.
Haha, yeah I know the struggle :)
Do you have any specific keyboard in mind?
@@DevJeeper
Tecware Phantom 87
I don't have much info about Mech Numpads, though. Just a budget hot-swap would be enough.
Why don't we have to do this in Love 2d?
Man I hate lua, but this was a good tutorial :)
What if i need more arguements? Ive tried but failed
Could you elaborate? What’s preventing you from adding additional arguments?
Pretty good tutorial but I have a question. Could you tell how to create a class like yours and use them in the other lua files?
All you need to do is use “require”: www.tutorialspoint.com/how-to-use-the-require-function-in-lua-programming
@@DevJeeper Thank you so much :)
Only tu ensure that is a problem of a my editor. How do you separate all this classes in their separate file to not have all the potential code in a single file?
You simply make a new lua file (for example by creating a new text file and changing the extension to lua). Then you call require("filename") :)
@@DevJeeper I know xd. But when I require this file the intellisence dont work so I thought that I make something wrong. Thank you
In what way does it not work?
@@DevJeeper I return a table and I require it. My editor dont show me the fields and functions that table contains
@@4strodev But apart from your editor not showing it, does the code run as expected?
IF that is the case, then yes the problem is with your editor :)
How to save the game with metatables?
Metatables should not change how you save your game, as you would keep the "data" in the instance (which is just a normal table). I will cover it in more depth in a future video ☜(゚ヮ゚☜)
@@DevJeeper For example, how to save the fish1 from 5:45?
@@darkfrei2 The basic concept is that you need to "serialize" your table, and then write it to file. I promise to cover this more in depth in a video! But feel free to ask any questions regarding it now :)
Can you increase your font size please
That is a change which has been done in the latest videos :)
Bro i cant see shit
i see the 99% area of your video is BLACK, what a waste of screen ... could you please zoom/enlarge the fonts? , with low resolution/bandwidth characters looks just like tiny blurred pixels
thanks