local xPosition -- Parent scope Var
function setPosition(xPosition, yPosition)
xPosition -- Access local var
-- How to access parent scope xPosition ?
end
If it isn't possible I think I should rename my arguments but are there any naming convention for this in lua ?
local DATA = {xPosition} -- Parent scope Var
function setPosition(xPosition, yPosition)
xPosition -- Access local var
DATA.xPosition -- Access parent scope
end
This can also be a better strategy overall, since you contain all your true global or up-value locals in a single table, which in theory you could serialise or save out relatively easy (as well as import back in).
A total self applied convention is that I create these tables in upper case to identify their scope importance.
If it isn't possible I think I should rename my arguments but are there any naming convention for this in lua ?
Might be possible using the debug module, but I DON'T recommend it - especially as an alternative to naming your variables.
Just don't reuse the same variable names across scopes - it's confusing and can lead to bugs.
One common practice I have noticed is to put an underscore before any variables in the parent scope:
@drunken_munki To me xPosition is an object private field and I feel more natural to put everything in separate vars. But maybe it could be done in a better way with a table named private or something like this. I have to think about it
local Object = {}
--[[
Game object is base for object updating each frame
@return table : new object
]]
function Object.new()
local self = {}
--[[
Publics vars
--------------------------------]]
self.foo = 'bar'
--[[
Locales vars
--------------------------------]]
-- Positions
local _xPosition = -1 -- X position on world
local _yPosition = -1 -- Y position on world
local _xOriginPosition = -1 -- X origin position on world
local _yOriginPosition = -1 -- Y origin position on world
--[[
Auto called methods
--------------------------------]]
--[[
Init
]]
-- function self.init()
-- end
--[[
Sleep
]]
-- function self.sleep()
-- end
--[[
Wake up
]]
-- function self.wakeUp()
-- end
--[[
Update
]]
-- function self.update()
-- end
--[[
Fixed update
]]
-- function self.fixedUpdate()
-- end
--[[
Draw
]]
-- function self.draw()
-- end
--[[
Methods
--------------------------------]]
--[[
Set position
@param int xPosition
@param int yPosition
]]
function self.setPosition(xPosition, yPosition)
_xPosition = xPosition
_yPosition = yPosition
end
--[[
Get position
@return table : {_xPosition, _yPosition}
]]
function self.getPosition()
return {_xPosition, _yPosition}
end
return self
end
return Object
I didn't think about a way to serialise objects. It sure could be useful.
In a way, they are. "_xPosition" cannot be accessed outside of that file so it's sort of an "internal" variable.
Your code looks fine, note that if you want to do OO using closures
you can just return a function
and you don't need to use self or the colon operator(":")
ivan wrote: ↑Sun Dec 17, 2017 3:51 pm
Your code looks fine, note that if you want to do OO using closures
you can just return a function
and you don't need to use self or the colon operator(":")
local Object = {}
--[[
Object definition
@return table : new Object
]]
function Object.new()
local self = require('script.gameobject').new() -- this is the previous code I posted above
--[[
Update
]]
function self.update()
print('update')
end
--[[
Draw
]]
function self.draw()
print('draw')
end
--[[
Draw
]]
function self.move()
print('move')
end
return self
end
return Object
Is this doable with the code you posted ?
And what id point in function(id) ?
And can you have multiple instance of an object ?
Sorry for that much questions
ivan wrote: ↑Sun Dec 17, 2017 3:51 pm
Don't do stuff like:
metatables are typically the way to go - if you plan to have inheritance.
Is this doable with the code you posted ?
There are different ways to program OO in Lua.
The examples we have posted so far are based on closures.
Inheritance is much easier to implement using metatables.
Ok, I changed my code with the closure you gave me and it look nicer.
I tried some example with metatable but to me it look over complicated. Also I think it better fit with real OOP (with multiple inheritance and stuff like that) but I m not sure for oriented prototype code ?
There is no "real" OOP in Lua, it all depends on what you are trying to do.
Closures are simple, fast and work best for long-lived objects.
If you want inheritance, then metatables are the way to go. It doesn't have to be complicated (see my super simple lib: https://github.com/2dengine/oo.lua )
For multiple inheritance, then you probably want something like mixins (see kikito's middleclass: https://github.com/kikito/middleclass )
Personally, I try to keep my code simple and clear, so I avoid OOP as much as possible.
Last edited by ivan on Wed Dec 15, 2021 11:31 am, edited 1 time in total.