Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

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

1 reply to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

marino.peter

[GLOBAL: userInfoPane.html]
marino.peter
  • 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
	end
	]]

	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 }
	timer_new:insert(text)

	return timer_new
end

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.

 

Anyone?

Peter



[TOPIC: post.html]
#2

davebollinger

[GLOBAL: userInfoPane.html]
davebollinger
  • Corona Geek

  • 1,348 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
  end,
  destroy = function(self)
    display.remove(self.view)
    self.view = nil
  end,
  debug = function(self)
    print("i am: ", self, "my display group is: ", self.view)
  end,
  -- want overrides for convenience? ok, but not DIRECTLY via meta on group.  fe:
  insert = function(self, dispobj)
    self.view:insert(dispobj)
  end,
}
YourWrappedGroup.__index = YourWrappedGroup
return YourWrappedGroup

-- main.lua
local YourWrappedGroup = require("YourWrappedGroup")
local wg1 = YourWrappedGroup:new()
local wg2 = YourWrappedGroup:new()
wg1:debug()
wg2:debug()
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




[topic_controls]
[/topic_controls]