Metatables across threads

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
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

Metatables across threads

Post by IMP1 »

I'm using a thread to perform level generation so I can show progress and keep on rendering things while it's happening.

But the final object being passed to the main thread is a nested table and it seems that the metatables of the sub-tables are not preserved "through" the channel.

I can instead pass the necessary data and construct the objects after the level generation, but it seems to be like this makes using the separate thread (slightly) less useful.

Is this a feature of channels? Is it a feature that is likely to be changed? Or have I just missed something in my code?

Thanks to anyone who can help!
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Metatables across threads

Post by grump »

Unfortunately, metatables are not preserved across channels. They wouldn't be very useful anyway, because you can't send (raw) functions over channels.

One possible and efficient solution is to build a table of all required metatables in all threads, and include an identifer for the metatable when you send a table. You can then just call setmetatable on the receiving end, without the need to reconstruct the object.

Sending metatables over the channel is possible too, but you have to string.dump() all functions, and loadstring() them on the receiving end, and upvalues will be lost.
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

Re: Metatables across threads

Post by IMP1 »

Ah, that clears things up. Good to know about functions also not being sent over channels. Thanks a lot!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 113 guests