OOP variables

q)Does someone know of an example of OOP with Corona where I have a main class and player/enemy class. Just the framework is god enough.

Each class will be in different file.

I will try to make one myself.
[import]uid: 138547 topic_id: 24987 reply_id: 324987[/import]

I have made 2 classes and a main file. It is a console file for testing.

I confused about member variables as I have to set every variable with the prefix to self?
module(…, package.seeall)

player = {}

player.__index = player

function player:new(i)
local o = {};
o._i = i;
setmetatable(o,self)
self:loadvars();
return o;
end

function player:loadvars()

self._aa=3344;
self._bb=“iiiiii”;

end

function player:get_aa()
return self._aa;
end

function player:set_aa(bb)
self._aa=bb;

end
[import]uid: 138547 topic_id: 24987 reply_id: 101498[/import]

One way to do it:

[lua]-- main.lua

local Player = require(“Player”)
local Enemy = require(“Enemy”)

local hero = Player:new(“Sir Bob”)
local villain = Enemy:new(“Mr Boar”)

hero:speak()
villain:speak()
hero:attack(enemy)[/lua]

[lua]-- Player.lua

local Player = {}

function Player:new(name)
local player = {
name = name
}
function player:speak()
print(“av’ at you!”)
end
function player:attack(target)
print(self.name … " attacks " … target.name)
end

return player
end

return Player[/lua]

[lua]-- Enemy.lua

local Enemy = {}

function Enemy:new(name)
local enemy = {
name = name
}
function enemy:speak()
print(“snork snork…”)
end

return enemy
end

return Enemy[/lua]

check out http://www.ludicroussoftware.com/blog/2011/07/06/simple-oop-with-inheritance-in-corona/ for more info [import]uid: 118390 topic_id: 24987 reply_id: 101596[/import]

why is it needed to have nested functions in the classes?
I dont do this .

declaring member variables in the classes is done with self all the time? [import]uid: 138547 topic_id: 24987 reply_id: 101606[/import]

why is it needed to have nested functions in the classes?
I dont do this .

declaring member variables in the classes is done with self all the time? [import]uid: 138547 topic_id: 24987 reply_id: 101607[/import]

Its an alternate way to do OOP, without the need for metatables.

The metatable approach would look something like this:

[lua]-- Player.lua

local Player = {}
local mt = {__index = Player}

function Player:new(name)
local player = {
name = name
}

setmetatable(player, mt)

return player
end

function Player:speak()
print(“av’ at you!”)
end

function Player:attack(target)
print(self.name … " attacks " … target.name)
end

return Player[/lua]

declaring member variables in the classes is done with self all the time?

Yes, lua doesn’t has real Classes and OOP. So trying to do:

[lua]function Player:attack(target)
print(name … " attacks " … target.name)
end[/lua]

(note the missing self.name) is not possible.

This link explains the use of self quite well: http://lua-users.org/wiki/ObjectOrientationTutorial [import]uid: 118390 topic_id: 24987 reply_id: 101609[/import]

thanks [import]uid: 138547 topic_id: 24987 reply_id: 101610[/import]

I haven’t tried using it for any projects yet, but I saw this OOP framework: https://github.com/kikito/middleclass

Looks promising to me. [import]uid: 120 topic_id: 24987 reply_id: 101612[/import]

I’ve also heard good things about middleclass. Very useful and super easy, especially if you need Inheritance. [import]uid: 118390 topic_id: 24987 reply_id: 101619[/import]