Attempt to concatenate a nil value

You can see below my attempt to make a Card class. I’m not really sure what I need to do. The idea was that p in Card:init(X,y,p) would be used as a key into the array of pictures, and then I would concatenate that key onto the string “Documents:” to make it grab the right picture. You can see my attempt in the main setup


Card = class()

--array containing diamond, spade, heart, clubs pictures
  --p is the key to pictures, appearing in sprite("Documents:
pictures = {dK,dQ,dJ,d10,d9,d8,d7,d6,d5,d4,d3,d2,dA,
            sK,sQ,sJ,s10,s9,s8,s7,s6,s5,s4,s3,s2,sA,
            hK,hQ,hJ,h10,h9,h8,h7,h6,h5,h4,h3,h2,hA,
            cK,cQ,cJ,c10,c9,c8,c7,c6,c5,c4,c3,c2,cA}
            
function Card:init(x,y,p) 
    self.x = x
    self.y = y
    self.pic = "Documents:"..pictures[p]
end

function Card:draw()
    
    sprite(pic,self.x,self.y)

    
end

and here is main

--Main
function setup()
    Setup:init()
    deal = Card(40,40,1)
end

function draw()
    background(black)
    --deal:draw()
end


@xThomas The contents of picture should be stored as a string using quotes Eg.

pictures = {"dK", "dQ", "dJ"}

Use readImage() to save the image to self.pic
And in Card:draw() you forgot self. before pic in the spirite draw

Try this

Card = class()
--array containing diamond, spade, heart, clubs pictures
--p is the key to pictures, appearing in sprite("Documents:
pictures = {"dK","dQ","dJ","d10","d9","d8","d7","d6","d5","d4","d3","d2","dA",
            "sK","sQ","sJ","s10","s9","s8","s7","s6","s5","s4","s3","s2","sA",
            "hK","hQ","hJ","h10","h9","h8","h7","h6","h5","h4","h3","h2","hA",
            "cK","cQ","cJ","c10","c9","c8","c7","c6","c5","c4","c3","c2","cA"}

function Card:init(x,y,p) 
    self.x = x
    self.y = y
    self.pic = readImage("Documents:"..pictures[p])
end

function Card:draw()
    sprite(self.pic,self.x,self.y)
end

I would do it much more simply.

First create a single image with all the card pictures on it in 4 rows, each one with a suit, and 13 card images running from 2 to A, in each row, ie 4 X 13 images.

Then you load that single picture into memory, and you can either copy out an individual card image from it when you need it, or copy them all out when the program starts, into a table of 52 images, for greater speed.

The advantage is that you can use numbers for suits and ranks, so for example the 10 of Clubs is row 1 and column 10, which is where that card is on your master image of all the cards.

Card = class()


imageCard = readImage("Documents:cardSheetPLACEHOLDER")
--note these are placeholder images from google images, each card is 50px wide and 80px high

function Card:init(x,y,row,column)
    --[[ row and column reference the card spritesheet --]]
    self.x = x
    self.y = y
    self.w = 50
    self.h = 80
    self.row = row
    self.column = column
    self.u = 1/13*self.row - 1/13
    self.v = 1/5*self.column - 1/5
    cMesh = mesh()
    cMesh.texture = imageCard
    idx = cMesh:addRect(self.x,self.y,self.w, self.h)
    cMesh:setRectTex(idx, self.u, self.v, 1/13, 1/5)
    print(self.w)
end

function Card:draw()

    cMesh:draw()

end

@Ignatz , is this what you meant?

Something like that, yes. It’s much neater, isn’t it?