Codea 2.3 table problems

Hi, I updated to Codea 2.3 this morning and found that when I tried to insert a value into a table, it did not work properly. I get one of two errors, either bad argument #2 to insert (position out of bounds) or bad argument #2 to insert (number has no integer representation) It know I have to add or edit something somehow, but I’m not very experienced and I don’t know what I need to do

The code is a little sloppy and doesn’t have enough notes, but here it is

Both errors happen on line 164, I create the table I am inserting into(BlockGrid) on line 36, the value being inserted(BlockType) is decided from lines 109 to 132, and the position it is being inserted into(BLockGridPosToInsert) is from line 71 to 105 and 166

--# Main
-- Islands Game

-- Use this function to perform your initial setup
function setup()
    print("Hello World!")
    parameter.integer("BlockGridPosToInsert",0,10000)
    parameter.integer("BlockGridXCounter",0,1000,50)
    parameter.integer("BlockGridYCounter",0,1000,50)
    parameter.integer("CharacterY",-3000,3000,0)
    parameter.integer("CharacterX",-3000,3000,800)
    parameter.integer("BlockGridCounter",0,1000)

    parameter.integer("BlockPosX",0,WIDTH)
    parameter.integer("BlockPosY",0,HEIGHT)
    
    parameter.integer("BlockGridParameterInsertX",0,WIDTH,50)
    parameter.integer("BlockGridParameterInsertY",0,HEIGHT,50)
    
    parameter.integer("RowCounter",-5,5,-2)
    
    
    parameter.integer("BlockGridInsert",0,1000,6)
    
    parameter.integer("RowWidthToAccess",0,1000,1)
    
    parameter.integer("RowTableInsert",0,1000,5)
    
  parameter.integer("RowToAccess",0,1000)
    
    parameter.integer("TypeOfBlock",0,1000)
    
    parameter.boolean("GenerateIronOre",false)
    parameter.integer("GenerateIronOreCounter",0,1000)
    parameter.integer("GenerateIronOreStartPos",0,1000)
    
     BlockGrid = {}
    BlockGridXInsert = {}
    BlockGridYInsert = {}
    CloudsY = {}
    CloudsX = {}
    RowWidth = {}
    
    parameter.integer("StoneCheckToPosOre",0,1000)
        parameter.integer("OreVeins",0,1000)
        parameter.integer("OreOffsett",-1000,1000)
        OreVeins = math.random(2,4)
       LocationOfOre = {}
        OreOffsetPossibilities = {}
        table.insert(OreOffsetPossibilities,50)
        table.insert(OreOffsetPossibilities,-50)
        table.insert(OreOffsetPossibilities,1)
        table.insert(OreOffsetPossibilities,-1)
        table.insert(OreOffsetPossibilities,51)
        table.insert(OreOffsetPossibilities,-51)
        table.insert(OreOffsetPossibilities,49)
        table.insert(OreOffsetPossibilities,-49)
    
    
    
    
    
    for i = 1,200 do
        table.insert(BlockGrid,17)
    end -- ends for i = 1,200 do
        for i = 1,11 do
        RowTableInsert = RowTableInsert+math.random(2,4)
        table.insert(RowWidth,RowTableInsert)
            -- Sets the width of the rows of the island
    end -- ends for i = 1,11 do

        for i = 1,11 do
 RowToAccess = RowToAccess+1
        if RowToAccess == 1 then
        BlockGridPosToInsert = BlockGridPosToInsert+50
end
        if RowToAccess == 2 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[2]+1
        end
        if RowToAccess == 3 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[3]+2
        end
        if RowToAccess == 4 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[4]/2
        end
        if RowToAccess == 5 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[5]+2
        end
        if RowToAccess == 6 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[6]+2
        end
        if RowToAccess == 7 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[7]+1
        end
        if RowToAccess == 8 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-8--RowWidth[8]
        end
        if RowToAccess == 9 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[9]+1
        end
        if RowToAccess == 10 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[10]+3
        end
        if RowToAccess == 11 then
            BlockGridPosToInsert = BlockGridPosToInsert+50-RowWidth[11]
        end
        -- this section made it so that the rows all line up, although it is innefficent
    
        
    for i = 1, RowWidth[RowToAccess] do --for each block in a row, repeat this loop
            TypeOfBlock = 1 --Sets the default block as stone
            
        if RowToAccess == 10 then -- If it is the 10th row, make it mostly grass, but 1 in 10 is dirt
            if math.random(1,10)<9 then
                TypeOfBlock = 3
                    else
                    TypeOfBlock = 2 
            
            end -- ends if math.random...
                end -- if RowToAccess == 10 then
        
        if RowToAccess == 8 or RowToAccess == 9 then --if it is the eighth of ninth row, have a combination of dirt and stone
                TypeOfBlock = math.random(1,2)
            end -- ends if RowToAccess ==8...
            
        if RowToAccess<8 then -- if it is less than the eighth row, make it all stone with a one in 8 chance of being dirt
                if math.random(1,4) == 1 then
                    
                    TypeOfBlock = math.random(1,2)
                    
                end -- ends if math.random...
         
                end --ends if RowTOAccess<8
            
        
        if RowToAccess == 11 then -- if it is the 11th row,have a 50% chance of adding a small hill
                if math.random(1,2) == 1 then
                    table.insert(BlockGrid,BlockGridPosToInsert+25,3)
                end -- ends if mat h.random...
            end -- ends if RowTOAccess == 11
            
                    for i = 1,100 do
            
          StoneCheckToPosOre = StoneCheckToPosOre+1
            if BlockGrid[StoneCheckToPosOre] == 1 then
              for i = 1, 6 do
                    OreOffsett = i+1
               --         OreOFfsetPossibilities[math.random(1,8)}
                 
                    
                  table.insert(LocationOfOre,StoneCheckToPosOre+OreOffsett)
                    
             end
            end
        end
                
            for i = 1,#LocationOfOre do
            if BlockGridPosToInsert == LocationOfOre[i+1] then
                    print("YASS")
                TypeOfBlock = 4
            end
                end
        if RowToAccess ~= 11 then -- if the row is not 11, insert the type of block in Pos BlockGridPosToInsert+25(+25 makes it in  the middle)
                
        table.insert(BlockGrid,BlockGridPosToInsert+25,TypeOfBlock)
  end -- ends if RowToAccess ~= 11
                   BlockGridPosToInsert = BlockGridPosToInsert+1
    
    end
        


    

   
        
        
        
    
    RowCounter = 0
    for i = 1,#BlockGrid do
     
            RowCounter = RowCounter+1
        
        if RowCounter == 50 then
            
            RowCounter = 0 
            RowWidthToAccess = RowWidthToAccess+1
        
        end -- ends if RowCounter == 50
        
        table.insert(BlockGridXInsert,RowCounter*50)
        table.insert(BlockGridYInsert,RowWidthToAccess*50)
    
        

    end -- ends for i = 1,#BlockGrid, do
    end
    print(#LocationOfOre)

-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
   background(24, 127, 164, 255)
    

    


     
    BlockGridCounter = 1
    
    for i = 1, #BlockGridXInsert-1 do
        BlockGridCounter = BlockGridCounter+1
        
    BlockPosX = BlockGridXInsert[BlockGridCounter]
    BlockPosY = BlockGridYInsert[BlockGridCounter]
    --print(BlockGridXInsert[BlockGridCounter])
        
        if BlockGrid[i] == 1 then
    sprite("Documents:Stone",BlockPosX-CharacterX,BlockPosY-CharacterY,50,50)
        elseif BlockGrid[i] == 2 then
        sprite("Documents:Dirt",BlockPosX-CharacterX,BlockPosY-CharacterY,50,50)
        elseif BlockGrid[i] == 3 then
            sprite("Documents:Grass",BlockPosX-CharacterX,BlockPosY-CharacterY,50,50)
            elseif BlockGrid[i] == 4 then
            sprite("Documents:Iron",BlockPosX-CharacterX,BlockPosY-CharacterY,50,50)
    end
        end
    -- This sets the line thickness
    strokeWidth(5)

    -- Do your drawing here
    
    
    for i = 1,100 do
        table.insert(CloudsY,math.random(0,HEIGHT))
        table.insert(CloudsX,math.random(0,WIDTH))
    end
    zLevel(-1)
    for i = 1,100 do
    sprite("Platformer Art:Cloud 3",CloudsX[i]-CharacterX/50,CloudsY[i]-CharacterY/50)
        end
end

end

@Nathan Anytime you get the error “number has no integer representation”, you need to make it an integer. You can use math.floor or math.ceil to get the integer value of a number. Look at the built in reference to see how they are used.

@Nathan

Since lua 5.3 has true integers, look at reference for parms requiring integers.

Note: 2.5 cannot be represented as an interger so code in 5.1 that used floats will not work in CODEA 2.3

If you’re dividing, // is the same as flooring the answer:

print(35.7312//5) --prints 7

@dave1707 Using math.floor() fixed the problem, thank you :slight_smile: But I am still getting the error saying that the position is out of bounds.

@Nathan What line is giving that error. Give the line number plus the line itself just in case the line numbers don’t match.

@Nathan Never mind about the line number and line. An out of bounds error means that you’re trying to insert something into a table that’s more than 1 entry beyond the end of the table.

@Nathan When I run your code and get the out of bounds error, the table length is 245. You’re trying to insert something at position 277 which is way beyond the end of the table.

@dave1707 I was trying to use the position in the BlockGrid table as a way to find where each block went and the value as which type of block it is. inserted a bunch of 0s (which I don’t do anything with) into the table and replaced them with the numbers I wanted, worked like a charm. Not sure if this is the best way to it, but I works. Thank you so much for helping!

It seems that table.insert changed around 5.2. I had an old test program that just did table.insert(t, 123, “foo”) and similar, that used to work and now gets the position out of bounds. I gather that insert used to do crazy things moving things around, and insert was changed to be more strict.

Not sure yet, but guessing one needs to use insert much more like an array insert and that mixed tables will be even weirder than they ever were.

Or, maybe something about integer? Anyway has def changed, Prob for the better.