clbk_consumebufferedkey functions merging

misha.physics

Well-known member
Joined
Dec 22, 2021
Messages
1,480
Reaction score
2,210
Points
128
Location
Lviv
Preferred Pronouns
he/him
I have two "clbk_consumebufferedkey" functions.

First:
C++:
function clbk_consumebufferedkey(key, down, kstate)
  if not down then
    return false
  end
  if oapi.keydown(kstate, OAPI_KEY.G) then
    GearStatus = revert_status(GearStatus)
    return true       
  end
    return false
end
Second:
C++:
function clbk_consumebufferedkey(key, down, kstate)
  if oapi.keydown(kstate, OAPI_KEY.LCONTROL) then   
    if oapi.keydown(kstate, OAPI_KEY.L) then
      if LightSwitch == false then
      LightSwitch = true
      else LightSwitch = false
      end

      LeftLight.active = LightSwitch
      RightLight.active = LightSwitch
      TailLight.active = LightSwitch
    end   
  end
end
Separately they work correctly, but if I place them one after another, then only the last is applied. How can I merge them into one "clbk_consumebufferedkey" function? There is an example of the HST.lua for multiple key commands:
C++:
function clbk_consumebufferedkey(key, down, kstate)
    if not down then -- only process keydown events
        return false
    end

    if oapi.keydown(kstate, OAPI_KEY.LCONTROL) or oapi.keydown(kstate, OAPI_KEY.RCONTROL) then
        if key == OAPI_KEY.KEY1 then     -- deploy/retract antenna
            ant_status = revert_status(ant_status)
            return true
        elseif key == OAPI_KEY.KEY2 then -- open/close hatch
            hatch_status = revert_status(hatch_status)
            return true
        elseif key == OAPI_KEY.KEY3 then -- open/fold solar arrays
            array_status = revert_status(array_status)
            return true
        end
    end
    return false
end
But I don't understand how to do it for my case.
 
Separately they work correctly, but if I place them one after another, then only the last is applied.
Remember Highlander? "There can be only one" ;)
If you define 2 functions with the same name (and parameter signature) how would the computer know what function to call when you call it?
Even in Object-Oriented languages that allow "function overloading" (multiple functions with the same name) - those must differ in the signature!

I would have thought that the Lua interpreter would not allow this; but it seems that, in case of re-definition of a function, it is simply replaced.

In your specific example, do as @Thunder Chicken said: combine the body of both into one function.
 
Thank you all. I didn't think about the same function name. It works now:
C++:
function clbk_consumebufferedkey(key, down, kstate)
  if oapi.keydown(kstate, OAPI_KEY.G) then
    GearStatus = revert_status(GearStatus)    
  end

  if oapi.keydown(kstate, OAPI_KEY.LCONTROL) then  
    if oapi.keydown(kstate, OAPI_KEY.L) then

      if LightSwitch == false then
      LightSwitch = true
      else LightSwitch = false
      end

      LeftLight.active = LightSwitch
      RightLight.active = LightSwitch
      TailLight.active = LightSwitch
    end  
  end
end
 
Back
Top