running a function only once

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
silverknight108
Prole
Posts: 17
Joined: Wed Oct 30, 2013 5:24 pm

running a function only once

Post 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
User avatar
clofresh
Citizen
Posts: 87
Joined: Sun Jul 26, 2009 4:21 pm
Contact:

Re: running a function only once

Post 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.
----------------------------------------
Sluicer Games
User avatar
silverknight108
Prole
Posts: 17
Joined: Wed Oct 30, 2013 5:24 pm

Re: running a function only once

Post by silverknight108 »

what example please :x
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: running a function only once

Post 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
User avatar
silverknight108
Prole
Posts: 17
Joined: Wed Oct 30, 2013 5:24 pm

Re: running a function only once

Post by silverknight108 »

does it rely on the system time cause when i ran it twice it put the same maze
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: running a function only once

Post by Plu »

It should automatically seed the randomiser and generate a different one each time.
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: running a function only once

Post 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.
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: running a function only once

Post by Roland_Yonaba »

Inny wrote:We really need a lodash library for Lua.
Would moses be a good candidate for that ? :3
User avatar
Inny
Party member
Posts: 652
Joined: Fri Jan 30, 2009 3:41 am
Location: New York

Re: running a function only once

Post 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".
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: running a function only once

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

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests