Page 1 of 2
Error with table of tables
Posted: Sat Apr 30, 2022 2:53 pm
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
Re: Error with table of tables
Posted: Sat Apr 30, 2022 2:57 pm
by NoreoAlles
Btw, why has everbody got "Obey" in their profile pics? Is it about the brand or some joke i dont get?
Re: Error with table of tables
Posted: Sat Apr 30, 2022 3:07 pm
by GVovkiv
uh, can we get full source?
it's looks, like you passed number as argument in
when table was expected...
Re: Error with table of tables
Posted: Sat Apr 30, 2022 3:18 pm
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
Re: Error with table of tables
Posted: Sat Apr 30, 2022 3:24 pm
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
Re: Error with table of tables
Posted: Sat Apr 30, 2022 3:31 pm
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.
Re: Error with table of tables
Posted: Sat Apr 30, 2022 4:52 pm
by pgimeno
Ahh, I see what happens. The error is not in map, it's in xs.
The error stems here:
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.
Re: Error with table of tables
Posted: Sat Apr 30, 2022 4:56 pm
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
Re: Error with table of tables
Posted: Sat Apr 30, 2022 5:18 pm
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:
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.
Re: Error with table of tables
Posted: Sun May 01, 2022 11:09 am
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