OOP help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: OOP help

Post by MarekkPie »

Completely off topic, but brilliant use of Ming the Merciless.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: OOP help

Post by Roland_Yonaba »

Kikito, that was epic. Bookmarked that response, I am likely to redirect bunch of people to this.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: OOP help

Post 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.
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post 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?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: OOP help

Post 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.
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post by Zilarrezko »

so then in which cases do tables get a numerical value when entered?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: OOP help

Post by Robin »

What do you mean?
Help us help you: attach a .love.
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post 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,{})?
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: OOP help

Post 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.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
Zilarrezko
Party member
Posts: 345
Joined: Mon Dec 10, 2012 5:50 am
Location: Oregon

Re: OOP help

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 51 guests