简体   繁体   中英

functions,tables and for in Lua

Right now I'm doing some tests but I cant seem to find what is wrong with this code - any idea?

function IATetris(Pieza,Rotacion,Array)
io.write("The table the script received has: ",Pieza,"\n")
RotacionInicial = Rotacion
PosInicial = 7
final = #Array --this gets the size of the array
i = 1

    for y = 1, 20 do --the array of my tetris board is 20 in x and 14 in y so it would be something like this Array2D[20][14]

    io.write(" First for y ",y,"\n")
    Array2D[y]={} --clearing the new array
    for x = 1,14 do
    io.write(" First for x ",x,"\n")
        if i == final then break end

        io.write(" First for i",i,"\n")
        Array2D[y][x] = Array[i] 
        i= i+1 --seems like you cant use i++ in lua
        end
   end
end

What I'm doing is getting 2 integers and 1 Array. I'm having to write in the console to check where the program is actually going, and what I'm getting is...

The first log message: "The table the script received has: "

and the second log message: " First for y "

But I don't get any further than those so probably the program is crashing there? This function is being called like every 20 seconds or so. I have really no idea why this is happening. Any help would be really appreciated, thank you.

If this line logs:

io.write(" First for y ",y,"\n")

and this line does not log:

io.write(" First for x ",x,"\n")

Then the problem is on one of these lines:

Array2D[y]={} --clearing the new array
for x = 1,14 do

for x... definitely works for me, so I'd suggest it's the Array2D line. There is nothing syntactically wrong with it, so it must be a runtime error. Runtime errors should be reported by Lua or the application it is embedded into. If they aren't and the function just "stops" then you are debugging blind, and you will waste a lot of time on problems like this.

The only error I can think might happen on that line would be if Array2D is not a table. Since you are trying to index it, it needs to be. Array2D isn't declared in your function, this is fine if it is a global variable that is already defined elsewhere. However if it is meant to be a local variable just for this function then you should add local Array2D = {} to it.

Without knowing what Array2D is, or what your actual error is even, it's hard to give a more accurate answer. If you really have no better method of finding out the problem than logging, this, just before the Array2D line, should test my hypothesis:

io.write("Array2D is: ", type(Array2D), "\n")

Looks like Array2D is not initialized (or not a table), so it craps on Array2D[y]={} .

You can use pcall to call a function and trap errors, like this:

local ok, msg = pcall(IATetris, pieza, rotacion, array)
if not ok then
    print("ERROR:", msg)
end

Side note: you should be using the local keyword whenever possible to limit the scope of your variables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM