Adding a table to my map table and accessing said table.

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
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

Adding a table to my map table and accessing said table.

Post by Delta9 »

To start, i am new to lua but have some experience scripting c# in RunUO. What I have done is through the tutorials and some posts I read, and what I have works perfectly so far, simple map, birds eye, with a player icon I can move about. Now I need to add a list of "stats" to each tile based on which graphic it is. I am stumped and would truly appreciate a point in the right direction at least. Also, I need to know the method for accessing these "stats" from other functions. Thanks in advance
~Delta

Code: Select all

	
function setupMap()
  mapWidth = 128
  mapHeight = 128
  
  map = {}
  for x=1,mapWidth do
    map[x] = {}
    for y=1,mapHeight do
      if math.random(1,100) <= 90 then  --Make tile grass, very likely 
      map[x][y] = 6
      elseif math.random(1,100) <= 60 then --Make tile forest, less likely
      map[x][y] = 2
      
      else map[x][y] = math.random(0,7) end --else pick a random tile, including grass and forest
  end
end
so how would I make each tile hold a table of variables and access them?
spir
Citizen
Posts: 76
Joined: Wed Oct 17, 2012 1:12 pm

Re: Adding a table to my map table and accessing said table.

Post by spir »

Delta9 wrote:To start, i am new to lua but have some experience scripting c# in RunUO. What I have done is through the tutorials and some posts I read, and what I have works perfectly so far, simple map, birds eye, with a player icon I can move about. Thanks in advance
~Delta

Code: Select all

	
function setupMap()
  mapWidth = 128
  mapHeight = 128
  
  map = {}
  for x=1,mapWidth do
    map[x] = {}
    for y=1,mapHeight do
      if math.random(1,100) <= 90 then  --Make tile grass, very likely 
      map[x][y] = 6
      elseif math.random(1,100) <= 60 then --Make tile forest, less likely
      map[x][y] = 2
      
      else map[x][y] = math.random(0,7) end --else pick a random tile, including grass and forest
  end
end
Hello Delta, and welcome to the wonderful Lua-land!

First, a few points about the piece of code you posted:
  • It wouldn't work because an 'end' was missing (for the inner 'for', I guess). Be slightly more consistent with indentation, it will help much; especially, it is better that either all branches of an if..elseif..end are indented blocks, or none. Exemple (I use tabs of width 3).

    Code: Select all

    function setupMap()
       mapWidth = 128
       mapHeight = 128
       map = {}
       
       for x=1,mapWidth do
          map[x] = {}
          for y=1,mapHeight do
             if math.random(1,100) <= 90 then      --Make tile grass, very likely
                map[x][y] = 6
             elseif math.random(1,100) <= 60 then  --Make tile forest, less likely
                map[x][y] = 2
             else           --else pick a random tile, including grass and forest
                map[x][y] = math.random(0,7)
             end
          end
       end
    end
    
  • Is your map a set of columns, or of rows? If of rows, then the first index should be called 'y' (or use i,j instead of x,y to avoid confusion with pixel coords).
  • You may make your map a table with proper data stored on it, directly (instead of wandering around as global vars, or hidden infuncs as you do here). For instance:

    Code: Select all

    local map = {
       width    = 128 ,
       height   = 128 ,
       -- random factors
       ...
    }
    
  • I find your way to create random elements weird --but I may be wrong. Here is the way I'd do it, using a single toss (it may be what you search, or not)

    Code: Select all

          for y=1,mapHeight do
             local toss = random (1, 100)
             if     toss < 60 then <grass>
             elseif toss < 90 then <forest>
             else   ...
             end
             ...
          end
    
Delta9 wrote: Now I need to add a list of "stats" to each tile based on which graphic it is. I am stumped and would truly appreciate a point in the right direction at least. Also, I need to know the method for accessing these "stats" from other functions.

so how would I make each tile hold a table of variables and access them?
Now, to the point. Simply make your tiles tables... Since Lua is very open & flexible, you have at least 3 possibilities. I'll take the example that in addition to the terrain type, you want to store (a) the creature on it, it any (else nil) (b) how many times a creature stayed on it:

1. A tile is a tuple (an unnamed record): each info corresponds to an index:

Code: Select all

   tile = {"grass", monster1, 3}
   ...
   terrain = tile[1]
   creature = tile[2]
2. A tile is an object/record/named tuple: each info has a field with its proper identifier:

Code: Select all

    tile = {terrain="grass", creature=monster1, stay_count=3}
   ...
   terrain = tile.terrain
   creature = tile.creature
3. Mix. Since the terrain is always here, you may make it the implicit item #1:

Code: Select all

   tile = {"grass", creature=monster1, stay_count=3}
   ...
   terrain = tile[1]
   creature = tile.creature
If you don't know how to fill the map with such tiles, ask more. Idem about accessing the tiles from outside the map.

Finally, to go back to a possible table representing the map itself, this example of structures for tiles may let you guess you have at least 2 possiblities: the matrix (of colums or rows themselves) holding tiles either is an explicit field ('matrix' or 'tiles' or 'colums' or 'rows'), or it is implicitely built as indexed elements (the array part of the table). Hum, hop I'm clear.

Denis
... la vita e estrany ...
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

Re: Adding a table to my map table and accessing said table.

Post by Delta9 »

Thats great man, thanks. I know my logic and structure are probably not perfect, this is my first project in Lua and I am using Love2D to learn it. Your examples were not the perfect solution for my problem but it sure did point out the things I was missing, so thank you very much, I am on the right path now. I figured out that if I just build a parallel table when creating my map table I could store my data separate from the actual quad map and reference it by player position in the move and update functions.

Thanks again, you have done me a great service. Leaving the thread open in case I come upon any more issues with this, will mark solved upon completion.
Delta9
Prole
Posts: 11
Joined: Mon Nov 12, 2012 5:01 am

Re: Adding a table to my map table and accessing said table.

Post by Delta9 »

okay, I have worked over some things here, have created a new table that holds stats, basically an overlay of my map, full of info. I did this to keep my tilemap static and unchanged.

I chose to maintain my randoms and tiles as personal preference being similar to c#.
as you can see I make map{} and mapdata{} together so they match dimensions with any sized map generated

Code: Select all

function setupMap()
  mapWidth = 128
  mapHeight = 128
  
  mapdata = {}
  map = {}
  for x=1,mapWidth do
    map[x] = {}
    mapdata[x] = {}
    for y=1,mapHeight do
      if math.random(1,100) <= 95 then  -- make grass, 95% chance
      	map[x][y] = 6
      	mapdata [x][y] = {
      			name = "Grass Land",
      			tra = 1			
      		}
      elseif math.random(1,100) <= 60 then  -- Make forest, 60% chance IF grass fails 
      	map[x][y] = 2
      	mapdata [x][y] = {
      			name = "Forest",
      			tra = 2			
      		}
      else 
      	map[x][y] = math.random(0,7) -- else random tile including forest and grass
    	mapdata[x][y] = {
    		name = "other",
    		tra = 1
    	}
    end
  end
end-- success, perhaps clean it up a bit?



Now, I navigate my map via player.grid_x,player. grid_y as seen in tutorials.

so how do I print/access the mapdata.x.y.name with player.grid_x and y?

I am sure this is trivial and I am just missing something stupid, but I just spent about four hours trying to work through this, so now I must turn to you, the professionals.

Thanksagain ~Delta9
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests