Ninjakid
Ninjakid

Reputation: 73

lua for loop usage

I am making an 2D mining game, and i want to draw the map with two for loops. It would look something like this:

for(int y = 0; y < height; y++){
    for(int x = 0; x < width; x++){
        //create map chunk
    }
}

I know that is incorrect syntax for lua (it is actually c++ syntax). I don't know if there is a way to use for loops like this in lua. Also if there is another way to accomplish the same task without manually drawing out each chunk, that would be very helpful. Thanks

Upvotes: 2

Views: 872

Answers (1)

Vlad
Vlad

Reputation: 5847

You can use numeric for loop.

for y=1,height do
    for x=1,width do
        -- create map chunk
    end
end

Note that I started counding from 1. Lua's numeric for includes the last value, i.e. height/width will be reached. And Lua normally indexes arrays starting from 1, not from 0.

Upvotes: 2

Related Questions