Question on Callback functions

I have been having trouble with callback functions in classes. How can I have a callback function that is part of a class. When I use self:callback it requires that I write self:callback(). This launches the call back before I even change the parameter. I’ve always hacked my way around it but why is this?

Test = class()

function Test:init(x)
    -- you can accept and set parameters here
    self.x = x
    parameter.action("test",self:callback())
end

function Test:callback()
    print("callback called")
end


The reason it launches the callback is because…well, you’re calling the callback yourself. I think what you intended was something like

 parameter.action("test", self.callback)

but that won’t work because self is not actually bound to the callback function, so if you try to use ‘self’ from within your callback you’ll crash. I think what you want to do is this:

parameter.action("test", function() self:callback() end)

Beware though…parameters are bound to global variables, so if you create more than one Test object, only the last one created will have it’s function bound to the parameter.

Incidentally, believe it or not, I’m wrapping up something I’ve been working feverishly on that allows you to create parameters for object variables (not just global variables). I am very excited about it, and I think it might be just the thing to solve this. I should have it wrapped up sometime tomorrow morning, I’ll let you know when it’s up.

Thanks @toadkick. That make a lot of sense. It was driving me nuts. Looking forward to seeing your parameter code.

I would love local parameters @Toadkick go ahead!
@Briarfox you know, we all went through that. If you want some additionnal very usefull advice, belive me, always do:
setContex(img) ... setContext().
And:
pushMatrix() pushStyle() .... PopStyle() PopMatrix() .
Always… This will save you a lot of headaches.

@jmv38 I’ve never really used setContext() only when playing with an image in a tutorial. Are there any other uses that I’m not realizing?

Also in a web cam app I’m working on I am having trouble with a callback firing when its loaded. The parameter is created based on user input from a table. Each parameter needs a separate callback. I opted for putting the callback directly into the parameter. However it fires when its created. I’m guessing its becuase I’m passing it the state. Any advice on how to fix this? Its several user generated parameters deep so its making my head spin. I really may just be better off making a screen ui for it.

for i,j in pairs(self.ports) do
parameter.boolean(i,true,function(v) 
                                    if v == true then 
                                    self:alarmOn(i) 
                                    else self:alarmOff(i) end end)
end

SetContext: i use it all the time because it is much faster (in many cases) to precompute the image outside the draw then to sprite it inside the draw.
Parameter fires at craetion: yes, and this is how i workaround it:


function Window:settingsSpecialMenu()
    self.parameterFirstPass = true
    
    parameter.integer("globalVar",mini,maxi,startValue,
        function(v) 
            if self.parameterFirstPass then return end
            -- here i do my action
        end)

    self.parameterFirstPass = false
end

This is not teh most beautiful code ever, but it works fine

@Jmv38 - What’s the maximum sized image/buffer SetContext() can generate? I’m sure there was something on here that indicated there’s a maximum size? Hmmmm :-/

I think it is 2048x2048? Or maybe just the screen size? But setContext is faster only if you draw small things, if you draw a full image theN it might not be so good. And setContext is per se of course slower that than 1 draw, but if image is complex to build, then if you sprite the image it can be faster over many frames.

Thanks Jvm, I was trying to do something similar but with the unknown number of parameters and parameter names it was not looking good. I just got to figure it out :slight_smile:

Alright guys…I think this is about ready to go. I’m going to wait until tomorrow to open up a thread, because I really want to write some documentation first, but for now I’ll put it out there in case you’re feeling brave.

Introducing tparameter:
https://gist.github.com/apendley/5401761

tparameter piggybacks onto Codea’s parameter system to allow you to bind parameters to arbitrary tables, not just _G (like Codea’s parameters). It also features a couple of new types of psuedo-parameters, and supports metavariables (yeah, I know, that stuff is probably meaningless at the moment without documentation). Hopefully you can figure out how to use it by the example code and by running the program. Sorry there’s no documentation yet, but I said I’d post the code today, and I think I just barely made it!

Thank you @Toadkick. While you are on it, could you add a pushParameters() and popParameters() function? That could be useful for nested test menus.

@Jmv38: unfortunately, no, because the system is not stack based, it’s list based (well, sort of). However, you don’t need a stack to provide parameters for each menu…if you’ll notice in the example, every Ellipse creates it’s own parameters in the Ellipse:init() method. Every object that add’s parameters becomes selectable in the object parameters selector. So, if all of your menus create their own parameters, it’s simply a matter of choosing the one you want in the selector.

Ok. Sound so coool! But i’ll wait for your doc to dig into it.

I’ve written up a lot of info in http://twolivesleft.com/Codea/Talk/discussion/2615/introducing-tparameter%3A-parameters-for-any-table#Item_1 and honestly I’m probably not going to do too much more.

I’ve designed the interface to be as similar to Codea’s parameter interface as possible, so it really should be simple to use. Grab the example project and run it and you’ll get an idea of most of the things you can do, as well as how to do them. I’d appreciate any feedback, bug reports, questions, etc, so please give it a look! :slight_smile: