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...

КОМЕНТАРІ • 86

  • @DevJeeper
    @DevJeeper  3 роки тому +10

    We now have a DISCORD server: discord.gg/mUaVASvG4z
    Thank you for watching, and hope to see you there :)

    • @changedev.198
      @changedev.198 3 роки тому +1

      Finally

    • @DevJeeper
      @DevJeeper  3 роки тому

      I think it's fair to say that your persistency had an effect ;)

  • @GBloxers
    @GBloxers Рік тому +13

    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!

    • @DevJeeper
      @DevJeeper  Рік тому

      Cheers! That's my main goal, that people learn something, not just memorize it :)

  • @5I6
    @5I6 Рік тому +3

    7 minute video
    I learn for half an hour
    But it was worth it

  • @rodrigofonseca1160
    @rodrigofonseca1160 8 місяців тому +2

    Finally a good presenter of this topic...all the others speak unprofessionally in a way that is hard to follow. Excellent coverage, thanks!

  • @billrice3307
    @billrice3307 3 роки тому +12

    Cleanest video yet that I've watched on Lua OOP.

    • @DevJeeper
      @DevJeeper  3 роки тому

      Very happy to hear that, thank you! :)

  • @wingledguzmano9476
    @wingledguzmano9476 Рік тому +4

    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.

  • @mungdahl7549
    @mungdahl7549 3 роки тому +14

    As always, a very easy to understand video thats edited well. Making it super useful for average experienced Lua users like me. Thank you!

    • @DevJeeper
      @DevJeeper  3 роки тому +1

      That's awesome to hear! :)

  • @susiebaka3388
    @susiebaka3388 Рік тому +1

    better than any of those roblox lua tutorials out right now. thank you!

  • @tmd2964
    @tmd2964 3 роки тому +4

    Cool, I actually didn’t know about the overshadowing thing.
    Thank you for making this video.

    • @DevJeeper
      @DevJeeper  3 роки тому +1

      Happy to help! ☜(゚ヮ゚☜)

  • @skaruts
    @skaruts 3 роки тому +6

    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.

    • @DevJeeper
      @DevJeeper  3 роки тому +2

      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 :)

  • @real_cdx
    @real_cdx 2 роки тому +3

    Good grief, You are awesome. Make some more OOP tutorials I would love to watch them.

    • @DevJeeper
      @DevJeeper  2 роки тому +1

      Thank you! Will for sure make more OOP related videos :)

  • @LuaPuzzle
    @LuaPuzzle Рік тому

    Omg you don’t know how much this helped me

  • @notcrispeeweevile1388
    @notcrispeeweevile1388 2 роки тому

    Been looking for something this easy for a long time...

  • @JAMingMayo
    @JAMingMayo 2 роки тому +1

    What an absolutely amazing tutorial!!! Made it super easy to understand!

  • @cmcdonough2
    @cmcdonough2 2 роки тому +1

    That was the best example and explanation I have seen. Thanks

    • @DevJeeper
      @DevJeeper  2 роки тому

      Awesome to hear, thank you! :)

  • @tendzin8207
    @tendzin8207 Рік тому +1

    Thank you for this easy to understand video, this really help me.

  • @DS-rd8ud
    @DS-rd8ud 3 роки тому +3

    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.

    • @DevJeeper
      @DevJeeper  3 роки тому +1

      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 :)

  • @PoppaDawwg
    @PoppaDawwg 10 місяців тому

    Very clear explanation. Thank you

  • @bretabel1642
    @bretabel1642 Рік тому

    Very helpful tutorial. Thank you!

  • @rameshrg22
    @rameshrg22 11 місяців тому +2

    Awesome videos bro but too difficult to watch it in mobile devices because of too small fonts in the editor.

  • @darkfrei2
    @darkfrei2 3 роки тому +4

    Lua is really nice!

    • @DevJeeper
      @DevJeeper  3 роки тому +2

      It's an amazing language, I personally really like how it balances simplicity and power :)

  • @ilikemorestuff
    @ilikemorestuff 3 роки тому +1

    Another hot video! Keep 'em coming :D

    • @DevJeeper
      @DevJeeper  3 роки тому

      Thanks, Will do! ☜(゚ヮ゚☜)

  • @j_sat
    @j_sat Рік тому +1

    Nice nice tutorial

  • @Gaimo_
    @Gaimo_ 2 роки тому +1

    Thank you so much!

  • @andriypostelzhuk9655
    @andriypostelzhuk9655 6 місяців тому

    Animations are awesome

  • @erykczajkowski8226
    @erykczajkowski8226 3 роки тому +4

    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

    • @DevJeeper
      @DevJeeper  3 роки тому

      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 :)

    • @erykczajkowski8226
      @erykczajkowski8226 3 роки тому +1

      ​@@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. ;)

    • @DevJeeper
      @DevJeeper  3 роки тому

      @@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 :)

    • @erykczajkowski8226
      @erykczajkowski8226 3 роки тому +1

      @@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?

    • @DevJeeper
      @DevJeeper  3 роки тому

      ​@@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.

  • @mrs8683
    @mrs8683 2 роки тому +1

    Underrated

    • @DevJeeper
      @DevJeeper  2 роки тому +1

      This comment is underrated! ;D

  • @seadrown6252
    @seadrown6252 Рік тому

    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)?

  • @taylor4379
    @taylor4379 Рік тому

    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?

    • @DevJeeper
      @DevJeeper  Рік тому

      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

  • @cprn.
    @cprn. 2 роки тому

    And how do we call a parent method in an overloaded child method?

  • @kcvinu
    @kcvinu Рік тому

    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 ?

  • @AlexTechie
    @AlexTechie 2 роки тому

    Good video, but the font size is too small. 😕

  • @anoname10
    @anoname10 2 роки тому

    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?

  • @thesphyrth
    @thesphyrth 3 роки тому +1

    Out of curiosity, what's your keyboard layout size?
    I want to know how much I can shave off the 100% as a programmer.

    • @DevJeeper
      @DevJeeper  3 роки тому +2

      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 :)

    • @thesphyrth
      @thesphyrth 3 роки тому +1

      @@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.

    • @DevJeeper
      @DevJeeper  3 роки тому

      Haha, yeah I know the struggle :)
      Do you have any specific keyboard in mind?

    • @thesphyrth
      @thesphyrth 3 роки тому

      @@DevJeeper
      Tecware Phantom 87
      I don't have much info about Mech Numpads, though. Just a budget hot-swap would be enough.

  • @fntr
    @fntr Рік тому

    Why don't we have to do this in Love 2d?

  • @pianochannel100
    @pianochannel100 2 місяці тому

    Man I hate lua, but this was a good tutorial :)

  • @skyklimes6108
    @skyklimes6108 Рік тому

    What if i need more arguements? Ive tried but failed

    • @DevJeeper
      @DevJeeper  Рік тому

      Could you elaborate? What’s preventing you from adding additional arguments?

  • @carl4701
    @carl4701 2 роки тому

    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?

    • @DevJeeper
      @DevJeeper  2 роки тому +1

      All you need to do is use “require”: www.tutorialspoint.com/how-to-use-the-require-function-in-lua-programming

    • @carl4701
      @carl4701 2 роки тому +1

      @@DevJeeper Thank you so much :)

  • @4strodev
    @4strodev 2 роки тому

    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?

    • @DevJeeper
      @DevJeeper  2 роки тому

      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") :)

    • @4strodev
      @4strodev 2 роки тому

      @@DevJeeper I know xd. But when I require this file the intellisence dont work so I thought that I make something wrong. Thank you

    • @DevJeeper
      @DevJeeper  2 роки тому

      In what way does it not work?

    • @4strodev
      @4strodev 2 роки тому

      @@DevJeeper I return a table and I require it. My editor dont show me the fields and functions that table contains

    • @DevJeeper
      @DevJeeper  2 роки тому

      @@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 :)

  • @darkfrei2
    @darkfrei2 3 роки тому +1

    How to save the game with metatables?

    • @DevJeeper
      @DevJeeper  3 роки тому

      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 ☜(゚ヮ゚☜)

    • @darkfrei2
      @darkfrei2 3 роки тому +1

      @@DevJeeper For example, how to save the fish1 from 5:45?

    • @DevJeeper
      @DevJeeper  3 роки тому

      @@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 :)

  • @hrushikeshpote2046
    @hrushikeshpote2046 2 роки тому

    Can you increase your font size please

    • @DevJeeper
      @DevJeeper  2 роки тому

      That is a change which has been done in the latest videos :)

  • @MGamerGames
    @MGamerGames 2 роки тому +1

    Bro i cant see shit

  • @atesin_dj
    @atesin_dj 11 місяців тому

    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