Drawing from table

I was doing a little experiment with tables and the draw function. Basically I wanted to put the x,y into a table and then run a for do loop to draw rectangles from the table. The idea being I could then remove any given item from the table. I seem to be missing something and being a complete newbie to any type of coding I am sure it is something simple. Can anyone tell me why this doesn’t work? It only draws the last brick. I ran a print command while inserting into the table and it looks like they are all getting in there but it only draws one. What am I missing? Thanks in advance for you help.

function Bricks:init()
    -- you can accept and set parameters here
    self.block = {}
    self.position = vec2(-40,HEIGHT - 200)
    self.size = vec2(100,20)
    self:makeBricks()
    
end

function Bricks:makeBricks()
        for i = 1,10 do
        table.insert(self.block,self.position)
        self.position.x = self.position.x + 100
        

    end
end
function Bricks:draw()
    -- Codea does not automatically call this method
    pushStyle()
    rectMode(CENTER)
    fill(57, 0, 255, 255)
    for j,b in ipairs(self.block) do
    rect(b.x,b.y,self.size.x,self.size.y)
    end
    
    popStyle()
end

Ok, I guess first I need to know how to post code on here

Edit your post and put three ~ on a line before your code, and the same after your code

I really wish there was an easy to find FAQ on the markup symbols.

The way to post code is to use three ~ symbols on a line by themselves, use that at the beginning and end of the code block:

some code

also, the * symbol creates italics, two *'s create bold, like this:

*italics*
**bold**

I’m sure there’s an in-line code tag, but I don’t know what it is.

To force a line break, you have to press Enter twice. Otherwise, the text will get word wrapped.

Finally, always use the Preview tab at the top of the message box. Reading over your posts that way is always a good idea, partly to make sure that the formatting looks right, and partly just to make sure that you’re saying what you think you’re saying. You have no idea how many times I’ve edited the content of posts after a careful re-reading of what I just typed. I have reached the point where I feel a post that hasn’t been edited just hasn’t been done right. =)

your problem is that you are putting self.position into your table, and although you are changing the x value each time, all the entries in your table end up the same.

The reason is that Codea variables (like many/most languages) can only hold one number or a string of text. If you put anything else in a variable, what actually goes in there is the memory address (or “pointer”) of that thing, not the thing itself. And this applies to vec2.

So your variable self.position does not contain a vec2 , but the address of a vec2. And no matter how much you change self.position.x, the address of self.position stays the same. So you have put the same memory address into your table ten times, and hence the same value.

To make the values different, create a local x variable, x=self.position.x, and use that in your loop instead, increasing it instead of self.position.x. Then it works.

I have an ebook on Lua in the wiki link above, with a chapter on pointers if you want to know more.

@tomxp411 The code bit is in the FAQ which is the fourth discussion in the list on the forum with the title “FAQ -Please read”.

Ignatz you are rapidly becoming my hero! I have read your blog on pointers but somehow did not make the connection to vec2 and changing the variable. Thanks for your help and the great explanations. And keep up the great work on coolcodea. I am slowly working my way through it.