Self-referring to a table?

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.
Post Reply
User avatar
Sslaxx
Citizen
Posts: 57
Joined: Sat Feb 14, 2009 8:54 pm
Location: Malvern, Worcs, UK
Contact:

Self-referring to a table?

Post by Sslaxx »

This might well be a stupid question, but I haven't been able to locate any answers in the Lua documentation, so...

Is there a way to have a table refer to itself?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Self-referring to a table?

Post by bartbes »

You mean in a method?
Like:

Code: Select all

sometable = {}
sometable.value = 3

function sometable:somefunction() --note the : there
    return self.value
end

print(sometable:somefunction()) --again, note the :
-- output = 3
User avatar
Sslaxx
Citizen
Posts: 57
Joined: Sat Feb 14, 2009 8:54 pm
Location: Malvern, Worcs, UK
Contact:

Re: Self-referring to a table?

Post by Sslaxx »

Ah. I was trying it as "spriteInfo.Draw" - not using it as a method. I'll give that way a go, thanks!

That said, I'd imagine it would work the same way in a copy of the table - would it?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Self-referring to a table?

Post by Robin »

What do you mean by "copy"? If all the functions are "copied"*, it will work.

*Although the functions are not actually copied, but referenced.
Help us help you: attach a .love.
User avatar
Tenoch
Citizen
Posts: 76
Joined: Mon Jul 21, 2008 7:49 am

Re: Self-referring to a table?

Post by Tenoch »

Now this may be unrelated, but you can always do

Code: Select all

a = {}
a.myself = a
I'm not sure how that would be useful, since when you have "a", well, you have it already, so no need for "a.myself", but I felt like mentioning it anyway. Maybe someone will have an application for this.
"When in doubt, use brute force." Ken Thompson
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Self-referring to a table?

Post by Robin »

Tenoch wrote:I'm not sure how that would be useful, since when you have "a", well, you have it already, so no need for "a.myself", but I felt like mentioning it anyway. Maybe someone will have an application for this.
Except for _G._G?
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Majestic-12 [Bot] and 6 guests