Thread Tools Display Modes
10-13-17, 12:58 PM   #1
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
OOP and metatables - pairs problem

Hello everyone,

I want to use some classes for my addon, but I have a problem with pairs() in methods. A exemple :

Code:
ClassTest = {}
ClassTest.__index = ClassTest

function ClassTest:New()
   local self = {}
   setmetatable(self, ClassTest)
   
   self.tab = {}
   return self
end

function ClassTest:CheckTab()
   for i=1, stat in pairs(self.tab) do
      -- Do something
   end
end
There is a error on pairs() function. I find this topic that seem a similar problem, but metatables are new for me and I don't understand the (potential) solution.
  Reply With Quote
10-13-17, 03:58 PM   #2
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Your metatable is fine, what's not fine is that pairs supplies your iterator for you. You don't set it yourself.
Lua Code:
  1. function ClassTest:CheckTab()
  2.    for i, stat in pairs(self.tab) do
  3.       -- Do something
  4.    end
  5. end
__________________

Last edited by MunkDev : 10-14-17 at 10:28 AM.
  Reply With Quote
10-13-17, 06:51 PM   #3
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Thanks for the answer.

Originally Posted by MunkDev View Post
Your metatable is fine, what's not fine is that pairs supplies your iterator for you. You don't set it yourself.
I try this, the same error popup " 'do' expected near 'in' ". It's like self.tab isn't considered like a table or 'do' is ignored...

Last edited by Alizia : 10-13-17 at 06:55 PM.
  Reply With Quote
10-13-17, 06:57 PM   #4
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Originally Posted by MunkDev View Post
Your metatable is fine, what's not fine is that pairs supplies your iterator for you. You don't set it yourself.
I try this, the same error popup " 'do' expected near 'in' ". It's like self.tab isn't considered like a table.
  Reply With Quote
10-13-17, 07:06 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
pairs returns the key, value pairs in a table

Code:
self.tab = {
      key1 = 1,
      key2 = { a=1, b=2 },
      key3 = "Hello"
}

for key, value in pairs(self.tab) do
    -- This will return over the 3 iterations (in no particular order)
    key == "key1", value == 1
    key == "key2", value == (pointer to the table)
    key == "key3", value == "Hello"
end
You can replace the key, value terms with any names that suit the situation.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 10-13-17 at 07:10 PM.
  Reply With Quote
10-13-17, 08:20 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Originally Posted by Alizia View Post
I try this, the same error popup " 'do' expected near 'in' ". It's like self.tab isn't considered like a table.
It sounds like you missed something somewhere. What is your code?
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
10-13-17, 09:02 PM   #7
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Originally Posted by Seerah View Post
It sounds like you missed something somewhere. What is your code?
Code:
-- Class Pokemon --
CPA_Pokemon_Class = {}
CPA_Pokemon_Class.__index = CPA_Pokemon_Class

function CPA_Pokemon_Class:New()
	local self = {}
	setmetatable(self, CPA_Pokemon_Class)
	
	self.stat = {}
	self.stat.pv = {
		["stat"] = -- Int,
		["EV"] = -- Int,
		["IV"] = -- Int,
		["reel"] = -- Int,
		["reelactu"] = -- Int,
	}
	return self
end

function CPA_Pokemon_Class:InitFight()
	for i, stat in pairs(self.stat) do
		-- Do some stuff
	end
end
This is a part of my code (yes, it's a pokemon game), 5 others stats (stat.att, stat.def...) are set in the same way. Each "-- Int" is a simple integer value (5, 18, 27...) stored in a "DB" table.

'do' is here, I don't understand what I missed.
  Reply With Quote
10-13-17, 09:58 PM   #8
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,871
local self = {}

Is local to that chunk of code (in this case function) hence doesn't exist in the lower function and has not been passed to it. You would need too:

Code:
function CPA_Pokemon_Class:InitFight(table)
	for key, stat in pairs(table.stat.pv) do
		print(key, value)
	end
end

local table = CPA_Pokemon_Class:New()
CPA_Pokemon_Class:InitFight(table)
You don't really need all the [" "]

Code:
self.stat.pv = {
	stat = -- Int,
	EV = -- Int,
	IV = -- Int,
	reel = -- Int,
	reelactu = -- Int,
}
but that's entirely up to you.

I'm not metatable enabled so there is probably a more direct method using it.
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.

Last edited by Fizzlemizz : 10-13-17 at 10:27 PM.
  Reply With Quote
10-14-17, 05:11 AM   #9
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Originally Posted by Fizzlemizz View Post
local self = {}

Is local to that chunk of code (in this case function) hence doesn't exist in the lower function and has not been passed to it. You would need too:

Code:
function CPA_Pokemon_Class:InitFight(table)
	for key, stat in pairs(table.stat.pv) do
		print(key, value)
	end
end

local table = CPA_Pokemon_Class:New()
CPA_Pokemon_Class:InitFight(table)
I tried this, same error.
PS : The function isn't even called, it's like a syntax error.

Last edited by Alizia : 10-14-17 at 07:53 AM.
  Reply With Quote
10-14-17, 09:03 AM   #10
pas06
A Theradrim Guardian
Join Date: Apr 2009
Posts: 62
please Show your whole Code. Looks like you never call the New function
  Reply With Quote
10-14-17, 10:02 AM   #11
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Originally Posted by pas06 View Post
please Show your whole Code. Looks like you never call the New function
Of course I don't call New function, the error is on check syntax on the addon, before any call, I can't call it before fix this. :/

Last edited by Alizia : 10-15-17 at 04:26 AM.
  Reply With Quote
10-14-17, 10:30 AM   #12
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Okay, I find out the problem... I let a initialisation on a ipairs... I'm dumb.

Thanks for all, I will post the finale addon when it will be ready.
  Reply With Quote
10-14-17, 10:35 AM   #13
MunkDev
A Scalebane Royal Guard
 
MunkDev's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2015
Posts: 431
Well, of course you're getting syntax errors. You only corrected the iterator in one place. That's not what's giving you the error. You have to correct all the other iterators in your code. It fails while compiling, not while running. Hint: read the line number in your error.
__________________
  Reply With Quote
10-14-17, 10:55 AM   #14
Alizia
A Murloc Raider
Join Date: Oct 2017
Posts: 8
Originally Posted by MunkDev View Post
Hint: read the line number in your error.
Yeah... When I tried to delete initiation on pairs, the error go from 82 to 92... I didn't noticed this.

It compile now
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » OOP and metatables - pairs problem

Thread Tools
Display Modes

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