Object Oriented Programming in Maratis

From MaratisWiki
(Difference between revisions)
Jump to: navigation, search
 
(2 intermediate revisions by one user not shown)
Line 1: Line 1:
 
Lua doesn't have a way to do Object Oriented Programming. But you can fake it. Here is how:
 
Lua doesn't have a way to do Object Oriented Programming. But you can fake it. Here is how:
  
==WHAT IS OBJECT ORIENTED PROGRAMMING?==
+
==What is object oriented programming?==
  
 
Object Oriented Programming(OOP) is taking variables and turning them into Objects. An Object is a variable that has TRAITS and ABILITIES. OOP is used to simulate real life objects. If we had a variable named:
 
Object Oriented Programming(OOP) is taking variables and turning them into Objects. An Object is a variable that has TRAITS and ABILITIES. OOP is used to simulate real life objects. If we had a variable named:
  
<pre>
+
  apple =
apple =
+
</pre>
+
  
 
An apple can have several traits. It has a TYPE and a COLOR and a TASTE. Apples don't have very many abilities except BeEaten().
 
An apple can have several traits. It has a TYPE and a COLOR and a TASTE. Apples don't have very many abilities except BeEaten().
 
OOP allows us to assign traits and abilities to your apple and can be used like this:
 
OOP allows us to assign traits and abilities to your apple and can be used like this:
  
<pre>
+
  print (apple.type)
print (apple.type)
+
  print (apple.color)
print (apple.color)
+
  print (apple.taste)
print (apple.taste)
+
  apple:BeEaten()
apple:BeEaten()
+
</pre>
+
  
 
Apples belong to the class of FRUIT, and that is why apples can have a taste and a color and a type. All objects can be CLASSFIED this way. All of the traits of an apple are inherited from the general traits of a fruit.  
 
Apples belong to the class of FRUIT, and that is why apples can have a taste and a color and a type. All objects can be CLASSFIED this way. All of the traits of an apple are inherited from the general traits of a fruit.  
Line 23: Line 19:
 
This is how you create a class and object in LUA:
 
This is how you create a class and object in LUA:
  
<pre>
+
  --New Player Class
--New Player Class
+
 
         function Player(name)
 
         function Player(name)
 
             local object = {}
 
             local object = {}
 
+
 
 
             --Traits
 
             --Traits
 
             object.name = name
 
             object.name = name
 
             object.hit= false
 
             object.hit= false
 
+
 
 
             txt_score = getObject("ScoreText")
 
             txt_score = getObject("ScoreText")
 
+
 
 
             --Abilities
 
             --Abilities
 
             function object:ShowHitStatus()
 
             function object:ShowHitStatus()
Line 41: Line 36:
 
             return object
 
             return object
 
         end
 
         end
 
+
 
--Create Joe
+
  --Create Joe
local joe = Player("Joe")
+
  local joe = Player("Joe")
 
+
 
--Create Bill
+
  --Create Bill
local bill = Player("Bill")
+
  local bill = Player("Bill")
 
+
 
--Print Joe's name
+
  --Print Joe's name
print (joe.name)
+
  print (joe.name)
 
+
 
--Show Bill's hit status
+
  --Show Bill's hit status
bill:ShowHitStatus()
+
  bill:ShowHitStatus()
</pre>
+
  
 
The PLAYER CLASS is just a function named Player(). What this function does is creates a table named OBJECT and then it returns the table.
 
The PLAYER CLASS is just a function named Player(). What this function does is creates a table named OBJECT and then it returns the table.
  
==HOW DOES RETURN WORK?==
+
==How does return work?==
  
 
Well, when you put an apple into a Blend() function, the blender returns a chopped up apple. But you don't have to always make a function return something. You can just have the blender Blend() and not give you back a chopped up apple. But most of the time when you put some fruit into a Blender, you want to be able to get the result (a nice smoothie perhaps?)
 
Well, when you put an apple into a Blend() function, the blender returns a chopped up apple. But you don't have to always make a function return something. You can just have the blender Blend() and not give you back a chopped up apple. But most of the time when you put some fruit into a Blender, you want to be able to get the result (a nice smoothie perhaps?)
Line 63: Line 57:
 
So what is actually occuring is that we are making the Player() function EQUAL TO "object" When you put an apple into the blender you get a chopped apple.  
 
So what is actually occuring is that we are making the Player() function EQUAL TO "object" When you put an apple into the blender you get a chopped apple.  
  
<pre>
+
  regular apple + Blend() function = chopped apple
regular apple + Blend() function = chopped apple
+
</pre>
+
  
 
(That isn't real code)
 
(That isn't real code)
Line 71: Line 63:
 
So, if Player() = object then when we type:
 
So, if Player() = object then when we type:
  
<pre>
+
  local joe = Player()
local joe = Player()
+
</pre>
+
  
 
We are saying that joe = object and if joe = object, then joe.name = object.name. Inside of the Player() funtion we made object.name = name. Then we took that "name" variable and made it an ARGUMENT of the Player() function.
 
We are saying that joe = object and if joe = object, then joe.name = object.name. Inside of the Player() funtion we made object.name = name. Then we took that "name" variable and made it an ARGUMENT of the Player() function.
  
==WHAT IS AN ARGUMENT?==  
+
==What is an argument?==
  
 
Going back the Blend() function, we can Blend() an apple like this:
 
Going back the Blend() function, we can Blend() an apple like this:
  
<pre>
+
  Blend(apple)
Blend(apple)
+
</pre>
+
  
 
Or we can blend an orange:
 
Or we can blend an orange:
  
<pre>
+
  Blend(orange)
Blend(orange)
+
</pre>
+
  
 
Or we can blend paper:
 
Or we can blend paper:
  
<pre>
+
  Blend(paper)
Blend(paper)
+
</pre>
+
  
 
The things we swtich in and out of the Blend() function are called arguments.  
 
The things we swtich in and out of the Blend() function are called arguments.  
Line 103: Line 87:
 
Example:
 
Example:
  
<pre>
+
  function Add(x,y)     
function Add(x,y)     
+
 
         result = x+y     
 
         result = x+y     
 
         return result
 
         return result
end
+
  end
 
+
 
Add(1,2)
+
  Add(1,2)
</pre>
+
  
 
The 1 get's plugged in where the x is. The 2 get's plugged in where the y is.
 
The 1 get's plugged in where the x is. The 2 get's plugged in where the y is.
  
<pre>
+
  Add(1,2)
Add(1,2)
+
</pre>
+
  
 
should return 3
 
should return 3
  
<pre>
+
  Add(978,562)
Add(978,562)
+
</pre>
+
  
 
should return 1540
 
should return 1540
Line 128: Line 106:
 
So, in our example of the Player() function, "name" is an argument, and whatever we plug in:
 
So, in our example of the Player() function, "name" is an argument, and whatever we plug in:
 
   
 
   
<pre>
+
  Player(_here_)  
Player(_here_)  
+
</pre>
+
  
 
gets put:
 
gets put:
  
<pre>
+
  function Player(_here_)
function Player(_here_)
+
 
         print(_here_)
 
         print(_here_)
 
         setText(Text0,_here_)
 
         setText(Text0,_here_)
end
+
  end
</pre>
+
  
 
and whatever/wherever else.
 
and whatever/wherever else.
Line 145: Line 119:
 
Here is the Class again:
 
Here is the Class again:
  
<pre>
+
  --New Player Class
--New Player Class
+
 
         function Player(name)
 
         function Player(name)
 
             local object = {}
 
             local object = {}
Line 160: Line 133:
 
             return object
 
             return object
 
         end
 
         end
 
+
 
--Create Joe
+
  --Create Joe
local joe = Player("Joe")
+
  local joe = Player("Joe")
 
+
 
--Create Bill
+
  --Create Bill
local bill = Player("Bill")
+
  local bill = Player("Bill")
 
+
 
--Print Joe's name
+
  --Print Joe's name
print (joe.name)
+
  print (joe.name)
 
+
 
--Show Bill's hit status
+
  --Show Bill's hit status
bill:ShowHitStatus()
+
  bill:ShowHitStatus()
</pre>
+
  
 
Functions work the same way. If bill = Player() and Player() = object (returns object) then object:ShowHitStatus = bill:ShowHitStatus.
 
Functions work the same way. If bill = Player() and Player() = object (returns object) then object:ShowHitStatus = bill:ShowHitStatus.
Line 178: Line 150:
 
Now that we have a PLAYER CLASS we can make any variable a PLAYER. And just by making them a PLAYER they will obtain the TRAITS and ABILITIES that every other PLAYER OBJECT has.  
 
Now that we have a PLAYER CLASS we can make any variable a PLAYER. And just by making them a PLAYER they will obtain the TRAITS and ABILITIES that every other PLAYER OBJECT has.  
  
<pre>
+
  sarah= Player("Sarah Williams")
sarah= Player("Sarah Williams")
+
  francais = Player("Francais Smith")
francais = Player("Francais Smith")
+
  margret = Player("Margret Sachel")
margret = Player("Margret Sachel")
+
</pre>
+
  
<pre>
+
  sarah:ShowHitStatus()
sarah:ShowHitStatus()
+
  francais:ShowHitStatus()
francais:ShowHitStatus()
+
  margret:ShowHitStatus()
margret:ShowHitStatus()
+
</pre>
+
  
 +
[[Category:User manual]]
 
[[Category:Lua scripting]]
 
[[Category:Lua scripting]]

Latest revision as of 16:01, 5 February 2014

Lua doesn't have a way to do Object Oriented Programming. But you can fake it. Here is how:

What is object oriented programming?

Object Oriented Programming(OOP) is taking variables and turning them into Objects. An Object is a variable that has TRAITS and ABILITIES. OOP is used to simulate real life objects. If we had a variable named:

 apple =

An apple can have several traits. It has a TYPE and a COLOR and a TASTE. Apples don't have very many abilities except BeEaten(). OOP allows us to assign traits and abilities to your apple and can be used like this:

 print (apple.type)
 print (apple.color)
 print (apple.taste)
 apple:BeEaten()

Apples belong to the class of FRUIT, and that is why apples can have a taste and a color and a type. All objects can be CLASSFIED this way. All of the traits of an apple are inherited from the general traits of a fruit.

This is how you create a class and object in LUA:

 --New Player Class
       function Player(name)
           local object = {}
 
           --Traits
           object.name = name
           object.hit= false
 
           txt_score = getObject("ScoreText")
 
           --Abilities
           function object:ShowHitStatus()
               setText (txt_score,object.name .. " got hit!")
           end    
               
           return object
       end
 
 --Create Joe
 local joe = Player("Joe")
 
 --Create Bill
 local bill = Player("Bill")
 
 --Print Joe's name
 print (joe.name)
 
 --Show Bill's hit status
 bill:ShowHitStatus()

The PLAYER CLASS is just a function named Player(). What this function does is creates a table named OBJECT and then it returns the table.

How does return work?

Well, when you put an apple into a Blend() function, the blender returns a chopped up apple. But you don't have to always make a function return something. You can just have the blender Blend() and not give you back a chopped up apple. But most of the time when you put some fruit into a Blender, you want to be able to get the result (a nice smoothie perhaps?)

So what is actually occuring is that we are making the Player() function EQUAL TO "object" When you put an apple into the blender you get a chopped apple.

 regular apple + Blend() function = chopped apple

(That isn't real code)

So, if Player() = object then when we type:

 local joe = Player()

We are saying that joe = object and if joe = object, then joe.name = object.name. Inside of the Player() funtion we made object.name = name. Then we took that "name" variable and made it an ARGUMENT of the Player() function.

What is an argument?

Going back the Blend() function, we can Blend() an apple like this:

 Blend(apple)

Or we can blend an orange:

 Blend(orange)

Or we can blend paper:

 Blend(paper)

The things we swtich in and out of the Blend() function are called arguments.

When you put an argument in the parentheisis of the function, it gets plugged in everywhere the argument is found in the function

Example:

 function Add(x,y)    
       result = x+y    
       return result
 end
 
 Add(1,2)

The 1 get's plugged in where the x is. The 2 get's plugged in where the y is.

 Add(1,2)

should return 3

 Add(978,562)

should return 1540

So, in our example of the Player() function, "name" is an argument, and whatever we plug in:

 Player(_here_) 

gets put:

 function Player(_here_)
       print(_here_)
       setText(Text0,_here_)
 end

and whatever/wherever else.

Here is the Class again:

 --New Player Class
       function Player(name)
           local object = {}
           
           object.name = name
           object.hit= false
           txt_score = getObject("ScoreText")
           
           function object:ShowHitStatus()
               setText (txt_score,object.name .. " got hit!")
           end    
               
           return object
       end
 
 --Create Joe
 local joe = Player("Joe")
 
 --Create Bill
 local bill = Player("Bill")
 
 --Print Joe's name
 print (joe.name)
 
 --Show Bill's hit status
 bill:ShowHitStatus()

Functions work the same way. If bill = Player() and Player() = object (returns object) then object:ShowHitStatus = bill:ShowHitStatus.

Now that we have a PLAYER CLASS we can make any variable a PLAYER. And just by making them a PLAYER they will obtain the TRAITS and ABILITIES that every other PLAYER OBJECT has.

 sarah= Player("Sarah Williams")
 francais = Player("Francais Smith")
 margret = Player("Margret Sachel")
 sarah:ShowHitStatus()
 francais:ShowHitStatus()
 margret:ShowHitStatus()
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox