Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]

OO and Inheritance
Started by marino.peter Dec 26 2018 12:51 AM

1 reply to this topic
[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Observer

  • 1 posts
  • Corona SDK

I am very new to Corona SDK and Lua, but I am a very experienced programmer in languages like; c/c++, javascript, php, asp, C# and many others.


I started Corona SDK by creating two very simple games "Simon Says" and "Memory Game". Which required very little Lua programming. This was very easy with Lua.


My next step was to learn how to do OO with inheritance which is needed if I am going to make bigger projects.... but here is where the nightmare begins. Lua is terrible for OO,.. it seems more like hacks then anything else. I keep stumbling onto limitation after limitation.


I tried to make a simple object that would inherit from display.newGroup

I would create a "hello world" text and insert it into my object (which I can't because of my limited knowledge how to create OO with Lua).


Here is a bad example:

GameTimer = { 

GameTimer.protoype = { 

GameTimer.mt = {}

function GameTimer:new()
	local timer_new = display.newGroup()

	setmetatable(timer_new, self)
	for k, v in pairs(GameTimer) do
		timer_new[k] = v

	timer_new.anchorChildren = true
	local text = display.newText({ 
		text = "10:00:00", 
		x = 0, 
		y = 0, 
		font = native.systemFont, 
		fontSize = 16,
	text.fill = { 1,1, 1,1 }

	return timer_new

apparently my setmetatable is incorrect, but I have no clue what do do? Lua has no natural programming pattern to follow, it all seems hardcoded to fit whatever.




[TOPIC: post.html]


[GLOBAL: userInfoPane.html]
  • Corona Geek

  • 1,276 posts
  • Enterprise

using setmetatable on any of Corona's display objects is tricky to say the least.  you can easily "decorate" display objects with additional methods and properties, and you can easily make prototype-based frameworks to manage it, but doing so via meta directly on display objects should be avoided by all but the bravest.  (Corona's docs even go so far as saying it can't be done at all, and claiming that display objects are exposed as raw userdata, but though that whole section is complete rubbish it's worth acting as if it were correct)


perhaps the simplest DIY approach is to "wrap" display objects with a simple table that CAN take a metatable, fe: (untested ottomh pseudocode just for concepts)

-- YourWrappedGroup.lua
local YourWrappedGroup = {
  new = function(self)
    local instance = setmetatable({},self)
    instance.view = display.newGroup()
    return instance
  destroy = function(self)
    self.view = nil
  debug = function(self)
    print("i am: ", self, "my display group is: ", self.view)
  -- want overrides for convenience? ok, but not DIRECTLY via meta on group.  fe:
  insert = function(self, dispobj)
YourWrappedGroup.__index = YourWrappedGroup
return YourWrappedGroup

-- main.lua
local YourWrappedGroup = require("YourWrappedGroup")
local wg1 = YourWrappedGroup:new()
local wg2 = YourWrappedGroup:new()
wg1.view:insert(display.newRect(...  -- use view directly
wg2:insert(display.newImage(... -- or via overridden

but... if you'll be doing a lot of this sort of stuff, and want something that looks/acts more formally OOP, then it's worth learning something like middleclass

  • roaminggamer likes this