Page 2 of 5

Re: OOP help

Posted: Sun Feb 09, 2014 6:46 am
by MarekkPie
Completely off topic, but brilliant use of Ming the Merciless.

Re: OOP help

Posted: Sun Feb 09, 2014 11:55 am
by Roland_Yonaba
Kikito, that was epic. Bookmarked that response, I am likely to redirect bunch of people to this.

Re: OOP help

Posted: Sun Feb 09, 2014 12:15 pm
by Robin
Zilarrezko wrote:So I should make a whole new table for the objects to store in?
That's possible, but not necessary (although personally, I prefer to have a separate table for instances, but again: you don't need to).

What you really need is to use ipairs, instead of pairs. Unlike pairs, which goes over all the keys in the table, ipairs only goes over integer keys from 1 to n, which is what you want in this case.

Re: OOP help

Posted: Sun Feb 09, 2014 5:30 pm
by Zilarrezko
[quote="Robin"
What you really need is to use ipairs, instead of pairs. Unlike pairs, which goes over all the keys in the table, ipairs only goes over integer keys from 1 to n, which is what you want in this case.[/quote]

Ah! So it skips all the things like metatables and other tables?

Re: OOP help

Posted: Mon Feb 10, 2014 9:29 am
by Robin
Neither pairs nor ipairs does anything with a table's metatable.

If you have this table:

Code: Select all

local t = {}
t[1] = "one"
t[2] = "two"
t[3] = "three"
t["four"] = 4
local five = {five = 5}
t[five] = 5
t[true] = 6
Then

Code: Select all

for i, v in ipairs(t) do
    print(i, v)
end
prints

Code: Select all

1	one
2	two
3	three
in that order.

But

Code: Select all

for k, v in pairs(t) do
    print(k, v)
end
prints

Code: Select all

1	one
2	two
3	three
four	4
table: 0x1067080	5
true	6
but you don't know in which order beforehand.

Re: OOP help

Posted: Mon Feb 10, 2014 5:25 pm
by Zilarrezko
so then in which cases do tables get a numerical value when entered?

Re: OOP help

Posted: Mon Feb 10, 2014 6:06 pm
by Robin
What do you mean?

Re: OOP help

Posted: Tue Feb 11, 2014 1:55 am
by Zilarrezko
In which cases when you enter a value into a table will it's key be a numerical value such as [1] or [2]? Like table.insert(t,{})?

Re: OOP help

Posted: Tue Feb 11, 2014 3:03 am
by ejmr
Zilarrezko wrote:In which cases when you enter a value into a table will it's key be a numerical value such as [1] or [2]? Like table.insert(t,{})?
Here are the basics on how table.insert() affects numeric indices.

Code: Select all

local t = { "foo", "bar" }

-- t[1] == "foo"
-- t[2] == "baz"
-- #t   == 2     i.e. The length of the table is two.

table.insert(t, "baz")

-- Now t[3] == "baz"
--
-- The call to table.insert() above is a short-cut for this, which
-- would accomplish the same thing:

table.insert(t, #t + 1, "baz")

-- The optional second argument there is the 'position' where the
-- function should insert the new element.  It defaults to the
-- length of the table plus one.

Re: OOP help

Posted: Tue Feb 11, 2014 3:22 am
by Zilarrezko
HEY HEY! no way, I was looking for something like that. I wanted my main menu console to display the start of the table at the bottom just above the console input display area and work it's way up rather than down. Sweetness. But I think I get a good gist of basics of building basic objects and OOP. The last thing I think I need is a greater understanding of how to use the self parameter, and how to build my system and metatable for the use of "self".That series of information from kikito were very confusing.