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: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

Thanks you, i acctually wanted to do like first a perline noise grass layer and then start filling it with dirt and stone, but this will definitly help! Thank you.
"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: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

pgimeno wrote: Sun May 01, 2022 11:09 am 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
Sorry to bother again, but for some reason this simply does nothing, i even looked if it even looped thru there with and if statement in the second for loop, yet nothing happend. The second one for the stones works fine, but always starts from the left.

Code: Select all

    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
"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: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

I think i might have over estimated my lua knowledge, i ended up fixing it with this:

Code: Select all

 map = {{0,0,0,0,0},
           {0,0,0,0,0},
           {0,0,0,0,0},
           {0,0,0,0,0},
           {0,0,0,0,0},}
   
    for y, xs in ipairs (map) do 
        for x, value in ipairs ( xs ) do 
            map[y][x] = love.math.random(1, 3) --will be doing some 1d perlin noise!
            print("done")
        end
    end
I only want 16 * 16 chunks anyway and it will be way easier like this, i tried making the system so i could make a chunk as big as i want it to be, but this is easier for me to work with.
Thank you for your help! ^^
"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: 3549
Joined: Sun Oct 18, 2015 2:58 pm

Re: Error with table of tables

Post by pgimeno »

NoreoAlles wrote: Mon May 02, 2022 6:23 pm Sorry to bother again, but for some reason this simply does nothing, i even looked if it even looped thru there with and if statement in the second for loop, yet nothing happend. The second one for the stones works fine, but always starts from the left.
Sorry, I didn't realize you didn't have anything inside the horizontal tables. They need to be filled with something for ipairs() to do its job.
User avatar
NoreoAlles
Party member
Posts: 107
Joined: Mon Jan 03, 2022 5:42 pm

Re: Error with table of tables

Post by NoreoAlles »

pgimeno wrote: Thu May 05, 2022 12:47 am Sorry, I didn't realize you didn't have anything inside the horizontal tables. They need to be filled with something for ipairs() to do its job.
No worries, now i know just a bit more about Lua!
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 94 guests