OOP, External Modules, and Display Object Parameters

Hi! I’m not really sure where to post this, but here it goes…

I’m using Jon Beebe’s owl.lua to create classes for some of the display objects in my game because I need to listen for changes in object properties. I finally got it to work!

 

Data for each level is in a json file.

 

Each class is a separate lua (class_pen.lua)

 

The instance creation function is in gameobjects.lua

 

The level scene invokes gObj.getPen() to create the instance

Problem: I don’t want the display object parameters to be hard-coded in the code. I can, since most of these used as control points and are not visible to the user. However, I would like to use the json file to read the parameters for the creation of the display object. 

If I put a json.decode in each of the classes (there will be <10, probably only 5) plus the level scene to get data, will it have a significant impact on game performance? If this is the way to go, I will separate the static game data into single json, separate from the level ones.

 

I’ve tried several different scenarios trying to get the parameters to pass from the level scene, but am not really sure that is even viable. 

I’ve already developed the basic game without classes. However, I need more flexibility and to fix some timing issues. I also want to cut down on the number of runtime event listeners. owl seemed like a possible solution. Below is the pertinent code.

owl.lua

 https://github.com/pickerel/owl (I had to change params.custom to params.createFunction in get_display_object() to get it to work with non-image display objects.)

class_pen.lua

local owl = require "scripts.level-000.owl" -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*^ -- Functions -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*^ -- Create Object -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* local function newPen() local newGamePen = display.newCircle(50, 50, 10) -- Want these to be variables newGamePen.alpha = 1 -- newGamePen:setFillColor( 255, 236, 79 ) newGamePen:setStrokeColor( 10, 10, 10 ) return newGamePen end --\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*^ -- Create Class -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*^ local Pen = owl.class{ name="Pen", createFunction=newPen } return Pen

gameobject.lua

local owl = require "scripts.level-000.owl" require "scripts.level-000.class\_pen" local gOb = {} -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*^ -- Pen -- \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*^ function penChangeX(self, event ) print(event.target.name .. ": " .. "x=" .. event.value) self[event.key] = event.value end function penChangeY(self, event ) print(event.target.name .. ": " .. "y=" .. event.value) self[event.key] = event.value end function gOb.getPen() local i\_pen = owl.instance{ from="Pen" } i\_pen:add\_property\_callback("x", penChangeX) i\_pen:add\_property\_callback("y", penChangeY) return i\_pen end return gOb

level00X.lua

 local pen = gObj.getPen() group:insert(pen.raw) transition.to(pen, {time=300, x=200, y=350}) -- To test property callback

Never mind… 

After doing a bunch of print statements, I discovered that the class is created before any statements are run on the scene page. Therefore it needs it’s own data feed or hard coding. 

Writing the issue out, walking away, then going back to a few hours later certainly helps in resolving problems.

Never mind… 

After doing a bunch of print statements, I discovered that the class is created before any statements are run on the scene page. Therefore it needs it’s own data feed or hard coding. 

Writing the issue out, walking away, then going back to a few hours later certainly helps in resolving problems.