Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Why is the argument a table? Trying to use _call, think i messed up

-- Card7
Card = {}
Card.__index = Card
local RANK,SUIT = {"ACE","2","3","4","5","6","7","8","9","10","JACK", "QUEEN","KING"},{"CLUBS", "DIAMONDS", "HEARTS","SPADES"}
table.move(RANK, 1,13,0)
setmetatable(Card, {__call = function (int) return end })


    print(int)  -- WHY IS THIS A TABLE

    return setmetatable({suit = int%4, rank = int%13}, Card)
function Card:__tostring()
    return "<Card:" ..RANK[self.rank].." of "..SUIT[self.suit]

-- Use this function to perform your initial setup
function setup()
    deck = {}
    for i = 1,52 do deck[i] = Card(i) end
    for i = 52,2,-1 do j=math.random(52) deck[i], deck[j] = deck[j], deck[i] end
    field = {}

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets Line thickness


function touched(touch)

    if touch.state == BEGAN then

    elseif touch.state == MOVING then

    elseif touch.state == ENDED then


I dont realy know what I'm doing with metatables, i hardly use them.


  • dave1707dave1707 Mod
    Posts: 7,605

    @xThomas Are you trying to learn how to use metatables. I'm not sure what you're trying to accomplish by using them unless you just want to experiment.

  • Posts: 500

    I think he's trying to implement inheritance. But we have a class() constructer for that already...

  • edited September 2017 Posts: 175

    @xThomas In your code the first argument that is passed to the __call metamethod is the Card table. The reason is because Lua passes the non function value that is called as the first parameter to __call. Not the first argument of the call. The arguments follow afterwards.

    In your case, you're calling the non function value Card in setup. So that is the value that is passed to the parameter that you have named int in your __call function.

    someValue = {}
        __call = function(value,...)
            print(value == someValue)

    You can find more about metatables and metamethods in the manual.

  • Posts: 121

    @dave1707, @XanDDemoX : Yes, im trying to learn metatables. thanks, didnt know what it was doing.

  • Posts: 121
    -- Cards Model (of MVC)
    -- Use this function to perform your initial setup
    Card = {}
    Card.__newindex = Card = function(i) return{i=i,texCoordSuit=1/(i%4), texCoordRank=1/(i%13), suit=i%4, rank=i%13} end
    setmetatable(Card, {__call = function(_,...) return end} )
    Card.__tostring = function(self) return "<Suit: "..self.suit,"Rank: "..self.rank..">" end
    print( deck[1] )
    local c = deck[1]
    print( c.i, c.suit, c.rank, c.texCoordSuit, c.texCoordRank )

    Ok, so I ask this time, how to make __tostring work. I read that it would make print print different things. So i tried to have it print rank and suit. But it didnt work

  • @xThomas It's not working because your instances of Card don't have a metatable. Try this out looking carefully at how I have wired it up. You may also find it helpful to study Codea's implementation of class. You can find it on GitHub.

    someClass = {}
    someClass.__index = someClass
    someClass.__tostring = function(instance)
        return "{instance.someMember="..tostring(instance.someMember).."}"
        __call = function(value,...)
            return setmetatable({},someClass)
    someInstance = someClass()
    someInstance.someMember = "value"
Sign In or Register to comment.