Post subject: Lua Script: How to reset joypad to normal
Joined: 3/26/2014
Posts: 2
I'm writing a lua script that sends joypad input during parts of the game and returns control back to me during other parts of the game. I can't figure out how to do this though based on the joypad documentation. Here is an example script:
function testing(allow)
    local keytable = joypad.get()

    if allow then
        for key, _ in pairs(keytable) do
            keytable[key] = nil
        end
    else
        keytable["P1 Left"] = "False"
        keytable["P1 Right"] = "True"
    end
    
    joypad.set(keytable)
end

framecount = 0
while true do
    if framecount > 100 then
        testing(true)
    else
        testing(false)
        framecount = framecount + 1
    end
	emu.frameadvance()
end
What I expect this script to do: Hold right on the joypad for 100 frames, then allow manual control on every frame after that. What it actually does: Holds right for 100 frames, then only allows manually moving right on every frame after that. Also, even if I stop the script, I still don't have full control back, I have to restart BizHawk completely. What am I doing wrong here?
Editor, Skilled player (1202)
Joined: 9/27/2008
Posts: 1085
After a short talk with adelikat, BizHawk doesn't implement "False" correctly in joypad.set (it sticks the button as unpressed for an extended time even after the script has ended). This should be fixed soon. Until then, use nil and avoid manually pressing the buttons you don't want when the script is doing its thing.
Joined: 3/26/2014
Posts: 2
Ah, that makes sense. I hadn't tried using nil instead of "False", that should work for me. Thanks!
Editor, Emulator Coder
Joined: 8/7/2008
Posts: 1156
Depending on your point of view, this may never have been fixed. Things are complicated. Use this pattern: OnFrameStart: joypad.get (will be unmolested physical values) Calculate new buttons joypad.set (new buttons) OnFrameEnd: joypad.set({Start="",Up="", etc)) --"rests joypad to normal" -- this actually clears the stickies set by OnFrameStart, but the timing is important and must be done by OnFrameEnd MainLoop: while true do emu.frameadvance