Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with a callback *RESOLVED*

BriarfoxBriarfox Mod
edited July 2013 in Questions Posts: 1,542

I know I had found a way to work this out but I have forgotten it. I'm basicly creating an instance of a class which has a self function I would like to pass to another class. This class returns values in the callback. Here is a very simplified version of what I am trying to do. I'm sure it's simple but i just cant see it. I hope this example explains it better then I'm trying to!


--# Main function setup() t = test() end function draw() background(40, 40, 50) end --# test test = class() function test:init() self.buffer = " " -- you can accept and set parameters here self.cbtest = callbacktest(self:callback) end function test:callback(txt) self.buffer = txt print(self.buffer) end --# callbacktest callbacktest = class() function callbacktest:init(cb) -- you can accept and set parameters here self.txt = "Hello World" self.cb = cb self.cb(self.txt) end

Comments

  • BriarfoxBriarfox Mod
    Posts: 1,542

    I updated the example to more accurately represent what I'm trying to do. I know I can do this with Globals but I'd really like to keep it within the class. This is to allow my ui button class to communicate with custom keyboards made up of the button class.

  • edited July 2013 Posts: 1,595

    Does self.cb = cb(self.text) work?
    Then call it by self.cb(), also do you not need to declare self.buffer as nil in init?

  • BriarfoxBriarfox Mod
    edited July 2013 Posts: 1,542

    Woops missed the self.buffer thanks. It basically can not pass the self:callback. It will not load saying that ( is expected near callback.

    I need the callback to access self.buffer. If I didn't I could just add the callback to the global callbacks table.

  • Jmv38Jmv38 Mod
    Posts: 3,295

    This does what you want i think (from initial example) 2 modifications.




    --# Main function setup()     t = test() end function draw()     background(40, 40, 50) end --# test test = class() function test:init()     -- you can accept and set parameters here     self.cbtest = callbacktest(self.callback) -- replace the : by . end function test:callback(txt)     print(txt) end --# callbacktest callbacktest = class() function callbacktest:init(cb)     -- you can accept and set parameters here     self.txt = "Hello World"     self.cb = cb     self.cb(nil,self.txt) -- cb = test.callback has 2 inputs: (self,text). self is not used so can be nil end
  • BriarfoxBriarfox Mod
    Posts: 1,542

    I ended up just assigning the callback function to a self variable. Seemed to fix the issue. I believe its similar to what you did @Jvm38. Thank you



    --# Main function setup() t = test() end function draw() background(40, 40, 50) end --# test test = class() function test:init() self.buffer = "Foo " self.cb = function(txt) print(self.buffer..txt)end -- you can accept and set parameters here self.cbtest = callbacktest(self.cb) end --# callbacktest callbacktest = class() function callbacktest:init(cb) -- you can accept and set parameters here self.txt = "Bar" self.cb = cb self.cb(self.txt) end
  • BriarfoxBriarfox Mod
    edited July 2013 Posts: 1,542

    @Jvm38 your example works well, however I did not have a correct example. The callback needs to be able to access self. When the callback is called and nil is passed it is unable to access self.

  • Posts: 90

    It sounds like what you want is a method bound to self that can be passed around like a normal function. Read this thread (about half way down to @toadkick's entry for a function named "bind"):

    http://twolivesleft.com/Codea/Talk/discussion/2509/can-you-use-a-method-as-a-variable#Item_7

    Then where you said this:

        self.cbtest = callbacktest(self:callback)
    

    (which is illegal Lua), say this instead:

        self.cbtest = callbacktest(bind(self, "callback"))
    

    Will that do the trick?

  • BriarfoxBriarfox Mod
    Posts: 1,542

    Thanks @starblue great post on creating a bind function. Do you know if there is any real difference from:

    self.cbtest = function(txt) self.something = txt end
    

    This seems to pass without error and has access to self.

  • Posts: 175

    @Briarfox Effectively bind does this :) self.callback = function(...) return self:mycallback(...) end also is this what your looking for :)?

Sign In or Register to comment.