Page cover

👾Metatable Functions

ดึง metatable ของค่าโดยไม่คำนึงถึงฟิลด์ __metatable ของ Value นั้นๆ

<table> getrawmetatable(<table> value)

ตัวอย่าง #1

-- ทำซ้ำโค้ดที่อยู่ภายในบล็อก while ในระหว่างรอ 1 วินาทีทุกครั้ง
while wait(1) do
    -- แสดงค่าของตัวแปร workspace.Camera ในคอนโซล (Console)
    print(workspace.Camera)
end

ตัวอย่าง #2

-- นำค่า metatable ของตัวแปร game มาเก็บในตัวแปร game_meta
local game_meta = getrawmetatable(game)

-- นำค่า __index ของ metatable มาเก็บในตัวแปร game_index
local game_index = game_meta.__index

-- ทำให้ metatable ของตัวแปร game เปลี่ยนแปลงค่าได้ (mutable) โดยใช้ setreadonly() โดยกำหนดให้เป็น false
setreadonly(game_meta, false) -- Required to avoid errors

-- กำหนดให้ตัวแปร game_meta เปลี่ยนแปลงค่าของ __index ใหม่โดยใช้ฟังก์ชัน (function) ที่กำหนดเอง
game_meta.__index = function(Instance, string)
    -- เมื่อมีการเรียกใช้ __index ของตัวแปร game_meta ด้วยตัวอักษร string

    -- ถ้า string คือ "Camera" ให้คืนค่าการเรียกใช้ __index ของ game โดยใช้ชื่อ Instance ของตัวแปร "Workspace"
    if string == "Camera" then
        return game_index(game, "Workspace")
    end

    -- หากไม่ใช่กรณีข้างต้น ให้คืนค่าการเรียกใช้ __index ของตัวแปร Instance ด้วยตัวอักษร string
    return game_index(Instance, string)
end

กำหนดตารางเมตาเทเบิลตัวแรกให้เป็นตารางที่ระบุ (provided mt) โดยไม่สนใจว่ามีฟิลด์ __metatable อยู่ในตารางเมตาเทเบิลตัวแรกหรือไม่" ซึ่งเป็นคำอธิบายของการใช้งานภาษาโปรแกรม Lua ในสภาวะที่ต้องการกำหนดตารางเมตาเทเบิลของค่าให้เป็นตารางที่กำหนดไว้ โดยไม่สนใจว่าตารางเมตาเทเบิลตัวแรกมีฟิลด์ __metatable อยู่หรือไม่

Hook Metamethod

เชื่อมต่อ Metamethod ที่ระบุไว้ในตารางเมตาเทเบิลของ Object กับฟังก์ชัน f

ตัวอย่าง

ตัวอย่าง : Basic Remote Spy

Last updated