Out of Bounds Tile?

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
User avatar
Chiffon
Prole
Posts: 2
Joined: Thu Jul 10, 2014 3:12 am
Location: Montréal, Quebec

Out of Bounds Tile?

Post by Chiffon »

Hello! This is my first post, although I used to be here on the forums in 2010 (username Maggots, forgot my password and sadly lost whichever e-mail I used for that, so I guess it's a lost cause). I had coded some kind of timer module, I wonder if it's still posted. But I digress. :awesome:

I've been coding a type of tile-based roguelike game, and I have some map generation going on (not for dungeons, but outdoors terrain: hills, forests...) and it's very rudimentary. My problem is that I have a function called Map:placeNode(x, y, range, type) that should place a type of terrain, say forest, on a certain tile and surrounding it. It goes fine, but every few times I regenerate, it gives me this error:

Code: Select all

map.lua:107: attempt to index a nil value
What I presume is, despite my Map:isWithinBounds(x, y) check, it attempts to interact with an out of bounds tile... Here are the functions I use to place terrain

Code: Select all

function Map:placeNode(x, y, range, type)
	self.tiles[x][y].type = type

	local startx = x - range
	local starty = y - range

	local endx = x + range
	local endy = y + range

	for x2 = startx, endx, 1 do
		for y2 = starty, endy, 1 do

			if self:isWithinBounds(x2, y2) then

				local d = (math.abs(x2-x) + math.abs(y2-y))/2
				local luck = 0.5^d

				if love.math.random() <= luck then self.tiles[x2][y2].type = type end

			end

		end
	end
end

function Map:isWithinBounds(x,y)
	if x > 0 and y > 0 and x <= self.width-1 and y <= self.width-1 then
		return true
	else return false end
end
and the function I use to decide where and when to actually place tile terrain:

Code: Select all

function Map:placeNode(x, y, range, type)
	self.tiles[x][y].type = type

	local startx = x - range
	local starty = y - range

	local endx = x + range
	local endy = y + range

	for x2 = startx, endx, 1 do
		for y2 = starty, endy, 1 do

			if self:isWithinBounds(x2, y2) then

				local d = (math.abs(x2-x) + math.abs(y2-y))/2
				local luck = 0.5^d

				if love.math.random() <= luck then self.tiles[x2][y2].type = type end

			end

		end
	end
end

function Map:isWithinBounds(x,y)
	if x > 0 and y > 0 and x <= self.width-1 and y <= self.width-1 then
		return true
	else return false end
end
N.B.
  • The first tile is at (x, y) = (1, 1) and they go all the way up to (width, height) of the map.
  • If you want my .love file, my code isn't optimized and is probably in all honestly a disgusting sight. Prepare barf-bags.
If need be, I can post my .love file. :) Thanks a lot, guys!
User avatar
HugoBDesigner
Party member
Posts: 403
Joined: Mon Feb 24, 2014 6:54 pm
Location: Above the Pocket Dimension
Contact:

Re: Out of Bounds Tile?

Post by HugoBDesigner »

Not sure if that's just a typo or if you made that on purpose, but on the function "isWithinBounds" you made this:
if x > 0 and y > 0 and x <= self.width-1 and y <= self.width-1 then

When it should be this:
if x > 0 and y > 0 and x <= self.width-1 and y <= self.height-1 then

If that's not the case, I'd like to know which line is the one in the error you get. I hope I helped...
@HugoBDesigner - Twitter
HugoBDesigner - Blog
User avatar
Chiffon
Prole
Posts: 2
Joined: Thu Jul 10, 2014 3:12 am
Location: Montréal, Quebec

Re: Out of Bounds Tile?

Post by Chiffon »

I love you. :) It was just a typo, jesus...

Well, that's solved! I think. I'll let you know

EDIT: Solved! That was easy.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest