Love doesnt wanna accept my structure

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.
Post Reply
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

Love doesnt wanna accept my structure

Post by mk8 »

So Im trying to make a level system and this is how im doing it:

Code: Select all

levels = {
        {
            tileset = love.graphics.newImage("tiles_blue")
            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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 0, 0, 0, 0, 0, 0}
            }
      }
}
and when i try to run it Love says "line 9: you have to close the { from the line 7" (line 9 is the "map = {" line)
I kind of see why this is, cuz Love apparently thinks its a separate {} but I want it inside, how do I do dat?
EDIT: So to explain further I need it like this because later on I wanna add more tilesets and thus make different-colored levels & stuff, right now I could just delete the map inside the levels[0] structure but I wanna make the job easier for future me. The system is based on looping trough the table every love.draw() and if map[row][collumn] == 1 it will draw a tile there.
EDIT2: Also now I tried a structure like this

Code: Select all

levels = {
        {
            {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {1, 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, 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, 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, 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, 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, 0, 0},
            {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
            {0, 0, 0, 0, 1, 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, 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, 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}
        },
        
        {
            {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, 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, 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, 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, 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, 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, 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, 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, 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, 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, 0, 0, 0, 0, 0, 0}
        }
    }
and it worked fine for a few times but then I tried doing stuff with offsets and then removed it, and now when I do levels[currentLevel][row][collumn] it says "attempted to index a nil value".
EDIT3: (ik thats a lot of edits but no1s responding) I figured out that the "attempted to index a nil value" only happens when there is a 1 in the table (if it's all 0's, it's fine) but also, I'm not sure what the problem even is, because even if I do level[1][1] instead of level[variable][variable] (so it's not 0), it's still "indexing a nil value". I looked the error up in the forums but only found other "variations" of the error, not mine. The drawing code (which is causing the errors) is this:

Code: Select all

for y = 0, 15 do
        for x = 0, 15 do
            tableX = x + 1
            tableY = y + 1
            if level[tableY][tableX] == 1 then
                local up = tableY < 0 or level[tableY - 1][tableX] == 1
                . . .
(the last line always throws the error). I used the same code before and it didn't cause any trouble (and even did what it was supposed to do). Any1 help pls
EDIT4: Also, I noticed that the line before I use level[tableX][tableY] so it can't be because of the variables. I literally have no hint of an idea of what it could be
Last edited by mk8 on Thu Apr 22, 2021 12:17 pm, edited 3 times in total.
hippity hoppity ur code is my property (thanc in advanc)
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Love doesnt wanna accept my structure

Post by darkfrei »

Code: Select all

{tileset = love.graphics.newImage("tiles_blue")
map = map}
Don't forget the comma!

Code: Select all

{tileset = love.graphics.newImage("tiles_blue"),
map = map}
Lua starts from 1:

Code: Select all

local currentLevel = 1
local row = 1
local collumn = 1 -- maybe column? :)
local value = levels[currentLevel][row][collumn]
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

Re: Love doesnt wanna accept my structure

Post by mk8 »

Thanks, I think the problem was because of the comma. I was blind I didnt see. However, as I already edited the post, a NEW problem appeared out of nowhere. It's described in the main post. Could you help me out with that?
Last edited by mk8 on Thu Apr 22, 2021 12:12 pm, edited 1 time in total.
hippity hoppity ur code is my property (thanc in advanc)
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Love doesnt wanna accept my structure

Post by darkfrei »

You can use the cycles as:

Code: Select all

local levels = {} -- or level? :)
for y = 1, 16 do
  levels[y] = {} -- you are need to define empty table here
  for x = 1, 16 do
    -- your code here, for example value 0, 1 or 2:
    levels[y][x] = math.random (0, 2)
  end
end
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

Re: Love doesnt wanna accept my structure

Post by mk8 »

darkfrei wrote: Thu Apr 22, 2021 12:08 pm You can use the cycles as:

Code: Select all

local levels = {} -- or level? :)
for y = 1, 16 do
  levels[y] = {} -- you are need to define empty table here
  for x = 1, 16 do
    -- your code here, for example value 0, 1 or 2:
    levels[y][x] = math.random (0, 2)
  end
end
Wait I don't understand, I have the table predefined, I don't define a new one in the process. Could you explain it a bit further?
hippity hoppity ur code is my property (thanc in advanc)
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

Re: Love doesnt wanna accept my structure

Post by mk8 »

OK SO IM DONE, because I disabled the level[tableY - 1][tableX] and IT WORKED FINE AND THERE ARE ANOTHER 3 EXACT SAME LINES BUT IT'S JUST SMTH ABOUT THIS PARTICULAR LINE IM DONE
This is the function:

Code: Select all

function drawTiles(level)
    for y = 0, 15 do
        for x = 0, 15 do
            tableX = x + 1
            tableY = y + 1
            if level[tableY][tableX] == 1 then
                local up = tableY < 0 --or level[tableY - 1][tableX] == 1 <- this is the part I disabled
                local down = tableY + 1 > 16 or level[tableY + 1][tableX] == 1
                local left = tableX < 0 or level[tableY][tableX - 1] == 1
                local right = tableX + 1 > 16 or level[tableY][tableX + 1] == 1
                local upleft = tableY > 0 and tableX > 0 and up and left and level[tableY - 1][tableX - 1] ~= 1
                local upright = tableY > 0 and tableX + 1 < 16 and up and right and level[tableY - 1][tableX + 1] ~= 1
                local downleft = tableY + 1 < 16 and tableX > 0 and down and left and level[tableY + 1][tableX - 1] ~= 1
                local downright = tableY + 1 < 16 and tableX + 1 < 16 and down and right and level[tableY + 1][tableX + 1] ~= 1
                
                if upleft then
                  love.graphics.draw(tileset, upleftinnerQuad, x * 32, y * 32, 0, 2, 2, 4, 4)
                elseif not up and left then
                  love.graphics.draw(tileset, upQuad, x * 32, y * 32, 0, 2, 2, 4, 4)
                elseif up and not left then
                  love.graphics.draw(tileset, leftQuad, x * 32, y * 32, 0, 2, 2, 4, 4)
                elseif up and left then
                  love.graphics.draw(tileset, centerQuad, x * 32, y * 32, 0, 2, 2, 4, 4)
                elseif not up and not left then
                  love.graphics.draw(tileset, upleftQuad, x * 32, y * 32, 0, 2, 2, 4, 4)
                end
                
                if upright then
                  love.graphics.draw(tileset, uprightinnerQuad, x * 32 + 16, y * 32, 0, 2, 2, 4, 4)
                elseif not up and right then
                  love.graphics.draw(tileset, upQuad, x * 32 + 16, y * 32, 0, 2, 2, 4, 4)
                elseif up and not right then
                  love.graphics.draw(tileset, rightQuad, x * 32 + 16, y * 32, 0, 2, 2, 4, 4)
                elseif up and right then
                  love.graphics.draw(tileset, centerQuad, x * 32 + 16, y * 32, 0, 2, 2, 4, 4)
                elseif not up and not right then
                  love.graphics.draw(tileset, uprightQuad, x * 32 + 16, y * 32, 0, 2, 2, 4, 4)
                end
                
                if downleft then
                  love.graphics.draw(tileset, downleftinnerQuad, x * 32, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif not down and left then
                  love.graphics.draw(tileset, downQuad, x * 32, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif down and not left then
                  love.graphics.draw(tileset, leftQuad, x * 32, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif down and left then
                  love.graphics.draw(tileset, centerQuad, x * 32, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif not down and not left then
                  love.graphics.draw(tileset, downleftQuad, x * 32, y * 32 + 16, 0, 2, 2, 4, 4)
                end
                
                if downright then
                  love.graphics.draw(tileset, downrightinnerQuad, x * 32 + 16, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif not down and right then
                  love.graphics.draw(tileset, downQuad, x * 32 + 16, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif down and not right then
                  love.graphics.draw(tileset, rightQuad, x * 32 + 16, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif down and right then
                  love.graphics.draw(tileset, centerQuad, x * 32 + 16, y * 32 + 16, 0, 2, 2, 4, 4)
                elseif not down and not right then
                  love.graphics.draw(tileset, downrightQuad, x * 32 + 16, y * 32 + 16, 0, 2, 2, 4, 4)
                end
            end 
        end
    end
end
and now it works fine...
I solemnly dont kno
hippity hoppity ur code is my property (thanc in advanc)
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Love doesnt wanna accept my structure

Post by darkfrei »

mk8 wrote: Thu Apr 22, 2021 12:11 pm
darkfrei wrote: Thu Apr 22, 2021 12:08 pm You can use the cycles as:

Code: Select all

local levels = {} -- or level? :)
for y = 1, 16 do
  levels[y] = {} -- you are need to define empty table here
  for x = 1, 16 do
    -- your code here, for example value 0, 1 or 2:
    levels[y][x] = math.random (0, 2)
  end
end
Wait I don't understand, I have the table predefined, I don't define a new one in the process. Could you explain it a bit further?
Sorry, it was creating example.

You can read it just as:

Code: Select all

for y = 1, 16 do
  for x = 1, 16 do
    local value = levels[y][x]
    -- and other code here
  end
end
If you not sure that any column or row exists:

Code: Select all

for y = 1, 16 do
  for x = 1, 16 do
    local is_negative = levels[y] and levels[y][x] and (levels[y][x] < 0) or false
    -- and other code here
  end
end
See the situation:

Code: Select all

local y = 0
local tableY = y+1
local up = tableY < 0
The up is never true, (0+1) is always greater than 0.

I think that you need:

Code: Select all

-- here x and y are from 1 to 16:
local up = level[y-1] and level[y-1][x] and (level[y-1][x] ==1) or false
local down = level[y+1] and level[y+1][x] and (level[y+1][x] ==1) or false
local left = level[y] and level[y][x-1] and (level[y][x-1] ==1) or false
local right = level[y] and level[y][x+1] and (level[y][x+1] ==1) or false
Diagonals are simple:

Code: Select all

local upleft = up and left
and not (level[y-1] and level[y-1][x-1] and (level[y-1][x-1] ==1) or false)
It can be just a function:

Code: Select all

function is_one (level, x, y)
  return level[y] and level[y][x] and (level[y][x] == 1) or false
end
And call it just as

Code: Select all

local up = is_one (level, x, y-1)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
DrNefario
Prole
Posts: 6
Joined: Thu Apr 08, 2021 7:44 pm

Re: Love doesnt wanna accept my structure

Post by DrNefario »

mk8 wrote: Thu Apr 22, 2021 12:23 pm OK SO IM DONE, because I disabled the level[tableY - 1][tableX] and IT WORKED FINE AND THERE ARE ANOTHER 3 EXACT SAME LINES BUT IT'S JUST SMTH ABOUT THIS PARTICULAR LINE IM DONE
This is the function:

Code: Select all

function drawTiles(level)
		...
                local up = tableY < 0 --or level[tableY - 1][tableX] == 1 <- this is the part I disabled
             	...
 end
and now it works fine...
I solemnly dont kno
When tableY is 1, level[tableY - 1] is going to be off the start of the map, and therefore nil.
mk8
Prole
Posts: 34
Joined: Thu Apr 22, 2021 7:11 am

Re: Love doesnt wanna accept my structure

Post by mk8 »

darkfrei wrote: Thu Apr 22, 2021 12:25 pm I think that you need:

Code: Select all

-- here x and y are from 1 to 16:
local up = level[y-1] and level[y-1][x] and (level[y-1][x] ==1) or false
local down = level[y+1] and level[y+1][x] and (level[y+1][x] ==1) or false
local left = level[y] and level[y][x-1] and (level[y][x-1] ==1) or false
local right = level[y] and level[y][x+1] and (level[y][x+1] ==1) or false
Diagonals are simple:

Code: Select all

local upleft = up and left
and not (level[y-1] and level[y-1][x-1] and (level[y-1][x-1] ==1) or false)
It can be just a function:

Code: Select all

function is_one (level, x, y)
  return level[y] and level[y][x] and (level[y][x] == 1) or false
end
And call it just as

Code: Select all

local up = is_one (level, x, y-1)
TYSM ur awesome, I just overthought it because it was too much text overall and my brain just couldn't comprehend it
hippity hoppity ur code is my property (thanc in advanc)
Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests