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?
Self-referring to a table?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Self-referring to a table?
You mean in a method?
Like:
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
Re: Self-referring to a table?
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?
That said, I'd imagine it would work the same way in a copy of the table - would it?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Self-referring to a table?
What do you mean by "copy"? If all the functions are "copied"*, it will work.
*Although the functions are not actually copied, but referenced.
*Although the functions are not actually copied, but referenced.
Help us help you: attach a .love.
Re: Self-referring to a table?
Now this may be unrelated, but you can always do
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.
Code: Select all
a = {}
a.myself = a
"When in doubt, use brute force." Ken Thompson
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Self-referring to a table?
Except for _G._G?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.
Help us help you: attach a .love.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 2 guests