Buttons touch function problem

So, as long as the functions called by the touched function have the same name for touch, then it should be fine, correct?

So this would be okay:

function touched(t)
button:touched(t)
button2:touched()
end

function button:touched(touch)
print(touch.x)
end

function button2:touched(t)
print(t.x)
end

Because currently, this is what is happening with my code.

Your button2 code won’t work because you don’t pass the touch variable

The function will work, the call will not, just to clarify.

But it is currently working, is there something going on?

function touched(t)
    VirtualStick:touched(t)
    Buttons:touched()
end

function Button:touched(touch)
    
    if pressedField(self.x,self.y,self.width,self.height) == true then
        self.pressed = "Yes"
    end
    if releasedField(self.x,self.y,self.width,self.height) == true and self.pressed == "Yes" then
        self.released = "Yes"
        self.pressed = "No"
    end
    
    if self.released == "Yes"
    then if self.buttonFunction01 == nil
        then self.buttonFunction()
            else
            if self.buttonFunction04 ~= nil
            then self.buttonFunction(self.buttonFunction01,self.buttonFunction02,self.buttonFunction03,self.buttonFunction04)
            end
            if self.buttonFunction03 ~= nil
            then self.buttonFunction(self.buttonFunction01,self.buttonFunction02,self.buttonFunction03)
            end
            if self.buttonFunction02 ~= nil
            then self.buttonFunction(self.buttonFunction01,self.buttonFunction02)
            end
            if self.buttonFunction01 ~= nil
            then self.buttonFunction(self.buttonFunction01)
            end
        end
    end
    
    self.released = "No"
    
    if CurrentTouch.state == ENDED then
        self.pressed = "No"
    end
    
end

function VirtualStick:touched(t)
    local pos = touchPos(t)
    local goodZone = self:check(t)
    
    if t.state == BEGAN and self.touchId == nil and goodZone then
        self.touchId = t.id
        self.touchStart = pos
        self.stickOffset = vec2(0, 0)
        self.pressedCallback()
        elseif t.id == self.touchId then
        if t.state == MOVING then
            self.stickOffset = clampLen(pos - self.touchStart, self.radius)
            self.steerCallback(self:vector())
            elseif t.state == ENDED or t.state == CANCELLED then
            self:reset()
            self.releasedCallback()
        end
    end
end

Currently, this is part of the code that is working perfectly as intended.

Well, that is simply because Button:touched(touch) doesn’t actually use the touch parameter passed to it, it uses CurrentTouch instead, so it doesn’t matter if you don’t pass touch.

I suggest you clean up your code, either using the touch parameter or Currenttouch but not mixing them up, or you will continue to be confused.

Thanks for the advice.

Yeah - like @Ignatz said - if possible avoid the use of CurrentTouch.

As for your button2:touched() function, it’s a “feature” of Lua that it won’t complain if you don’t pass enough (or too many) parameters to a function.

Any that aren’t passed are simply given the value of nil and any extra ones are ignored, so if you don’t pass a value and try and access the parameter, then Lua will probably through a nil reference error

function touched(t)
button:touched(t)        -- fine : t is passed
button2:touched()       -- wrong : nothing is passed so lua will pass "nil" for you
end

function button:touched(touch)
print(touch.x)
end

function button2:touched(t)
print(t.x)  -- lua will report an error as t is nil
end

@CayDay47 Actually, the only function that needs to be call touched is the original function “touched”. The functions called from “touched” can be called anything you want. Also, the receiving parameter in those functions can be called anything you want. Just as long as you use the correct names, it doesn’t matter what you use.


function setup()
    button1=button()
    button2=button()
end


function touched(t)
    button:calledAnything(t)
    button2:calledSomething(t)
end

button=class()

function button:calledAnything(whatEver)
    print("whatEver",whatEver.x)
end

function button:calledSomething(whoCares)
    print("whoCares",whoCares.x)
end

Thanks for the code demo, @dave1707, it also made me realise what happens with the stuff in the brackets in the touched and subsequent functions. Thanks again.