Log In  

Hi, i'm working on a project with this function;

function amount_living_neighbours(x, y, board)
    local res = 0
    for i= -1, 1 do 
        for j = -1,1 do
            if i == 0 and j == 0 then goto continue end
            if x+i >= 0 and x+i <= 127 and y+j >= 0 and y+j <= 127 then
                if board[x+i][y+j] == white then
                    res += 1
                end
            end
            ::continue::
        end
    end
    return res
end

and the interpreter tells me that i'm attempting to index a field ? in the line "if board[x+i][y+j] == white then " i.e it's null. Should'nt lua interpret it as a parameter and not as a nil? i'm not acquantained to the language and seems pretty nasty to use globals with this code.

P#81025 2020-08-21 23:01

Are you sure you're setting the board argument to a table value? I tested your function in my copy of PICO-8 along with setting up a 2D board array and the white global, and it seems to work just fine:

white=7
b={}
for x=0,127 do
    b[x]={}
    for y=0,127 do b[x][y]=white end
end

function amount_living_neighbours(x, y, board)
    local res = 0
    for i= -1, 1 do 
        for j = -1,1 do
            if i == 0 and j == 0 then goto continue end
            if x+i >= 0 and x+i <= 127 and y+j >= 0 and y+j <= 127 then
                if board[x+i][y+j] == white then
                    res += 1
                end
            end
            ::continue::
        end
    end
    return res
end

?amount_living_neighbours(0,0,b)

Running this code prints 3, as they're 3 neighboring "white" pixels in the very top-left of my 2D array.

How are you setting up the board on your end, and how are you calling this function?

P#81028 2020-08-22 00:10

Thanks for the response, i found the mistake. I was initializing the table correctly but the if condition tries to access to board[0]; that provoques the nil. It's hard to get used to the tables starting at 1 xD

P#81056 2020-08-22 21:28

So the problem was, were you initializing the array starting at 1, but read the array starting at 0?

What programming languages besides Lua have you came from?

P#81069 2020-08-23 04:05 ( Edited 2020-08-23 04:06)

Python, mainly. The lists are easier than the tables in Lua :/

P#81293 2020-08-28 12:41
1

@qequ

Think of tables in Lua as a superset of lists, sets, arrays, and dictionaries. You can basically use them in place of any of those more-strictly-defined things.

As for the 1-based thing, yeah, that sucks, but it is what it is at this point. You can always treat them as 0-based if you do it manually. For instance, you can convert this 1-based initialization:

t = {3,4,5}

To 0-based by using the feature that allows initializing specific keys, to set the first value at t[0], and the rest as usual starting at t[1] and going up:

t = {[0]=3,4,5}

Lua won't consider t[0] part of what it calls its "sequence" elements, i.e. the stuff from t[1] to t[#t] that can be manipulated with del(t,?) or all(t) or ipairs(t), but you can if you handle all array stuff manually, like you might in C/C++. You just have to be careful not to rely on #t, as it doesn't know about t[0]. Track the count manually with a t.n value, or test for nil as a sentinel value:

for i=0,t.n-1 do
  ...
end

for i=0,32767 do
  if(t[i]==nil) break
  ...
end

It's surprising, though, just how often you really don't need 0-based indexing. As someone who grew up on C/C++, I adore 0-based indexing and it took me a long time to come around, but there's just a ton of stuff where the index doesn't really matter, only the order of the indices.

P#81332 2020-08-29 08:04 ( Edited 2020-08-29 08:18)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2022-12-05 02:00:57 | 0.019s | Q:17