How to pass a function by refference
Posted: Mon Mar 18, 2013 1:19 am
Hi,
I have this code -
What I get here is the text ;clicked; at the x position 0 before I click. then when I click nothing happens. I expected the x pos to be set to 100
It is evaluating mouseclick(button) with the statement button.click = mouseclick(button)
and dummy = button.click doesn't seem to be doing anything.
From wat I have read, simple single dimentional variables are passed as a value and anything else (data structures) are passed by refference.
It seems that a function that is refferenced at the time of entry to a data structure is evaluated at that time and that time only.
So how do I pass a function by refference alone?
I have this code -
Code: Select all
function love.load()
button = {text = "Hello World", x = 0, y = 0, r = 0, sx = 1, sy = 1, ox = 0, oy = 0, kx = 0, ky = 0}
button.click = mouseclick(button)
button.x = 0
end
function love.draw()
local ob = button
love.graphics.print(ob.text, ob.x, ob.y, ob.r, ob.sx, ob.sy, ob.ox, ob.oy, ob.kx, ob.ky)
end
function love.mousepressed()
dummy = button.click
end
function mouseclick(lob)
lob.x = 100
lob.text = "Clicked"
end
It is evaluating mouseclick(button) with the statement button.click = mouseclick(button)
and dummy = button.click doesn't seem to be doing anything.
From wat I have read, simple single dimentional variables are passed as a value and anything else (data structures) are passed by refference.
It seems that a function that is refferenced at the time of entry to a data structure is evaluated at that time and that time only.
So how do I pass a function by refference alone?