Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If later on you start needing inheritance (i.e. LaserCannon is a subclass of Weapon) you will probably need to use metatables more profoundly.</p> <p>There are lots of libraries that will allow you to do "oop on top of Lua". You can see a very good list here:</p> <p><a href="http://lua-users.org/wiki/ObjectOrientedProgramming" rel="noreferrer">http://lua-users.org/wiki/ObjectOrientedProgramming</a></p> <p>I'm the author of <a href="https://github.com/kikito/middleclass" rel="noreferrer">middleclass</a>. With my lib, you would have to do something like this:</p> <pre><code>local Weapon = class('Weapon') function Weapon:initialize(a,b,c) self.x,self.y,self.z = a,b,c end function Weapon:fire(x,y) ... end </code></pre> <p>LaserCannon would be easy to implement - you just pass a second parameter to class:</p> <pre><code>local LaserCannon = class('LaserCannon', Weapon) function LaserCannon:initialize(a,b,c,d) self.w = d Weapon.initialize(self, a,b,c) -- superclass' constructor end function LaserCannon:foo() ... end </code></pre> <p>You could use it like this:</p> <pre><code>require 'middleclass' -- so you can use "class" LaserCannon = require 'laser_cannon' local playerWeapon = LaserCannon:new() -- a laser local opponentWeapon = Weapon:new() -- a regular generic weapon opponentWeapon:fire(100,200) -- typical use playerWeapon:fire(100, 200) -- LaserCannon inherits fire from Weapon playerWeapon:foo() -- LaserCannon-exclusive </code></pre> <p>This is with middleclass, which is the one I prefer, since I made it. Other libraries on the page I mentioned before offer similar features.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload