OOP help
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: OOP help
Completely off topic, but brilliant use of Ming the Merciless.
- Roland_Yonaba
- Inner party member
- Posts: 1563
- Joined: Tue Jun 21, 2011 6:08 pm
- Location: Ouagadougou (Burkina Faso)
- Contact:
Re: OOP help
Kikito, that was epic. Bookmarked that response, I am likely to redirect bunch of people to this.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: OOP help
That's possible, but not necessary (although personally, I prefer to have a separate table for instances, but again: you don't need to).Zilarrezko wrote:So I should make a whole new table for the objects to store in?
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.
Help us help you: attach a .love.
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: OOP help
[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?
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?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: OOP help
Neither pairs nor ipairs does anything with a table's metatable.
If you have this table:
Then
prints
in that order.
But
prints
but you don't know in which order beforehand.
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
Code: Select all
for i, v in ipairs(t) do
print(i, v)
end
Code: Select all
1 one
2 two
3 three
But
Code: Select all
for k, v in pairs(t) do
print(k, v)
end
Code: Select all
1 one
2 two
3 three
four 4
table: 0x1067080 5
true 6
Help us help you: attach a .love.
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: OOP help
so then in which cases do tables get a numerical value when entered?
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: OOP help
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,{})?
- ejmr
- Party member
- Posts: 302
- Joined: Fri Jun 01, 2012 7:45 am
- Location: South Carolina, U.S.A.
- Contact:
Re: OOP help
Here are the basics on how table.insert() affects numeric indices.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,{})?
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.
- Zilarrezko
- Party member
- Posts: 345
- Joined: Mon Dec 10, 2012 5:50 am
- Location: Oregon
Re: OOP help
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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Majestic-12 [Bot] and 3 guests