Error with table of tables

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Error with table of tables

Post by NoreoAlles »

Error:

Code: Select all

Error

src/generator.lua:21: bad argument #1 to 'ipairs' (table expected, got number)
Code:

Code: Select all

for y, xs in ipairs (map) do
        local max_height = height - 1
        local min_height = height + 2
        height = love.math.random(min_height, max_height)
        map[height] = 1 
        for x, value in ipairs (xs) do

        end
    end
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

Btw, why has everbody got "Obey" in their profile pics? Is it about the brand or some joke i dont get?
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
GVovkiv
Party member
Posts: 686
Joined: Fri Jan 15, 2021 7:29 am

Re: Error with table of tables

Post by GVovkiv »

uh, can we get full source?
it's looks, like you passed number as argument in

Code: Select all

for y, xs in ipairs (map) do
when table was expected...
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Error with table of tables

Post by pgimeno »

Yeah, it looks like at that point, 'map' is a number instead of a table.

It might well be a case of "globals are dangerous".

As for avatars, viewtopic.php?f=3&t=9
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

Code: Select all

generator = {}

function generator:map(width, height)
    local width = width
    local height = height
    local block_x = 1
    local block_y = 0
    local table_amount = 0
    map = {}

    while table_amount < height do 
        table.insert(map, {})
        table_amount = table_amount + 1
    end

    for y, xs in ipairs (map) do
        local max_height = height - 1
        local min_height = height + 2
        height = love.math.random(min_height, max_height)
        map[height] = 1 
        for x, value in ipairs (xs) do

        end
    end

    generator:createSpritebatchAndObjects()
end

function generator:createSpritebatchAndObjects()
    blocks = {}
    local tile_width = 64
    local qoad_width = 16
    for y, xs in ipairs (map) do
        for x, value in ipairs (xs) do
            if value == 1 then
                spritebatch:add(grass, (x-1)*qoad_width,(y-1)*qoad_width)
                b = Object.new((x-1)*tile_width,(y-1)*tile_width, tile_width, tile_width, x, y)
                table.insert(blocks, b)
            elseif value == 2 then
                spritebatch:add(dirt, (x-1)*qoad_width,(y-1)*qoad_width)
                b = Object.new((x-1)*tile_width,(y-1)*tile_width, tile_width, tile_width, x, y)
                table.insert(blocks, b)
            elseif value == 3 then
                spritebatch:add(stone, (x-1)*qoad_width,(y-1)*qoad_width)
                b = Object.new((x-1)*tile_width,(y-1)*tile_width, tile_width, tile_width, x, y)
                table.insert(blocks, b)
            end
        end
    end
end
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

pgimeno wrote: Sat Apr 30, 2022 3:18 pm Yeah, it looks like at that point, 'map' is a number instead of a table.

It might well be a case of "globals are dangerous".

As for avatars, viewtopic.php?f=3&t=9
map is now local, still the same issue.
So, the obey thing is just for laughs i guess.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Error with table of tables

Post by pgimeno »

Ahh, I see what happens. The error is not in map, it's in xs.

The error stems here:

Code: Select all

        map[height] = 1
What's happening is that height is a number, so you're effectively doing map[number] = 1. When ipairs gets to that number, xs gets that value, and since it's not a table, ipairs crashes.

You probably wanted map["height"] = 1 or map.height = 1.

Yes, that's my take on the obey thing.
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

pgimeno wrote: Sat Apr 30, 2022 4:52 pm Yes, that's my take on the obey thing.
I honestly thought i stumbled into some sort of cult when i first posted here. LOL
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

pgimeno wrote: Sat Apr 30, 2022 4:52 pm Ahh, I see what happens. The error is not in map, it's in xs.

The error stems here:

Code: Select all

        map[height] = 1
What's happening is that height is a number, so you're effectively doing map[number] = 1. When ipairs gets to that number, xs gets that value, and since it's not a table, ipairs crashes.

You probably wanted map["height"] = 1 or map.height = 1.

Yes, that's my take on the obey thing.
So you probably got that i want to do random terrain. So i have a "map" table and insert tables into it in a while loop till there exatly as much tables as high my maps should be. So i got one big table, with small "y"/"height" tables and then i want to go into the "y" table and put either an 0 or an 1 or an 2 or an 3 ... in there so i have a diverse and random terrain. But i dont really know how to do that.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
pgimeno
Party member
Posts: 3672
Joined: Sun Oct 18, 2015 2:58 pm

Re: Error with table of tables

Post by pgimeno »

Not sure I understand. Map is a "y" table (first subindex). Each element of the "y" table contains an "x" table.

If you put the 0 or 1 or 2 or 3 into the "y" table, you will be overwriting one "x" table.

Where do you want the 0 or 1 or 2 or 3 exactly? The most logical place seems to be inside the "x" tables, because if you place it in the "y" table, you're replacing one of the "x" tables with a number (which is the problem you were having). Also, the other function seems to expect the values to be indeed in the "x" tables.

And I don't get how you want them distributed. Do you want each cell different? Do you want to fill the terrain with all the same value, just that value is random from 1 to 4? Do you want them scattered, like rocks or coins?

I'll show you both just in case. You seem to have defined three types: grass (1), dirt (2) and stone (3). Let's imagine that you want each cell to randomly be either grass or dirt, and then have some stones scattered.

To do this, first you iterate over all tables, changing values in them, then you scatter some rocks (probably a percentage of the total number of cells):

Code: Select all

    -- first, fill each cell at random with either grass or dirt
    for y, xs in ipairs (map) do
        for x, value in ipairs (xs) do
            xs[x] = love.math.random(1, 2) -- a value between 1 and 2
        end
    end

    -- next, scatter some stones
    for i = 1, width * height * (5/100) do -- 5% of stone (approx)
        local y = love.math.random(1, height)
        local x = love.math.random(1, width)
        map[y][x] = 3
    end
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests