Class, Method & Properties

i have a main.lua and a class file and i manage to call the class:method, but how to read/write the property?

main.lua

-- calling class, package  
  
local player = require("CLSPlayer")  
player:callme()  

CLSPlayer.lua

module(..., package.seeall)  
--[[  
 PLAYER CLASS  
  
 PROPERTIES  
 METHOD  
]]  
-- local properties  
local myProp = 777  
  
-- local functions must initiate before call  
local function localCall()  
 print('in CLSPlayer ... localCall function')  
end  
function CLSPlayer:callme()  
  
 print('calling CLSPlayer:callme()')  
  
 localCall()  
end  

how can the main.lua read/write ‘myProp’?
Thanks for the advice [import]uid: 10373 topic_id: 7759 reply_id: 307759[/import]

you have 2 choices.

  1. make myProp *not* local and then just call [lua]player.myProp = 999[/lua]

or

  1. create a [lua]function CLSPlayer:setMyProp(val) myProp=val end[/lua] in your module and then use [lua]player:setMyProp(999)[/lua]

in other languages this would be known as a setter for a private variable (and because they are functions you can run logic in them before actually setting the value*)

you might also want a getter [lua]CLSPlayer:getMyProp() return myProp end[/lua]

(* note in flash a setter would actually be called like player.myProp = 3 which runs function get myProp(val:\*){ \_myProp=val }, but this isnt quite the same in Lua)

[import]uid: 6645 topic_id: 7759 reply_id: 27493[/import]

Thanks pal! [import]uid: 10373 topic_id: 7759 reply_id: 27526[/import]

i use approach 1

main.lua

-- calling class, package  
  
local p = require('playerClass')  
local p2 = require('playerClass')  
  
print('string ' .. p.maxHP .. ',' .. p2.maxHP)  
p2.maxHP = 11  
p.maxHP = 22  
print('string ' .. p.maxHP .. ',' .. p2.maxHP)  
  
print('end me')  

playerClass.lua

module(..., package.seeall)  
  
 player = 0  
 character = ''  
 moveSpeed = 3  
 maxHP = 10  
 currentHP = 10  
 maxSP = 10  
 currentSP = 10  

terminal output

string 10,10  
string 22,22  
end me  

it’s weird my variable override each other. but what i am expected is two instance from playerClass. How to modify it? [import]uid: 10373 topic_id: 7759 reply_id: 27544[/import]

Yes because maxHP etc is a property of the module not your instances, that is why it is getting overridden . Think of playerClass as your factory module or “mould” for player instances . For your example you need to implement a constructor that returns an instance with it’s own properties [import]uid: 6645 topic_id: 7759 reply_id: 27557[/import]

on a simple level…
[lua]module(…, package.seeall)

function new()
local player = {}
player.maxHP=100
return player
end[/lua]

[lua]local playerClass=require(“playerClass”)
local p1=playerClass.new()
local p2=playerClass.new()
p1.maxHP=30[/lua]

but you should look into metatables [import]uid: 6645 topic_id: 7759 reply_id: 27560[/import]

Guess this doesn’t really OO approach.
Coz i tried to create another method in playerClass call ‘dead’

But the ‘maxHP’ doesn’t read-able in ‘dead’ method

playerClass.lua

  
function new()  
 player.maxHP=100  
 return player  
end  
function dead()  
 print('Player is dead, HP = ' .. self.maxHP)  
end  

perhaps is there any tutorial available related to OOP in Corona site? [import]uid: 10373 topic_id: 7759 reply_id: 27637[/import]

hi that’s because you’re not passing “self” in, which happens automatically when you call

[lua]p1:dead() – the same as p1.dead(p1)[/lua]

playerClass.lua
[lua]module(…, package.seeall)
– create our main class table
playerClass={}

– prepare the metatable
mt = {__index = playerClass }
function new() – constructor

– create instance table
local player = {}

– your setup
player.maxHP=100
– …etc etc…

– basically this says the player instance
– should find it’s functions in the playerClass
setmetatable(player, mt)

– return the instance
return player

end

function playerClass:dead()
– “self” is passed in automatically,
– referring to the (p1) above
end[/lua]

look up metatables on the forum [import]uid: 6645 topic_id: 7759 reply_id: 27639[/import]