Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get __metatable function to be called?

Asked by 5 years ago
Edited 5 years ago

My goal

Get getmetatable to return nil by a function I've assigned a __metatable field to.

Code:

local tbl, mt = {}, {
    __metatable = function()
        return nil;
    end
};

setmetatable(tbl, mt);
print(getmetatable(tbl))

Problem

The call getmetatable(tbl) is literally evaluating to the function itself and not calling it,

Is there a way to get this to happen, or would I just have to use some other value? I'd really prefer nil being the output, but if not then oh well.


Please answer only if you know what you are talking about. Or else ur answer's gonna get bombarded with downvotes LOL

0
no programmerHere 371 — 5y
0
you can run the function inside the table by saying print(getmetatable(tbl)()) royaltoe 5144 — 5y
0
It does print the function memory address, but I want getmetatable to return the result of the function which is nil programmerHere 371 — 5y
0
so you don't want to do getmetatable(tbl)(), you want to call the function when you get the metatable, not afterwards? royaltoe 5144 — 5y
View all comments (4 more)
0
No i want it to return nil, i want getmetatable to return nil programmerHere 371 — 5y
0
You could do the following, not sure if theres a way to have the function inside the table since Lua will error because it's expecting a metatable https://pastebin.com/ZqCj1ejL I'll look into it some more if that isn't okay royaltoe 5144 — 5y
0
just set __metatable to nil theking48989987 2147 — 5y
0
Not gonna work since it won't even add the field programmerHere 371 — 5y

1 answer

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

The __metatable field takes any value, not just a function. Furthermore it will NOT cause getmetatable() to call the function you assigned to it. It will simply return the function instead of the metatable, but not call it.

0
I know it can be anything, but i was looking for a way to get the function to be called. Is my only option to rewrite set/get metatable to do this? programmerHere 371 — 5y
0
does the pastebin option work? if not, why? royaltoe 5144 — 5y
Ad

Answer this question