Page 1 of 2

running a function only once

Posted: Fri Aug 22, 2014 1:10 pm
by silverknight108
hello all you i need to run a function only once for a pathfinding test.The game works by genarating obstacles for my pathfinder but it genarates a map like10 times every second. Here's my code

Code: Select all

function love.load()
function dg()
h,w = love.window.getHeight,love.window.getWidth
for i = 0,12 do
for h = 0,12 do
t = love.math.random(1,2)

if t == 1 then

love.graphics.rectangle("line",i*45,h*45,45,45)

else

love.graphics.rectangle("fill",i*45,h*45,45,45)
end

end
end






end


end

function love.update()

end

function love.draw()
gen = true
if gen == true then
gen = false
dg()

end
end

Re: running a function only once

Posted: Fri Aug 22, 2014 1:52 pm
by clofresh
You're setting gen = true at the beginning of the love.draw function so the if statement will always pass and call dg. I think what you want to do is to generate the rectangles in love.load and save them to tables, then just draw those rectangles in the draw function.

Re: running a function only once

Posted: Fri Aug 22, 2014 1:56 pm
by silverknight108
what example please :x

Re: running a function only once

Posted: Fri Aug 22, 2014 2:12 pm
by Plu
The most straightforward example possible. (You can probably do this much cleaner.)

On loading; make the line/fill variables and stuff them in a table. When drawing, reload the line/fill from the table and use it to draw each block.

If you don't understand what is happening, let me know I'll explain more.

(Like everything I post; untested function. If it gives an error, try to solve it yourself as it's probably just a typo)

Code: Select all

function love.load()
	blocks = {}
	makeBlocks()
end

function makeBlocks()
	h,w = love.window.getHeight,love.window.getWidth
	for i = 0,12 do
		blocks[i] = {}
		for h = 0,12 do
			t = love.math.random(1,2)
			if t == 1 then				
				love.graphics.rectangle("line",i*45,h*45,45,45)
				blocks[i][h] = "line";
			else
				love.graphics.rectangle("fill",i*45,h*45,45,45)
				blocks[i][h] = "fill";
			end	
		end
	end
end

function drawBlocks()
	h,w = love.window.getHeight,love.window.getWidth
	for i = 0,12 do
		for h = 0,12 do
			love.graphics.rectangle(blocks[i][h],i*45,h*45,45,45)
		end
	end
end

function love.update()

end

function love.draw()
	drawBlocks()
end

Re: running a function only once

Posted: Fri Aug 22, 2014 2:19 pm
by silverknight108
does it rely on the system time cause when i ran it twice it put the same maze

Re: running a function only once

Posted: Fri Aug 22, 2014 2:26 pm
by Plu
It should automatically seed the randomiser and generate a different one each time.

Re: running a function only once

Posted: Sat Aug 23, 2014 5:55 pm
by Inny

Code: Select all

function only_run_once(func)
    return function(...)
        if func then
            local f = func
            func = nil
            return f(...)
        end
    end
end

my_function = only_run_once(function(...) return do_stuff(...) end)
We really need a lodash library for Lua.

Re: running a function only once

Posted: Sun Aug 24, 2014 11:19 am
by Roland_Yonaba
Inny wrote:We really need a lodash library for Lua.
Would moses be a good candidate for that ? :3

Re: running a function only once

Posted: Sun Aug 24, 2014 3:48 pm
by Inny
Roland_Yonaba wrote:
Inny wrote:We really need a lodash library for Lua.
Would moses be a good candidate for that ? :3
I like what I see, except for this:

Code: Select all

function _.once(f)
local _internal = 0
local _args = {}
return function(...)
_internal = _internal+1
if _internal<=1 then _args = {...} end
return f(unpack(_args))
end
end
This will run the function every time it's called with the arguments from the first call. This doesn't "only run the function once".

Re: running a function only once

Posted: Sun Aug 24, 2014 7:10 pm
by Roland_Yonaba
Inny wrote:This will run the function every time it's called with the arguments from the first call. This doesn't "only run the function once".
Well, that was intended. Moses was kind of inspired by Underscore.js library, at first, where _.once is implemented as a function for lazy initialization. Hence the actual Lua implementation. I do admit the name _.once is a bit misleading, though. :)