Page 1 of 1

Random number keeps generating

Posted: Fri Sep 02, 2011 4:59 am
by Chaoselite
So I'm trying to make a procedural dungeon generator, but the problem is my number generator keeps generating new numbers and there for it keeps moving tiles. Is there anyway I can just make numbers that it generates static? I've tried storing them in a table then accessing the table but that didn't work. The code is posted below.

Code: Select all

function dungeongen()
grass = love.graphics.newImage("data/tile0.png")
stone = love.graphics.newImage("data/tile2.png")
math.randomseed(love.timer.getTime())

cell = {}
	for  y=0, 32 do
		for  x=0,32 do
			cell[x] = {love.graphics.draw(grass, x*32, y*32)}
			
		end	
	end	
	for i=0, 16 do
		local x = math.random(1, 64 )
		local y = math.random(1, 64 )
		love.graphics.draw(stone, x*32, y*32)
	end
end

Re: Random number keeps generating

Posted: Fri Sep 02, 2011 6:46 am
by nevon
You need to understand that everything (except love.load) is in a loop. So if you call dungeongen() in love.draw, that code is going to be run several times per second. That means that you're reloading those images several times per second, and those random values are also reset several times per second.

The way to do it would be to generate a table in love load, containing all those values (a map, if you will). Then you can iterate over that table in love.draw when you want to draw it.

Re: Random number keeps generating

Posted: Fri Sep 02, 2011 8:13 pm
by Chaoselite
I put it in a table but I'm not to sure how to call it as the random variable how would I do that? Sorry I'm a little noobish to lua.

Re: Random number keeps generating

Posted: Fri Sep 02, 2011 8:25 pm
by GijsB
table[number]

Re: Random number keeps generating

Posted: Fri Sep 02, 2011 8:33 pm
by Chaoselite
Damnit... I was using {} instead of [] to get the number silly me. Thank you so much.

I got another question, how do I get the x, y position of a drawn graphic?

Re: Random number keeps generating

Posted: Sat Sep 03, 2011 1:08 pm
by Rad3k
Chaoselite wrote:I got another question, how do I get the x, y position of a drawn graphic?
Don't you need the position to draw the graphic in the first place?

Re: Random number keeps generating

Posted: Sat Sep 03, 2011 4:16 pm
by Chaoselite
Yeah, but the way I'm doing it currently isn't working when I'm trying to draw more tiles to make a room. Take a look and tell me what I'm doing wrong cause clearly this isn't working the way it's intended.

Code: Select all

function dungeongen()

floor= {}
	for i=1, 26 do
		 x = Randomnumber[i]
		 y = Randomnumber[i+1]
			for p=1, x/16 do
				for p2=1, y/16 do
				floor[i] = love.graphics.draw(stonetile, x*32, y*32)
			end
		end
	end
	
end
Nevermind, Fixed it wrong algorithm. It was suppose to be (x*32)+(p*32), once again, silly me.

Re: Random number keeps generating

Posted: Sat Sep 03, 2011 4:39 pm
by Robin

Code: Select all

floor[i] = love.graphics.draw(stonetile, x*32, y*32)
This is madness. love.graphics.draw draws an image, it doesn't create a tile or something. As you can see on the wiki, it returns nothing.

Re: Random number keeps generating

Posted: Sat Sep 03, 2011 5:01 pm
by Chaoselite
Robin wrote:

Code: Select all

floor[i] = love.graphics.draw(stonetile, x*32, y*32)
This is madness. love.graphics.draw draws an image, it doesn't create a tile or something. As you can see on the wiki, it returns nothing.
Wow, didn't even see that it returned nothing, I'll just have it return the co-ordinates of the tile.