WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   OOP and metatables - pairs problem (https://www.wowinterface.com/forums/showthread.php?t=55799)

Alizia 10-13-17 12:58 PM

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.

MunkDev 10-13-17 03:58 PM

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

Alizia 10-13-17 06:51 PM

Thanks for the answer.

Quote:

Originally Posted by MunkDev (Post 325470)
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... :confused:

Alizia 10-13-17 06:57 PM

Quote:

Originally Posted by MunkDev (Post 325470)
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. :confused:

Fizzlemizz 10-13-17 07:06 PM

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.

Seerah 10-13-17 08:20 PM

Quote:

Originally Posted by Alizia (Post 325473)
I try this, the same error popup " 'do' expected near 'in' ". It's like self.tab isn't considered like a table. :confused:

It sounds like you missed something somewhere. What is your code?

Alizia 10-13-17 09:02 PM

Quote:

Originally Posted by Seerah (Post 325477)
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.

Fizzlemizz 10-13-17 09:58 PM

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.

Alizia 10-14-17 05:11 AM

Quote:

Originally Posted by Fizzlemizz (Post 325480)
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.

pas06 10-14-17 09:03 AM

please Show your whole Code. Looks like you never call the New function

Alizia 10-14-17 10:02 AM

Quote:

Originally Posted by pas06 (Post 325487)
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. :/

Alizia 10-14-17 10:30 AM

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.

MunkDev 10-14-17 10:35 AM

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.

Alizia 10-14-17 10:55 AM

Quote:

Originally Posted by MunkDev (Post 325490)
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 :)


All times are GMT -6. The time now is 05:32 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI