<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Maratis forum - Make Lua Classes and Libraries(Tutorial)]]></title>
	<link rel="self" href="http://forum.maratis3d.com/extern.php?action=feed&amp;tid=841&amp;type=atom"/>
	<updated>2013-10-19T17:54:49Z</updated>
	<generator>PunBB</generator>
	<id>http://forum.maratis3d.com/viewtopic.php?id=841</id>
		<entry>
			<title type="html"><![CDATA[Make Lua Classes and Libraries(Tutorial)]]></title>
			<link rel="alternate" href="http://forum.maratis3d.com/viewtopic.php?pid=5611#p5611"/>
			<content type="html"><![CDATA[<p>Did you know that you can use more than one script at the same time using the dofile() function?<br /><a href="http://wiki.maratis3d.org/index.php?title=Dofile">http://wiki.maratis3d.org/index.php?title=Dofile</a></p><p>Did you know also that you can fake Object Oriented Programming in Lua?<br /><a href="http://forum.maratis3d.com/viewtopic.php?id=839">http://forum.maratis3d.com/viewtopic.php?id=839</a></p><p>In this post, I will be documenting how I will use these two things to create a class library that you can import into any game:</p><p>The first thing I do is figure out what will be the names of my classes. I also comment on what the classes are used for. <br /></p><h5>CLASSES</h5><div class="codebox"><pre><code>function newAnimation()
function newCutScene()
function newCharacter()
function newProp()
function newMenu()
function newLevel()
function newSound()
function newLight()
function newMotion()
function newCloth()
function newSense()
function newCamera()</code></pre></div><p>The next thing I do is make some psuedo-code to show how it can be used.<br /></p><h5>IMPLEMENTATION</h5><div class="codebox"><pre><code>cut_scene = newCutScene()
cut_scene:Commence()
cut_scene:Conclude()

level1 = newLevel()
level1:Launch()
level1:Finish()

prop1 = newProp()
prop1:Build(couch)
prop1:Destroy()

menu1 = newMenu()
menu1:Start()
menu1:End()

light1 = newLight()
light1:On()
light1:Off()

sound1 = newSound()
sound1:Play()
sound1:Stop()

animation1 = newAnimation()
animation1:Begin()
animation1:End()

character1 = newCharacter()
character1:Live()
character1:Die()

camera1 = newCamera()
camera1:Roll()
camera1:Cut()</code></pre></div><p>Notice how I used SYNONYMS and ANTONYMS to distinguish the class functions from one another. This is my Class Template:<br /></p><h5>TEMPLATE</h5><div class="codebox"><pre><code>--[[Class Template
    function Person(name,age)
            local object = {}
            
            object.name = name
            object.age = age
            
            function object:sayName()
                print (object.name)
            end    
                
            return object
        end

--Create Joe
local joe = Person(&quot;Joe&quot;,24)

--Create Bill
local bill = Person(&quot;Bill&quot;,25)

--Print Joe&#039;s name
print (joe.name)

--Make Bill say his name
bill:sayName()]]</code></pre></div><p>I am going to use this template to turn these classes into actual classes. But first, I am going to dig through the Maratis API to see what Maratis functions I can use to make these classes work like their title implies:<br />API<br /></p><div class="codebox"><pre><code>--CUTSCENE API
--changeCurrentCamera(object)
--getCurrentCamera()
--getCameraClearColor(object)
--getCameraFov(object)
--getCameraNear(object)
--getCameraFar(object)
--getCameraFogDistance(object)
--isCameraOrtho(object)
--isCameraFogEnabled(object)
--setCameraClearColor(object, {r, g, b})
--setCameraFov(object, fov)
--setCameraNear(object, near)
--setCameraFar(object, far)
--setCameraFogDistance(object, fogDistance)
--enableCameraOrtho(object, ortho)
--enableCameraFog(object, fog)
--enableCameraLayer(object, scene)
--disableCameraLayer(object)
--enableRenderToTexture(object, &quot;textureName&quot;, renderWith, renderHeight)
--disableRenderToTexture(object)
--***getProjectedPoint(object, point)
--***getUnProjectedPoint(object, point)
--getScene(&quot;sceneName&quot;)
--getScenesNumber()

--ANIMATION API
--changeAnimation(object, animationId)
--setAnimationSpeed(object,speed)

--PROP API
--vec3(x, y, z)
--getObject(« objectName »)
--getParent(object)
--getChilds(object)
--getClone(object)
--setParent(object, parent)
--getName(object)
--activate(object)
--deactivate(object)
--isVisible(object)
--isActive(object)
--rotate(object, {x, y, z}, angle, &quot;local&quot;)
--translate(object, {x, y, z}, &quot;local&quot;)
--getPosition(object)
--getRotation(object)
--getScale(object)
--setPosition(object, {x, y, z})
--setRotation(object, {x, y, z})
--setScale(object, {x, y, z})

--MENU API
--setText(object , &quot;text&quot;)
--getText(object)
--getTextColor(object)
--setTextColor(object, {r, g, b, a})
--getScene(&quot;sceneName&quot;)
--changeScene(scene)
--getCurrentSceneId()
--getScenesNumber()

--LEVEL API
--loadLevel(&quot;levels/myLevel.level&quot;)

--CHARACTER API (SubClass of Animation class?)
--vec3(x, y, z)
--getObject(« objectName »)
--changeAnimation(object, animationId)
--activate(object)
--deactivate(object)
--isVisible(object)
--isActive(object)

--LIGHT API
--getLightColor(object)
--getLightRadius(object)
--getLightIntensity(object)
--getLightShadowQuality(object)
--getLightShadowBias(object)
--getLightShadowBlur(object)
--getLightSpotExponent(object)
--setLightColor(object, {r, g, b})
--setLightRadius(object, radius)
--setLightIntensity(object, intensity)
--setLightShadowQuality(object, quality)
--setLightShadowBias(object, bias)
--setLightShadowBlur(object, blur)
--setLightSpotAngle(object, spotAngle)
--setLightSpotExponent(object, exponent)

--CAMERA API
--changeCurrentCamera(object)
--getCurrentCamera()
--getCameraClearColor(object)
--getCameraFov(object)
--getCameraNear(object)
--getCameraFar(object)
--getCameraFogDistance(object)
--isCameraOrtho(object)
--isCameraFogEnabled(object)
--setCameraClearColor(object, {r, g, b})
--setCameraFov(object, fov)
--setCameraNear(object, near)
--setCameraFar(object, far)
--setCameraFogDistance(object, fogDistance)
--enableCameraOrtho(object, ortho)
--enableCameraFog(object, fog)
--enableCameraLayer(object, scene)
--disableCameraLayer(object)
--enableRenderToTexture(object, &quot;textureName&quot;, renderWith, renderHeight)
--disableRenderToTexture(object)
--getProjectedPoint(object, point)
--getUnProjectedPoint(object, point)</code></pre></div><p>Now I am going to do one class at a time. I will do the most useful one first (refer to the template and API list) and fill it in with the neccessary API functions:</p><h5>CREATE A CLASS</h5><div class="codebox"><pre><code>--Prop Class
    function newProp(x)
        local object = {}
        
        --Variables:
        prop = getClone(x)
        object.name = getName(prop)
        object.location = getPosition(prop)
        scale = 1
        object.scale = scale
        
        
        
        --Methods:
        function object:Build()
            activate(prop)
        end
        
        function object:Destroy()
            deactivate(prop)
        end
        
        function object:Scale(scale)
            setScale(prop, {scale, scale, scale})
        end
        
        return object
    end</code></pre></div><div class="codebox"><pre><code>chair = getObject(&quot;Chair&quot;)

chair1 = newProp(chair)
chair1.Build()
chair1.Destroy()
chair1.Scale(2)</code></pre></div><p>Now I need to test this class out on an object...</p><div class="codebox"><pre><code>--Prop Class
    function newProp(x)
        local object = {}
        
        --Variables:
        prop = getClone(x)
        object.name = getName(prop)
        object.location = getPosition(prop)
        scale = 1
        object.scale = scale
        
        
        
        --Methods:
        function object:Build()
            activate(prop)
        end
        
        function object:Destroy()
            deactivate(prop)
        end
        
        function object:Scale(scale)
            setScale(prop, {scale, scale, scale})
        end
        
        return object
    end
    
monkey = getObject(&quot;Monkey&quot;)
deactivate(monkey)

monkey1 = newProp(monkey)
monkey1:Build()
--monkey1:Destroy()
monkey1:Scale(2)</code></pre></div><h5>IT WORKS!</h5><p>I named this file PropsClass.lua and put it in my scripts folder.<br />I created another empty file called Game.lua and put it in the same scripts folder.</p><p>If I want to use the class I wrote in the PropClass.lua file I just do it this way:</p><div class="codebox"><pre><code> dofile(&quot;PropClass.lua&quot;)</code></pre></div><p>Then I can use it in the Game.lua file like this:</p><div class="codebox"><pre><code> 
 dofile(&quot;PropClass.lua&quot;)

monkey = getObject(&quot;Monkey&quot;)
deactivate(monkey)

monkey1 = newProp(monkey)
monkey1:Build()
monkey1:Destroy()
monkey1:Scale(1)</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Tutorial Doctor]]></name>
				<uri>http://forum.maratis3d.com/profile.php?id=2493</uri>
			</author>
			<updated>2013-10-19T17:54:49Z</updated>
			<id>http://forum.maratis3d.com/viewtopic.php?pid=5611#p5611</id>
		</entry>
</feed>
