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)
table.move(SUIT,1,4,0)
setmetatable(Card, {__call = function (int) return Card.new(int) end })

function Card.new(int)
    
    print(int)  -- WHY IS THIS A TABLE
    
    
    return setmetatable({suit = int%4, rank = int%13}, Card)
end
function Card:__tostring()
    return "<Card:" ..RANK[self.rank].." of "..SUIT[self.suit]
end

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

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

    -- This sets Line thickness
    strokeWidth(5)

    
    
end


function touched(touch)

    if touch.state == BEGAN then

    elseif touch.state == MOVING then

    elseif touch.state == ENDED then


    end
end

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

@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.

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

@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 = {}
setmetatable(someValue,{
    __call = function(value,...)
        print(value == someValue)
        print(...)
    end
})
someValue("some","arguments")

You can find more about metatables and metamethods in the manual.
https://www.lua.org/manual/5.3/manual.html#2.4

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

-- Cards Model (of MVC)

-- Use this function to perform your initial setup
Card = {}
Card.__newindex = Card
Card.new = 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 Card.new(...) end} )
Card.__tostring = function(self) return "<Suit: "..self.suit,"Rank: "..self.rank..">" end
 deck={Card(math.random(52))}
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).."}"
end
setmetatable(someClass,{
    __call = function(value,...)
        return setmetatable({},someClass)
    end
})
someInstance = someClass()
someInstance.someMember = "value"
print(someInstance)