Page 2 of 2

Re: [help] Trying to learn Lua + LOVE doing tutorials

Posted: Fri Sep 09, 2011 8:27 am
by Strelok
Ok! I think I got it! :o

Code: Select all

function love.load()
   map_display_h = 15
   map_display_w = 15
   tile_w = 16
   tile_h = 16

   --NOTE: X comes before Y in the creation part so the array becomes map[X][Y] and not map[Y][X]
   map = {}
   for x=1, map_display_w do
      map[x] = {}
      for y=1, map_display_h do                                                         
         map[x][y] = {
            hit = math.random(0,1)
         }
      end
   end
At first we declare our variables inside the function love.load(). Those variables will say the size of the grid map and size of the tiles.

Then the second part of the code does the loop part. It'll use the variables storing the grid size in the loop process, the first for will loop 16 times the second for that'll also loop 16 times that code that says "hit = math.random(0,1)" although I don't know what it mean and does. Also why the second loop has "[x][y]" other than just "[y]" on it?

Code: Select all

function love.draw()
   for y=1, map_display_h do -- this y=1 means that we're setting the variable y with 1 and using it on map[x][y] like x=1 in the second for?
      for x=1, map_display_w do -- 
         if map[x][y] == 1 then
            love.graphics.setColor(0,0,0)
         else
            love.graphics.setColor(255,255,255)
         end
         love.graphics.rectangle("fill", x + map_offset_x, y + map_offset_y)
      end
   end
end
The first for in this function stores the second for, that'll change the color of the rectangle based in a random number. That just clarifies me that "hit = math.random(0,1)" a bit, it does the random part and it stores it in map[x][y] I think, then every time a number is stored in map[x][y] it'll generate a new tile in the exact place where this random number designed, then the tile will be draw by the love.draw function after it get it's color set.

What I didn't understand about "hit = math.random(0,1) is that:
Wont it generate numbers over 16?
Does it have a chance to generate the same numbers more than once?
Why (0,1) not (0,0)?

Other than that, thanks for this help. It clarifies thinks a LOT for me now. Really. :awesome:

Re: [help] Trying to learn Lua + LOVE doing tutorials

Posted: Fri Sep 09, 2011 8:53 am
by Taehl
I'm glad we could help!

By the way, Strelok, the STALKER games use Lua as their scripting language (and boy, do they have a lot of scripts). They're generally not very easy to follow, though.

Re: [help] Trying to learn Lua + LOVE doing tutorials

Posted: Sat Sep 10, 2011 8:59 pm
by Robin
Strelok wrote:What I didn't understand about "hit = math.random(0,1) is that:
Wont it generate numbers over 16?
Does it have a chance to generate the same numbers more than once?
Why (0,1) not (0,0)?
hit = math.random(0,1) makes hit randomly either 0 or 1. So no, yes, because (0, 0) always gives 0.

Re: [help] Trying to learn Lua + LOVE doing tutorials

Posted: Mon Sep 12, 2011 5:59 am
by Strelok
Oh! Sorry I didn't know about how it worked. I thought it was going to generate random numbers to (0, 1) for the zero and for the one. I didn't knew. Anyway, that clarifies a lot the code. I think I understand it completely now. :awesome: