[SOLVED]How to handle tile generation

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
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

[SOLVED]How to handle tile generation

Post by pauls313 »

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?
Last edited by pauls313 on Thu Sep 08, 2016 6:58 pm, edited 1 time in total.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: How to handle tile generation

Post by raidho36 »

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.
User avatar
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

Post by HugoBDesigner »

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:

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
This "TILE" can be a string, number or a table with all the properties. For example, instead of TILE, you'd have:

Code: Select all

{oxygen = 10, sprite = mySprite, solid = false}
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:

Code: Select all

{oxygen = 0, sprite = false, solid = false}
Then going through the tiles you want to change:

Code: Select all

map[5][3].solid = true
map[5][3].sprite = groundImage
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: How to handle tile generation

Post by Beelz »

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...

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
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

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
pauls313
Citizen
Posts: 50
Joined: Tue Aug 02, 2016 3:07 am

Re: How to handle tile generation

Post by pauls313 »

Thanks guys, that was exactly what I needed. Now I can properly work on my game.
User avatar
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

Post by HugoBDesigner »

If you want to use the 1d method suggested by Beelz, you will need to do some "converting":

Code: Select all

map = {}
for i = 1, mapwidth*mapheight do
	map[i] = TILE
end
If you want to set a tile based on its coordinates, you'd do this:

Code: Select all

local i = x + (y-1)*mapwidth
map[i] = PROPERTIES
For the other way around (getting coordinates from the index), you do this:

Code: Select all

local y = math.ceil(i/mapwidth)
local x = i - (y-1)*mapwidth
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 :nyu:
@HugoBDesigner - Twitter
HugoBDesigner - Blog
KayleMaster
Party member
Posts: 234
Joined: Mon Aug 29, 2016 8:51 am

Re: How to handle tile generation

Post by KayleMaster »

And for the way you want to store data for each tile, you will need a 3D array.
User avatar
Daniel Eakins
Citizen
Posts: 99
Joined: Thu Jul 18, 2013 9:14 pm
Location: France

Re: How to handle tile generation

Post by Daniel Eakins »

Post Reply

Who is online

Users browsing this forum: No registered users and 143 guests