Making a failproof button

Im using npryces controller class, and a joystick from that class. I don’t know how to incorporate a button in to the class which can be used at the same time as using the joystick, because I noticed that it overrides the touched function which means it cannot be used in the main script, what can I do to make this work?

It seems that the controller class wants to make it easy to use exactly one controller. I’d suggest a quick hack (I don’t consider this to be a fix, that would require more restructuring):


-- original, in Controller.lua
function Controller:activate()
    touched = function(t)
        self:touched(t)
    end
end

-- a bit better
function Controller:activate()
    local oldtouched = touched
    touched = function(t)
        self:touched(t)
        oldtouched(t)
    end
end

This is some sort of friendly override as it keeps the original touched function running. You should now be able to use multiple controllers as well as completely unrelated code the relies on the pure touched function.

Quickly rethinking my previous answer:

The controller framework requires you to explicitly call the draw function of the respective controller, do the same with the touched function, i.e. do not call activate but call the controller’s touched function from the global touched. This is also less of a hack.


function setup()
    controller = . . .
    -- don't call activate
    -- controller:activate()
end

function draw()
    controller:draw()
end

function touched(t)
    controller:touched(t)
end

This introduces a nice symmetry, you explicitly call draw and also touched. You now also have control over the touched function (the global one and the controller’s one). You understand better what’s going on because you explicitly keep the controller running.

And while you’re at it, give the specific controller’s instance a telling name, e.g. if you create a stick call it a stick.

That’s actually not a bad way of going around it. And to be honest i didn’t even see that before so thanks, ill try and implement and idea based on that. Should help a lot because I was getting quite annoyed with there not being a touch function over than the controller