How do I make the draw function just draw the image and stop?

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
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

How do I make the draw function just draw the image and stop?

Post by dyl4n130 »

I am messing around with random map generation and right now just to get it down I'm trying to make something that will randomly place 10 objects across 10 tiles with a 50% chance of each tile containing an object. It should check every tile and randomly chose between 0 and 1. If 0 then do nothing and if 1 then place a tile.

Here is the code I am using:

Code: Select all

for i = 0, 9 do
	if love.math.random(0, 1) == 1 then
		love.graphics.draw(wall, tilex(i), tiley(0))
	end
end
This would randomly place them across the top of the screen, but since this is in the love.draw function, it runs this every tick and the objects jump around. So every frame it re places the objects. How do I make it not do that?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: How do I make the draw function just draw the image and stop?

Post by MrFariator »

You need to have some way to track that the map has already been generated. Additionally you'll need some way to store the result, so that you can draw it later at will.

Examples:

Code: Select all

-- Somewhere outside love.update and love.draw; you could also make this a global by omitting the "local" if you so wish
local mapTable

-- Inside love.update
if not mapTable then -- check that the table is nil and then generate it
  mapTable = {}
  for i = 0, 9 do
    mapTable[i] = love.math.random(0, 1) 
  end
end

-- Inside love.draw
if mapTable then -- Only draw the map if it has been generated
  for i = 0, 9 do
    if mapTable[i] ~= 0 then
      love.graphics.draw(wall, tilex(i), tiley(0))
    end
  end
end
If you then want to re-generate the map, then just set the mapTable to nil.

Also you're better off leaving love.draw only to handle drawing graphics onto the screen, and put any generation or other things that need to be calculated per frame in love.update. For later stuff, if you want to use lua's builtin table functions later it's better to index everything starting from 1 as opposed to 0.
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: How do I make the draw function just draw the image and stop?

Post by dyl4n130 »

MrFariator wrote: Mon Aug 07, 2017 3:21 am You need to have some way to track that the map has already been generated. Additionally you'll need some way to store the result, so that you can draw it later at will.

Examples:

Code: Select all

-- Somewhere outside love.update and love.draw; you could also make this a global by omitting the "local" if you so wish
local mapTable

-- Inside love.update
if not mapTable then -- check that the table is nil and then generate it
  mapTable = {}
  for i = 0, 9 do
    mapTable[i] = love.math.random(0, 1) 
  end
end

-- Inside love.draw
if mapTable then -- Only draw the map if it has been generated
  for i = 0, 9 do
    if mapTable[i] ~= 0 then
      love.graphics.draw(wall, tilex(i), tiley(0))
    end
  end
end
If you then want to re-generate the map, then just set the mapTable to nil.

Also you're better off leaving love.draw only to handle drawing graphics onto the screen, and put any generation or other things that need to be calculated per frame in love.update. For later stuff, if you want to use lua's builtin table functions later it's better to index everything starting from 1 as opposed to 0.
thank you for the reply. I know this is going to seem like a super basic question but can you explain the whole 'table' thing? I am mostly self taught in love2d and i have never used such a thing. i am not aware what the whole "mapTable" is. is it arrays? a few years ago i took an intro to c++ class but i remember very little. i will also try this code and see it works, thanks again
dyl4n130
Prole
Posts: 24
Joined: Sat Jul 29, 2017 6:48 am

Re: How do I make the draw function just draw the image and stop?

Post by dyl4n130 »

Okay I skimmed through the code again and it seemed to refresh my memory on arrays, correct me if I am wrong but the way I see it basically runs the for loop and stores the random values in the 9 different cells of the array and then references them again in order while drawing the images?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: How do I make the draw function just draw the image and stop?

Post by MrFariator »

Yep, that's the gist of it. The terminology may be confusing, as what table and array are kinda depends on the code language in question. For example C/C++ has arrays but no "table" type as that term is usually reserved for 2D arrays. In lua everything is just a table given it's the sole container type variable you can have.

The lua docs are very good learning material, and here's a handy tutorial on lua tables. I recommend reading 'em through.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 122 guests