Thread Tools Display Modes
07/02/15, 10:21 AM   #1
kerb9729
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 56
ZO_Object

Hey guys,

New developer here. I've been looking at some existing addons, figuring out how they work as a learning exercise. Which brings me to my question:

I'm confused about ZO_Object. What is the benefit of using it?
  Reply With Quote
07/02/15, 10:43 AM   #2
Atropos
 
Atropos's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 19
I've been working on ESO addons since the very beginning and I still have big gaps in understanding about this.

There's a lot of things floating in the API that try and objectify LUA tables, but I'm not entirely clear what you actually GAIN by using this.

I'll stay tuned to see if you get any gurus in here to enlighten us.
  Reply With Quote
07/02/15, 11:05 AM   #3
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
It provides the basis for class inheritance. http://esodata.uesp.net/current/src/...bject.lua.html
  Reply With Quote
07/02/15, 11:34 AM   #4
kerb9729
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 56
Originally Posted by merlight View Post
It provides the basis for class inheritance. http://esodata.uesp.net/current/src/...bject.lua.html
I guess one of the drawbacks in learning from existing addon code is that I can't always be sure that what I'm looking at is the "right" way of doing things.

I wonder if someone would be willing to show me an example of the correct way to use ZO_Object?

Do I create a template using ZO_Object.SubClass and then instantiate an object from it using
myobject = ZO_Object.New(templateIcreated)?

That seems like it should be correct, but I don't see it done that way (in the addons I've looked at thus far)?
  Reply With Quote
07/02/15, 01:35 PM   #5
sirinsidiator
 
sirinsidiator's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 1,579
I don't now the *right* way to do things in Lua, as I only learned it since I started to make ESO addons and mostly by looking at the code of the ingame UI, but I quite like the ZO_Object way. It makes things resemble the other languages that I work with every day. Switching between multiple different programming languages is a pain if everything looks and works completely different.

I could do everything without it, but it allows me to structure code like in other languages.
  Reply With Quote
07/02/15, 02:08 PM   #6
XanDDemoX
AddOn Author - Click to view addons
Join Date: May 2015
Posts: 28
I use a slightly different function for class inheritance which requires a little less typing but it is essentially equivalent to ZO_Object

Code:
local function class(base)

	local c = {}
	
	if type(base) == "table" then 
		for k,v in pairs(base) do 
			c[k]=v
		end 
		c.base = base
	end 
	
	c.__index = c
	
	setmetatable(c,{ __call=function(self,...)

               -- create instance
		local obj = setmetatable({},c)
		if self.init then 
			self.init(obj,...)
		elseif base ~= nil and base.init ~= nil then 
			base.init(obj,...)
		end
		return obj
	end
	})
	return c
end
Usage

Code:
-- class
local Animal = class()

function Animal:init()

end


-- subclass
local Dog = class(Animal)

function Dog:init()
     self.base.init(self)
end


-- create new object instance
local dog1 = Dog()

Last edited by XanDDemoX : 07/02/15 at 02:17 PM.
  Reply With Quote
07/06/15, 10:17 AM   #7
Atropos
 
Atropos's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 19
Thanks to those who replied, but I'm still not clear what the actual gain is from trying to force OOP like this. Can someone provide an example use case where using ZO_Object allows them to do something that would otherwise be a big pain in the ass?

Is it simply a mechanism for making programming LUA more friendly/intuitive for people who are used to other OO languages?
  Reply With Quote
07/08/15, 10:19 AM   #8
kerb9729
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 56
Thanks everyone for the responses. Makes more sense now.
  Reply With Quote
07/16/15, 01:40 PM   #9
Lodur
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 108
I just read somewhere and can not for the life of me find the link to it... But to summarize:

LUA is not a functional, procedure, or OO language. It is designed to have the mechanisms needed to be used in any of these ways, but there is no policy on which way to go. Policy is left to the user.

Since ZOS does not give a best practice policy for functional vs procedural vs OO to my knowledge... Do what you want.

ZOS seems to have chosen the OO route for their own LUA code.
  Reply With Quote
07/16/15, 06:12 PM   #10
merlight
AddOn Author - Click to view addons
Join Date: Jul 2014
Posts: 671
I'm going to play the Grammar Nazi here, because everyone who used the word "functional" (except XanDDemoX and Lodur) actually meant "procedural". Functional programming is a whole different world. Read the article XanDDemoX posted if you're interested, although it doesn't relate to the previous discussion And I'm not sure it will make any sense to you if you're not familiar with functional programming to begin with.
  Reply With Quote
07/16/15, 07:31 PM   #11
Lodur
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 108
Originally Posted by merlight View Post
I'm going to play the Grammar Nazi here, because everyone who used the word "functional" (except XanDDemoX and Lodur) actually meant "procedural". Functional programming is a whole different world. Read the article XanDDemoX posted if you're interested, although it doesn't relate to the previous discussion And I'm not sure it will make any sense to you if you're not familiar with functional programming to begin with.
Yes. I mean Functional as in languages such as: Lisp, ML, Clojure, Racket, scala, etc..; and Procedural as in languages such as: C, Basic, Pascal, etc...; and OO as in: C++, Java, Ruby, etc..
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » ZO_Object


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off