Random number keeps generating

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
Chaoselite
Prole
Posts: 5
Joined: Fri Sep 02, 2011 4:55 am

Random number keeps generating

Post 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
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Random number keeps generating

Post 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.
User avatar
Chaoselite
Prole
Posts: 5
Joined: Fri Sep 02, 2011 4:55 am

Re: Random number keeps generating

Post 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.
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: Random number keeps generating

Post by GijsB »

table[number]
User avatar
Chaoselite
Prole
Posts: 5
Joined: Fri Sep 02, 2011 4:55 am

Re: Random number keeps generating

Post 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?
Rad3k
Citizen
Posts: 69
Joined: Mon Aug 08, 2011 12:28 pm

Re: Random number keeps generating

Post 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?
User avatar
Chaoselite
Prole
Posts: 5
Joined: Fri Sep 02, 2011 4:55 am

Re: Random number keeps generating

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Random number keeps generating

Post 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.
Help us help you: attach a .love.
User avatar
Chaoselite
Prole
Posts: 5
Joined: Fri Sep 02, 2011 4:55 am

Re: Random number keeps generating

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

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot], Semrush [Bot], snibo and 7 guests