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