Retrokin [Lexaloffle Blog Feed]https://www.lexaloffle.com/bbs/?uid=17251 Classes With Inheritance <p>Thought I would share some code I've been using for my pico-8 game. It's a simple class library that also includes inheritance.<br /> 139 Tokens<br /> 617 Characters without comments</p> <p>How to Use:<br /> Create a new instance with function &quot;new&quot;.<br /> The first variable is always the table that holds all the variables, 2nd and onwards are used for the function &quot;__init('table','...')&quot;<br /> Example: </p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>player:new({variablea=1,variableb=2},initarg1,initarg2)</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Variables are copied over just fine, however for tables if you don't want each instance to share the exact same table with each other then I would recommend defining them in the function &quot;__init('table','...')&quot;.<br /> Example:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--do this function newclass:__init(varc,vard) self.vartable={varc,vard} self.varemptytable={} end --if you don't do that then instance1.varemptytable==instance2.varemptytable</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>When creating a class, you can inherit from other classes upon it's creation by simply including classes after the first argument.<br /> Example:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>newclass=cwi:new({vara=3,varb=4},classtoinheritfroma,classtoinheritfromb)</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>You can also inherit from other classes by running function &quot;inherit('...')&quot;.</p> <p>Classes have an &quot;<strong>init&quot; function that is run every time a new instance is created.<br /> Classes that have been inherited from will NOT run their function &quot;</strong>init('...')&quot; upon creation of a new instance.</p> <p>Function &quot;oftype('class')&quot; allows you to check each if one class is of the type of another.<br /> Classes that have been inherited will also return true for function &quot;oftype('class')&quot;.</p> <p>Finally I present the code:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>--Classes With Inheritance! cwi={} cwi.__index=cwi function cwi:__init(...) self.__types={} if #{...}&gt;0 then self:inherit(...) end self.__init=function() end --create an empty function so all classes function &quot;__init&quot; doesn't inherit end function cwi:new(r,...) r=r or {} r.__index=r setmetatable(r,self) if r.__init then r:__init(...) end return r end function cwi:inherit(...) local function c(a,b) for k,l in pairs(b) do if not a[k] then a[k]=l end end end for l in all({...}) do c(self,l) --copy variables over to class c(self.__types,l.__types) --copy inheritance types over to class so that it knows it has inherited from more than class l self.__types[l]=true end end function cwi:oftype(s) if self==s or getmetatable(self)==s or self.__types[s] then return true end return false end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Let me know if this code helped out, if there's anything else I can improve upon it, or whatever else you may feel like saying.</p> https://www.lexaloffle.com/bbs/?tid=36166 https://www.lexaloffle.com/bbs/?tid=36166 Mon, 09 Dec 2019 05:55:25 UTC Having problems with #include <p>I've been trying to use the new (although not that new now) #include command but I can't seem to get it working.</p> <p>Code on pico-8 cart</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>#include testfile.lua test()</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>Code on testfile.lua</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>function test() print(&quot;test&quot;,100,0,8) end</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> <p>When I run I get the error:</p> <div> <div class=scrollable_with_touch style="width:100%; max-width:800px; overflow:auto; margin-bottom:12px"> <table style="width:100%" cellspacing=0 cellpadding=0> <tr><td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> <td background=/gfx/code_bg0.png> <div style="font-family : courier; color: #000000; display:absolute; padding-left:10px; padding-top:4px; padding-bottom:4px; "> <pre>testfile.lua line 3 syntax error line 1 (tab 0) endnd syntax error near `test`</pre></div></td> <td background=/gfx/code_bg1.png width=16><div style="width:16px;display:block"></div></td> </tr></table></div></div> https://www.lexaloffle.com/bbs/?tid=35925 https://www.lexaloffle.com/bbs/?tid=35925 Fri, 15 Nov 2019 12:25:58 UTC