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]
make myProp *not* local and then just call [lua]player.myProp = 999[/lua]
or
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)
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]