Metatable problem

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
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Metatable problem

Post by Hexenhammer »

Why doesn't this work?

Code: Select all

local Random = {

}

local RandomMT = {
  __call = math.random
}

setmetatable(Random, RandomMT)



function love.load()

  Random(1, 3)

end
I get "bad argument #1 to 'Random' (number expected, got table)
User avatar
markgo
Party member
Posts: 190
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Metatable problem

Post by markgo »

__call function expects the table you're calling as its first argument

Code: Select all

local Random = {

    }

    local RandomMT = {
      __call = function(self,...)
         return math.random(...)
      end
    }

    setmetatable(Random, RandomMT)



    function love.load()

      Random(1, 3)

    end

EDIT: Forgot to return the value
User avatar
Hexenhammer
Party member
Posts: 175
Joined: Sun Feb 17, 2013 8:19 am

Re: Metatable problem

Post by Hexenhammer »

Yup, works now. Thanks!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 197 guests