OOP Question - Passing an object method as a function parameter?

I’m trying to pass an object method as a parameter, essentially as a callback function for a network request in another class.

[lua]

function ItemManager:loadData()
    --print(tostring(self:getItemData))

    --cant pass with the : sytax
    Parse.getItems(self.getItemData)
end

[/lua]

This in turn should call the following function/method when the network request is finished.

[lua]

function ItemManager:getItemData(data)
    print("ItemManager: data - "…data.response)
    self.data = data.response
end

[/lua]

The problem is that I can’t pass the function as a method with the : syntax, and if I attempt to pass it with a .  it doesn’t fire the call.

I need getItemData to be a method because it needs to access the object’s properties.

Does anybody know how to handle this situation; or perhaps an alternative way of dealing with this?

Many thanks…

Solution is simple, just wrap your function using colon with anonymous function. Then call this wrapper function which will execute your code.

Thanks for your input, however I still can’t get this to work. I changed it to an anonymous function, like so:

[lua]

function ItemManager:loadData()
    --print(tostring(self:getItemData))
    Parse.getItems(
    function()
        self:getItemData()
    end)
end

[/lua]

However it’s not firing. The only way I’ve managed to get the callback to work is by having a local function like so:

[lua]

local function getItemData(data)
    print("ItemManager: data - "…data.response)
    self:populateData(data.response) 
end

function ItemManager:loadData()
    --print(tostring(self:getItemData))
    Parse.getItems(getItemData)
end

[/lua]

However I need getItemData to be a method as the call to self:populateData won’t work otherwise?!?

Looking further it seems as the anonymous function is being called immediately, perhaps due to the asynchronous nature of network.request.

You put it in wrong place as I see… it’s hard to say because I dont know what your functions do and cannot see data structure.

ItemManager is a basic class - it’s interacting with Parse.com to download a bunch of objects in a Parse.com class. The only real thing that is happening here is passing a callback to a function that makes a query to Parse.com via a network.request() call.

I’m a little confused by what you mean “… wrong place”; all I’m trying to do is pass a callback that refers to a method of ItemManager; however the anonymous function approach doesn’t work (well it seems to fire immediately with event == nil). The only approach that works so far is to have the callback as the local/private function.

Can I just ask where the anonymous call should go? Many thanks for your help.

But shouldn’t function you pass to Parse take an argument?
In your not-working example you made anonymous function but it never takes argument!

I do not know what structure Parse returns but you never pass data from it to your function.
If so it should be

local function func(dataFromParse) self:getItemData(dataFromParse) end Parse.getItems(func)

Thank you Piotrz - the noob problem was that I hadn’t specified the arguments in the anonymous callback function, once I changed that everything is working as expected.

Many thanks…

Solution is simple, just wrap your function using colon with anonymous function. Then call this wrapper function which will execute your code.

Thanks for your input, however I still can’t get this to work. I changed it to an anonymous function, like so:

[lua]

function ItemManager:loadData()
    --print(tostring(self:getItemData))
    Parse.getItems(
    function()
        self:getItemData()
    end)
end

[/lua]

However it’s not firing. The only way I’ve managed to get the callback to work is by having a local function like so:

[lua]

local function getItemData(data)
    print("ItemManager: data - "…data.response)
    self:populateData(data.response) 
end

function ItemManager:loadData()
    --print(tostring(self:getItemData))
    Parse.getItems(getItemData)
end

[/lua]

However I need getItemData to be a method as the call to self:populateData won’t work otherwise?!?

Looking further it seems as the anonymous function is being called immediately, perhaps due to the asynchronous nature of network.request.

You put it in wrong place as I see… it’s hard to say because I dont know what your functions do and cannot see data structure.

ItemManager is a basic class - it’s interacting with Parse.com to download a bunch of objects in a Parse.com class. The only real thing that is happening here is passing a callback to a function that makes a query to Parse.com via a network.request() call.

I’m a little confused by what you mean “… wrong place”; all I’m trying to do is pass a callback that refers to a method of ItemManager; however the anonymous function approach doesn’t work (well it seems to fire immediately with event == nil). The only approach that works so far is to have the callback as the local/private function.

Can I just ask where the anonymous call should go? Many thanks for your help.

But shouldn’t function you pass to Parse take an argument?
In your not-working example you made anonymous function but it never takes argument!

I do not know what structure Parse returns but you never pass data from it to your function.
If so it should be

local function func(dataFromParse) self:getItemData(dataFromParse) end Parse.getItems(func)

Thank you Piotrz - the noob problem was that I hadn’t specified the arguments in the anonymous callback function, once I changed that everything is working as expected.

Many thanks…