I'm making a tile-based game. For the tiles, I'm using two separate arrays for X and Y, and the movement works just as I want. But the problem is this:
I want each tile to have properties such as wethrr it is a solid tile,it's sprite,percentage of oxygen (I'll try to simulate atmospherics) etc. I can't assign properties to each tile since X and Y are separate arrays, so I wanted to make it a 2D array. I declared "map" as map = {{}}, but when I try to assign a value to map[5][3], map[5][3] returns nil and I can't assign anything to it.
Could someone help me?
[SOLVED]How to handle tile generation
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
[SOLVED]How to handle tile generation
Last edited by pauls313 on Thu Sep 08, 2016 6:58 pm, edited 1 time in total.
Re: How to handle tile generation
Well you only declared 1 element for map array, the empty sub-array. You'll need to create a sub-array for each element of root array to create a grid-like structure.
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: How to handle tile generation
Before you can assign values to coordinates, you need to "lay" the ground. "map = {{}}" basically states you have a map with width 0 and height 0, which is definitely not the case. What you may want to do is assign each tile their values beforehand, that is, while loading the map:
This "TILE" can be a string, number or a table with all the properties. For example, instead of TILE, you'd have:
This, of course, will vary on each tile, so it'll depend heavily on how you're loading/saving maps. If you're setting them manually, you'll probably want to assign "default" values to the whole map before. For example:
Then going through the tiles you want to change:
Code: Select all
map = {} --Assigns the variable
for x = 1, mapwidth do --Iterates through every vertical line of your map
map[x] = {} --This will later be filled with all the tiles in that particular column
for y = 1, mapheight do --Iterates through every horizontal line inside each column
map[x][y] = TILE
end
end
Code: Select all
{oxygen = 10, sprite = mySprite, solid = false}
Code: Select all
{oxygen = 0, sprite = false, solid = false}
Code: Select all
map[5][3].solid = true
map[5][3].sprite = groundImage
Re: How to handle tile generation
It would be easier to help you if you attached a .love or at least a snippet of the code in question. Anyways, after a glance I think the problem is your not defining map[x][y] as a nested table...
On that note, it has been proven that using a 1d array is much faster with around half the memory usage. Check here or here for similar topics.
Code: Select all
local map = {}
for x = 1, 10 do
map[x] = {}
for y = 1, 10 do
map[x][y] = {
solid = false,
sprite = 'blank',
potayto = 'potahto'
--etc
}
end
end
Re: How to handle tile generation
Thanks guys, that was exactly what I needed. Now I can properly work on my game.
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: How to handle tile generation
If you want to use the 1d method suggested by Beelz, you will need to do some "converting":
If you want to set a tile based on its coordinates, you'd do this:
For the other way around (getting coordinates from the index), you do this:
But we're glad to help! I suggest you add "[SOLVED]" to your thread's title, so other people with similar problems might find this solution more easily
Code: Select all
map = {}
for i = 1, mapwidth*mapheight do
map[i] = TILE
end
Code: Select all
local i = x + (y-1)*mapwidth
map[i] = PROPERTIES
Code: Select all
local y = math.ceil(i/mapwidth)
local x = i - (y-1)*mapwidth
-
- Party member
- Posts: 234
- Joined: Mon Aug 29, 2016 8:51 am
Re: How to handle tile generation
And for the way you want to store data for each tile, you will need a 3D array.
- Daniel Eakins
- Citizen
- Posts: 99
- Joined: Thu Jul 18, 2013 9:14 pm
- Location: France
Who is online
Users browsing this forum: No registered users and 78 guests