Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

optimize OOP lua function
Started by carloscosta Aug 03 2018 08:14 AM

- - - - -
7 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 586 posts
  • Corona SDK

I my quest of moving my functions to OOP and at the same time using metatables, i have some doubts.

 

I will give you a simple example of a spinner function I just remade using OOP and metatables.

function menu.spinner()
	local _M = {x=display.contentWidth*.5, y=display.contentHeight*.5, width=128, height=128}
	local mt = { __index = _M }
	
	function _M.start(self)
		self.view:start()
	end
	
	function _M.stop(self)
		self.view:stop()
	end
	
	function _M.delete(self)
		display.remove(self.view)
                self.view=nil
		self=nil
	end
	
	local instance = {}
	function instance.new(params)
		local widget = require( "widget" )
		params=params or {}
		setmetatable(params, mt)
		params.view=widget.newSpinner( {x=params.x, y=params.y, width=params.width, height=params.height} )
		return params
	end
	return instance
end

to use you just need:

local spinnerClass=ui.spinner()
local spinnerInstance=spinnerClass.new()
spinnerInstance:start()
--spinnerInstance:stop()
--spinnerInstance:delete()

I've a lot of this simple functions that simplify my life building apps.

 

My question is. I'm I doing it right? is there a better version out there using OOP? if it was you how would you do it?

 

regards,

 

Carlos.


Edited by carloscosta, 03 August 2018 - 11:02 AM.


[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,921 posts
  • Corona SDK

Removed comment here because after I re-reviewed your post I came to another conclusion. See below.

[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,921 posts
  • Corona SDK

Actually, I broke that down to its fundamental parts and I don't understand why you don't just do this:
 
local widget = require( "widget" )
local spinner = widget.newSpinner( { ... whatever params you need } )
You have essentially wrapped the widget in a table that has functions which then call the widget functions. I don't see how this is optimal or what problem it is solving.


I think you're trying to apply OOP techniques where they are not needed.

The primary value of using OOP in games is to efficiently create complex hierarchical systems.

Just wrapping existing features in an OOP-ish table doesn't do anything except make the code fat and buggy.

Sorry if this isn't what you want to hear, but I'm still not understanding the goal here. :(

Edited by roaminggamer, 03 August 2018 - 10:21 AM.


[TOPIC: post.html]
#4

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,921 posts
  • Corona SDK

To be as clear as possible.

I would use OOP to create

  • a inventory system, or
  • a statemachine, or
  • a layering management system, or
  • a sound management system, or
  • a enemy hierachy, or
  • weapon system ...

I wouldn't use it to put thin wrappers around existing features.



[TOPIC: post.html]
#5

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 586 posts
  • Corona SDK

Actually, I broke that down to its fundamental parts and I don't understand why you don't just do this:
 

local widget = require( "widget" )
local spinner = widget.newSpinner( { ... whatever params you need } )
You have essentially wrapped the widget in a table that has functions which then call the widget functions. I don't see how this is optimal or what problem it is solving.


I think you're trying to apply OOP techniques where they are not needed.

The primary value of using OOP in games is to efficiently create complex hierarchical systems.

Just wrapping existing features in an OOP-ish table doesn't do anything except make the code fat and buggy.

Sorry if this isn't what you want to hear, but I'm still not understanding the goal here. :(

 

 

I'm just changing default values corona have to the ones I've. this is the worst case scenario, because in the other spinner functions, i've  a default file ready fo spin, the rotation value I want, etc. not the default values from corona.

and since I use all the same values, files across an app, and since i invoke them 100x times. its easier to call

 

local spinnerInstance=spinnerClass.new()

 

than

 

local spinner = widget.newSpinner( {x=display.contentWidth*.5, y=display.contentHeight*.5, width=128, height=128 } )

 

and like i told, this is the worst case scene with few variables. I've put this code to see if my OOP logic is correct, not if this code makes sense to exist or not.



[TOPIC: post.html]
#6

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 586 posts
  • Corona SDK

i've other methods in my other functions that complement what corona can't provide or do.

for exemple,

i've a download.networkRequest function

that have methods that can do:

download single file;

download table files;

download single file, trie N times, return default image if it fails to download it.

download table files, tries N times each file, return default on that file if it fails to download it

 

all i need to do is networkRequest ({{filename={"file1.jpg", "file2.jpg", "file3.jpg"}, url="https://www.somewebsite.com"})

 

it will return a table of those files with the correct name and path where they were saved, so i can proper create them locally. 



[TOPIC: post.html]
#7

carloscosta

[GLOBAL: userInfoPane.html]
carloscosta
  • Contributor

  • 586 posts
  • Corona SDK

To be as clear as possible.

I would use OOP to create

  • a inventory system, or
  • a statemachine, or
  • a layering management system, or
  • a sound management system, or
  • a enemy hierachy, or
  • weapon system ...

I wouldn't use it to put thin wrappers around existing features.

 

i've already build all my functions without OOP, just converting them to OOP to train, for that i need to know if i'm doing it right.

 

until now all my oop functions are about the same size or fewer lines but the bonus is that i can expand my functions now easier.



[TOPIC: post.html]
#8

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 6,921 posts
  • Corona SDK

I misunderstood your goal. I don't have any real suggestions except you don't need these two lines:

self.view=nil
self=nil

They don't have any effect.




[topic_controls]
[/topic_controls]