How to pass a function by refference

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Rob.m
Prole
Posts: 23
Joined: Mon Mar 11, 2013 1:57 am

How to pass a function by refference

Post by Rob.m »

Hi,

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
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?
User avatar
pakoskyfrog
Citizen
Posts: 62
Joined: Mon Feb 04, 2013 12:54 pm
Location: France

Re: How to pass a function by refference

Post by pakoskyfrog »

Hellö !

Functions are in lua, just like any variables. However if you use the () syntax you will make a function call.
So, either you pass the function as it is : button.click = mouseclick
or you make a function that will return a function :

Code: Select all

local factory = function (btn)
    return function (x, y, b)
        -- your code here
    end
end

button.click = factory(button)
dummy = button.click
Actually this assign button.click into the global variable 'dummy' (and that's ugly:P)

And i think you should use some kind of OOP with the ':' and the self. For instance : (and if i understood you well, this should do what you intended to)

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.x = 0 -- not needed since you already have x=0 in button = { ... }
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()
  button:click()
  -- equivalent to button.click(button)
  --  or mouseclick(button)
end

function mouseclick(self)
  self.x = 100
  self.text = "Clicked"
end
This is not tested but should work anyway ^^
User avatar
Kadoba
Party member
Posts: 399
Joined: Mon Jan 10, 2011 8:25 am
Location: Oklahoma

Re: How to pass a function by refference

Post by Kadoba »

Rob.m wrote: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
That is because as soon as the button.x is set to 100 when calling mouseclick() it's immediately set it to 0 afterwards.
Rob.m wrote:It is evaluating mouseclick(button) with the statement button.click = mouseclick(button)

and dummy = button.click doesn't seem to be doing anything.
The function is being called but it doesn't return anything so dummy is set to nil.
Rob.m wrote: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?
In Lua functions are first-class citizens so the concepts of pass-by-reference and pass-by-value don't really apply. It's a little bit of both.

I'm not completely sure what your code is suppose to do so I'm going to assume you just mean to record and print a message at the mouse click location. If that's the case this should work:

Code: Select all

local button = {text = "Hello World", x = 0, y = 0, r = 0, sx = 1, sy = 1, ox = 0, oy = 0, kx = 0, ky = 0}

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(x,y,mb)
    button.x = x
    button.y = y
    button.text = "Clicked"
end
I hope that helps.
Post Reply

Who is online

Users browsing this forum: slime and 89 guests