Jump to content




need help with metatables


  • You cannot reply to this topic
5 replies to this topic

#1 JokerRH

  • Members
  • 147 posts

Posted 12 February 2013 - 01:46 PM

I recently learned basics about metatables in lua.
My problem is that i can only insert a metatable into anotherone
with it's metatable.
I hope the example can show my problem better:

t = {}
t = setmetatable({}, t)

s = {}
s = setmetatable({test = "test"}, s)

table.insert(t, s)

therefore t[1].test returns "test".
now, if i change s:

s.test = "asdf"

t[1].test also returns "asdf".
Is there a way to prevent that instead of changing these to normal tables?
(I need both of them as metatables for the rest of my apis)

thanks. :)/>

Edit: This means that i need a method like getmetatable to get back the table that has the metatable attached to it (if such a method exists)

#2 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 12 February 2013 - 06:47 PM

i think you arent understanding the concept of metatables. metatables are tables of functions to be used by the object they define

using the syntax:
object:metamethod()

and getmetatable does exist

it just wont work if the table has a __metatable index

#3 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 12 February 2013 - 07:14 PM

View Posttesla1889, on 12 February 2013 - 06:47 PM, said:

i think you arent understanding the concept of metatables. metatables are tables of functions to be used by the object they define

using the syntax:
object:metamethod()

and getmetatable does exist

it just wont work if the table has a __metatable index
I don't think you're understanding them either.

#4 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 12 February 2013 - 08:06 PM

View Postimmibis, on 12 February 2013 - 07:14 PM, said:

View Posttesla1889, on 12 February 2013 - 06:47 PM, said:

i think you arent understanding the concept of metatables. metatables are tables of functions to be used by the object they define

using the syntax:
object:metamethod()

and getmetatable does exist

it just wont work if the table has a __metatable index
I don't think you're understanding them either.

thats how you use them

i mean, there are library defined functions that you can set, but he probably wont need any of those

#5 Eric

  • Members
  • 92 posts
  • LocationUK

Posted 12 February 2013 - 09:07 PM

Your meteta les are irrelevant here. What you're seeing is standard pass-by-reference behaviour for tables.

t = {}
s = {test = "test"}

table.insert(t, s)
s.test = "changed"

print(t[0].test)



#6 JokerRH

  • Members
  • 147 posts

Posted 12 February 2013 - 10:10 PM

yes, I think that I do understand the way metatables work, I just thought that this
was a sideeffect of them and therefor made an example that only defined it and inserted
it into anotherone. I didn't even think that this was a normal table's behaviour...:(
(thanks Eric!)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users