Object Oriented Programming in Maratis

From MaratisWiki
Jump to: navigation, search

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