Files
wiki/dst/QScript/Lua/Classes.html
2023-09-25 16:50:17 +02:00

432 lines
16 KiB
HTML

<!DOCTYPE html>
<title>SourceBox Wiki</title>
<script>
documentlist = ['/wiki/index.html', '/wiki/QScript/Introduction.html', '/wiki/QScript/Private_Members.html', '/wiki/QScript/Contributing/Contributing.html', '/wiki/QScript/Contributing/API/qscript.html', '/wiki/QScript/Contributing/API/Structs/QCFunc.html', '/wiki/QScript/Contributing/API/Structs/QModuleDefFunc.html', '/wiki/QScript/Contributing/API/Structs/QReturn.html', '/wiki/QScript/Contributing/API/Structs/QScriptArgs.html', '/wiki/QScript/Contributing/API/Structs/QScriptCallback.html', '/wiki/QScript/Contributing/API/Structs/QScriptClass.html', '/wiki/QScript/Contributing/API/Structs/QScriptClassCreator.html', '/wiki/QScript/Contributing/API/Structs/QType.html', '/wiki/QScript/Contributing/API/Structs/QValue.html', '/wiki/QScript/Lua/Classes.html', '/wiki/QScript/Lua/Exports.html', '/wiki/QScript/Lua/Imports.html', '/wiki/QScript/Lua/Intro.html', '/wiki/QScript/Lua/Objects.html', '/wiki/QScript/Squirrel/Exports_And_Imports.html', '/wiki/QScript/Squirrel/Intro.html', '/wiki/QScript/Tutorial/Chapter1.html', '/wiki/QScript/Tutorial/Chapter2.html'];
namelist = ['Wiki Intro', 'QScript Intro', 'Private Members', 'Contributing', 'QScript API', 'QCFunc', 'QModuleDefFunc', 'QReturn', 'QScriptArgs', 'QScriptCallback', 'QScriptClass', 'QScriptClassCreator', 'QType', 'QValue', 'Lua Classes', 'Lua Exports', 'Lua Imports', 'Lua Intro', 'Lua Objects', 'Squirrel Exports and Imports', 'Squirrel Intro', 'QScript Tutorial Page 1: Mods', 'QScript Tutorial Page 2: Getting to work'];
function toggleTree(element)
{
nested = element.parentNode.childNodes[3];
if(nested.className.indexOf("active",0) != -1)
{
nested.className = "sidebar nested";
element.parentNode.childNodes[0].innerHTML = "+";
}
else
{
nested.className = "sidebar nested active";
element.parentNode.childNodes[0].innerHTML = "-";
}
if(element.className.indexOf("active",0) != -1)
{
element.className = "spanactive"
}
else
{
element.className = "spanactive active"
}
}
function Press(element)
{
if(element.className.indexOf("active",0) != -1)
{
element.className = "spanactive active"
}
else
{
element.className = "spanactive"
}
}
function unPress(element)
{
if(element.className.indexOf("active",0) != -1)
{
element.className = "active"
}
else
{
element.className = ""
}
}
function updateSearch()
{
searchtext = document.getElementById("searchbox").value.toLowerCase();
var res = [];
var searchresults = document.getElementById("searchresults")
searchresults.innerHTML = ""
if(searchtext === "")
{
document.getElementById("filetree").style.display = "block"
return;
}
else
{
document.getElementById("filetree").style.display = "none"
}
for (var i = 0; i < namelist.length; i++)
{
if(namelist[i].toLowerCase().indexOf(searchtext) == -1) continue;
var searchelement = document.createElement("li");
searchelement.className = "sidebar"
var link = document.createElement("a");
searchelement.appendChild(link);
link.setAttribute("href", documentlist[i]);
link.innerHTML = namelist[i];
searchresults.appendChild(searchelement);
}
}
</script>
<link rel="stylesheet" href="/wiki/default.css">
<style>
hr
{
color: #463c2a;
}
pre code
{
margin-left:16px;
background-color: #473c29;
border-color: #3b3221;
border-style:solid;
border-width:1px;
padding: 4px;
display:flow-root;
}
code {
background-color: #00000040;
border-color: #00000040;
border-style:solid;
border-width:1px;
}
.nested
{
display: none;
}
ul.sidebar.active
{
display: block !important;
}
ul.sidebar
{
padding-left: 4px;
}
li.sidebar ::marker
{
color:#00000000;
display:none;
}
li.sidebar
{
list-style-type: none !important;
background-color:#4A3F2C;
border-bottom: #312A1D 1px solid;
border-right: #312A1D 1px solid;
border-left: 1px solid #8d8881;
border-top: 1px solid #8d8881;
padding-left: 2px;
padding-bottom:2px;
padding-top:2px;
margin-right:2px;
}
.buttoncontainer, .sidebar .buttoncontainer
{
border: black 1px solid;
border-left-color: white;
border-top-color: white;
display:inline-flex;
}
.sidebar span
{
-moz-user-select: none;
user-select: none;
-webkit-user-select: none;
z-index: 1;
cursor: pointer;
display: inline-block;
border: black 1px solid;
border-left-color: white;
border-top-color: white;
margin: 2px;
}
.sidebar span span
{
-moz-user-select: none;
user-select: none;
-webkit-user-select: none;
z-index: 0;
border: #463c2a 1px solid;
background-color: #63553b;
color: white;
border-left-color: #8d8881;
border-top-color: #8d8881;
margin: 0px;
padding-left: 4px;
padding-right: 4px;
}
.spanactive
{
border-left-color: black;
border-top-color: black;
border-right-color: white;
border-bottom-color: white;
}
span .spanactive
{
border: #463c2a 1px solid;
border-right-color: #8d8881;
border-bottom-color: #8d8881;
}
.liicon
{
display:inline-block;
width:12px;
height:100%;
font-family: monospace;
font-size: 16px;
color: white;
}
nav
{
background-color: #63553b;
height: 100%;
}
a
{
border: #463c2a 1px solid;
background-color: #63553b;
color: white;
border-left-color: #8d8881;
border-top-color: #8d8881;
padding-left: 4px;
padding-right: 4px;
padding-top:2px;
padding-bottom:2px;
}
a:visited
{
border: #463c2a 1px solid;
background-color: #63553b;
color: white;
border-left-color: #8d8881;
border-top-color: #8d8881;
padding-left: 4px;
padding-right: 4px;
padding-top:2px;
padding-bottom:2px;
}
body
{
background-color: #63553b;
color:white;
font-family:"Tahoma", sans-serif;
font-size:9pt;
}
.searchbox
{
background-color: #473c29;
border: #403624 1px solid;
border-bottom-color: #837f7a;
border-right-color: #837f7a;
color: white;
padding: 0px;
width:196px;
height:16px;
position: relative;
}
input:focus {
outline: none;
}
.searchboxcontainer
{
background-color: #473c29;
border: black 1px solid;
border-bottom-color: white;
border-right-color: white;
position:relative;
margin-left:auto;
margin-right:auto;
height:18px;
width:198px;
display: inline-block;
}
.sidebarcontainer
{
float:right;
display:block;
top: -16px;
right: -8px;
margin-left: 8px;
width:200px;
border-color: black;
border-width: 1px;
border-style:solid;
position:relative;
overflow:auto;
}
table {
border: #403624 1px solid;
border-top-color: #837f7a;
border-left-color: #837f7a;
background-color: #564A33;
}
th, td {
border: #00000040 1px solid;
border-right-color: #837f7a;
border-bottom-color: #837f7a;
background-color: #00000020;
}
</style>
<div>
<div class="sidebarcontainer">
<span class="searchboxcontainer"><input type="text" placeholder="search..." autocomplete="off" id="searchbox" oninput="updateSearch();" class="searchbox"></span>
<nav>
<ul class="sidebar" id="searchresults">
</ul>
<ul class="sidebar" id="filetree">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/index.html">Wiki Intro</a></span></li>
<li class="sidebar"><small class="liicon">-</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">QScript</span></span>
<ul class="sidebar nested active">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Introduction.html">QScript Intro</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Private_Members.html">Private Members</a></span></li>
<li class="sidebar"><small class="liicon">+</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">Contributing</span></span>
<ul class="sidebar nested">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/Contributing.html">Contributing</a></span></li>
<li class="sidebar"><small class="liicon">+</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">API</span></span>
<ul class="sidebar nested">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/qscript.html">QScript API</a></span></li>
<li class="sidebar"><small class="liicon">+</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">Structs</span></span>
<ul class="sidebar nested">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QCFunc.html">QCFunc</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QModuleDefFunc.html">QModuleDefFunc</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QReturn.html">QReturn</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QScriptArgs.html">QScriptArgs</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QScriptCallback.html">QScriptCallback</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QScriptClass.html">QScriptClass</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QScriptClassCreator.html">QScriptClassCreator</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QType.html">QType</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Contributing/API/Structs/QValue.html">QValue</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class="sidebar"><small class="liicon">-</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">Lua</span></span>
<ul class="sidebar nested active">
<li class="sidebar"><b>Lua Classes</b></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Lua/Exports.html">Lua Exports</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Lua/Imports.html">Lua Imports</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Lua/Intro.html">Lua Intro</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Lua/Objects.html">Lua Objects</a></span></li>
</ul>
</li>
<li class="sidebar"><small class="liicon">+</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">Python</span></span>
<ul class="sidebar nested">
</ul>
</li>
<li class="sidebar"><small class="liicon">+</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">Squirrel</span></span>
<ul class="sidebar nested">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Squirrel/Exports_And_Imports.html">Squirrel Exports and Imports</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Squirrel/Intro.html">Squirrel Intro</a></span></li>
</ul>
</li>
<li class="sidebar"><small class="liicon">+</small><span onmousedown="toggleTree(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);"><span onmousedown="Press(this);" onmouseleave="unPress(this);" onmouseup="unPress(this);">Tutorial</span></span>
<ul class="sidebar nested">
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Tutorial/Chapter1.html">QScript Tutorial Page 1: Mods</a></span></li>
<li class="sidebar"><span class="buttoncontainer"><a href="/wiki/QScript/Tutorial/Chapter2.html">QScript Tutorial Page 2: Getting to work</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
</div>
<h1>Lua Classes</h1>
<p>Let's start with an analogy. Imagine you want to make star-shaped cookies. You could of course, manually cut the baked cookie and then be left with a mess, or you could use a cookie cutter before baking it. In this analogy, the cookie is an object, the manual cutting method is using tables, and the cookie cutter is a class.</p>
<p>Before explaining what an object is, let's first take a look at how you even make a class.</p>
<p>To create a class, you first call the <code>class()</code> function like this:</p>
<div class="codehilite">
<pre><span></span><code><span class="n">my_class</span> <span class="o">=</span> <span class="n">class</span><span class="p">()</span> <span class="c1">-- Creates a class creator</span>
</code></pre>
</div>
<p><code>my_class</code> becomes a class creator. You can think of it like a bowl of ingredients which you can add to by simply setting the values of it.</p>
<div class="codehilite">
<pre><span></span><code><span class="n">my_class</span> <span class="o">=</span> <span class="n">class</span><span class="p">()</span> <span class="c1">-- Creates a class creator</span>
<span class="n">my_class</span><span class="p">.</span><span class="n">MyVar</span> <span class="o">=</span> <span class="mf">50.0</span> <span class="c1">-- Creates a variable inside the class</span>
<span class="kr">function</span> <span class="nc">my_class</span><span class="p">.</span><span class="nf">MyFunc</span><span class="p">(</span><span class="n">self</span><span class="p">,</span><span class="n">a</span><span class="p">)</span> <span class="c1">-- Creates a function inside the class</span>
<span class="kr">return</span> <span class="n">self</span><span class="p">.</span><span class="n">MyVar</span><span class="o">+</span><span class="n">a</span>
<span class="o">&lt;</span><span class="n">small</span> <span class="n">style</span><span class="o">=</span><span class="s2">&quot;position:relative; top:8px;&quot;</span><span class="o">&gt;</span><span class="kr">end</span><span class="o">&lt;/</span><span class="n">small</span><span class="o">&gt;</span>
</code></pre>
</div>
<p>Once you are done with defining your class, you call the <code>finish()</code> function like this:</p>
<div class="codehilite">
<pre><span></span><code><span class="n">finish</span><span class="p">(</span><span class="n">my_class</span><span class="p">)</span> <span class="c1">-- Finishes the class</span>
</code></pre>
</div>
<p><code>my_class</code> will turn into a normal class where you cannot add any more variables or functions to it. In other words, <code>my_class</code> will get "baked" and you cannot add any more ingredients to it.</p>
<p>The <code>class()</code> function accepts another class as a parameter which will "inherit" the other class.</p>
<p>In simple terms, every function and variable from the other class, will be copied into yours, without having to specify them all over again.</p>
<pre><code>another_class = class(my_class) -- Inherits from my_class
another_class.MyVar = 20.0 -- Changes the existing variable
function another_class.NewFunc(self,b) -- Makes a new function
return self.MyVar-b
end
finish(another_class) -- Finishes it
</code></pre>
<p>We can now move on to objects.</p>
<hr />
<p><span class="buttoncontainer"><a href="/wiki/QScript/Lua/Intro.html">&lt;- Prev</a></span> | <span class="buttoncontainer"><a href="/wiki/QScript/Lua/Objects.html">Next -></a></span></p>
</div>