Post subject: Trying out something in lua.
Editor, Skilled player (1202)
Joined: 9/27/2008
Posts: 1085
--===========================
local HoldKey= "space"
local ClearMouse= "Z"
--===========================
local ColorSticky= 0x00FF00FF  --green
local ColorHeld=   0xFFFFFFFF  --white
local ColorBoth=   0xFF0000FF  --red; Sticky and held together is unpressed
--===========================


local btn={"left","up","right","down","A","B","X","Y","L","R","start","select",
           "lid","debug"}
--*****************************************************************************
local function JoypadSet(inputs) -- Expects table containing keys
--*****************************************************************************
--I so wish FCEUX's "invert" option existed. This function mimics that.
--Best called from a function within emu.registerbefore
    local JoyWanted= joypad.peek()
    for i= 1, #btn do
        if type(inputs[btn[i]]) == "string" then
            inputs[btn[i]]= not JoyWanted[btn[i]]
        end
    end
    joypad.set(inputs)
end


local held, sticky= {}, {}
--*****************************************************************************
local function MessWithInput()
--*****************************************************************************
--For use with emu.registerbefore
--Checks for held and stickied keys and applies them
    local ThisInput= {}

    for i= 1, #btn do
        if sticky[btn[i]] then
            ThisInput[btn[i]]= not held[btn[i]]
        elseif held[btn[i]] then
            ThisInput[btn[i]]= "inv"
        end
    end
    JoypadSet(ThisInput)

    if sticky.x then
        stylus.set(     {touch= true, x= sticky.x, y= sticky.y} )
    elseif held.x then
        if not stylus.peek().touch then
            stylus.set( {touch= true, x= held.x  , y= held.y  } )
        end
    end

    sticky= {}
end
emu.registerbefore(MessWithInput)

local fc= emu.framecount()
local LastButtons= joypad.peek()
--*****************************************************************************
local function PeekAtInput()
--*****************************************************************************
--For use with gui.register
--Peeks at input if the emulation is paused, and handles stuff.
--Doubles as display for the input it messes with.
    local keys= input.get()

    local _fc= emu.framecount()
    if fc == _fc then
        local buttons= joypad.peek()

        for i= 1, #btn do
            if buttons[btn[i]] and not LastButtons[btn[i]] then
                if keys[HoldKey] then
                    held[btn[i]]= not held[btn[i]]
                else
                    sticky[btn[i]]= not sticky[btn[i]]
                end
            end
        end
        LastButtons= buttons

        buttons= stylus.peek()
        if buttons.touch then
            if keys[HoldKey] then
                held.x= buttons.x
                held.y= buttons.y
            else
                sticky.x= buttons.x
                sticky.y= buttons.y
            end
        end

    end
    fc= _fc

    if keys[ClearMouse] then
        held.x= nil
        held.y= nil
        sticky.x= nil
        sticky.y= nil
    end

    for i= 1, #btn do
        local color
        if sticky[btn[i]] then
            if held[btn[i]] then
                 color= ColorBoth
            else
                 color= ColorSticky
            end
        elseif held[btn[i]] then
            color= ColorHeld
        end
        if color then
            gui.text(1,10*i - 200, btn[i], color)
        end
    end
    if held.x then
        local color= 0xFFFFFFFF
        if sticky.x then color= 0xFF0000FF end
        gui.text(40,-185, held.x, color)
        gui.text(60,-185, held.y, color)
    end
    if sticky.x then
        gui.text(40,-170, sticky.x, 0x00FF00FF)
        gui.text(60,-170, sticky.y, 0x00FF00FF)
    end

end
gui.register(PeekAtInput)
This I call "sticky joypad". Save this into a .lua file and run it from DeSmuME's file menu, but if you needed me to tell you that, you probably need to learn a lot more about DeSmuME anyway. Pause emulation, tap a button, and it will be pressed when you finally frame-advance. This should help in making sure you don't overload the keyboard trying to press so darn many buttons at once. Also, hold space while pressing a joypad button, and it will act like the FCEUX-style auto-hold. This particular auto-hold can only be changed when emulation is paused, sorry about that. And whether you like it or not, running this script will also stick or hold stylus input. A key is assigned to clear the stylus input (how would one unclick the mouse effectively?), but change it to something other than Z if you wish. I sort of note a lack of a lua discussion thread, so I create my own topic to share my scripting stuff. Hope no one minds.
Post subject: Re: Trying out something in lua.
Emulator Coder, Skilled player (1310)
Joined: 12/21/2004
Posts: 2687
FatRatKnight wrote:
Pause emulation, tap a button, and it will be pressed when you finally frame-advance. This should help in making sure you don't overload the keyboard trying to press so darn many buttons at once.
This particular functionality is already in DeSmuME (and several other re-recording emulators), called the "Auto-Hold" hotkey. Originally it only supported the buttons but I think Gocha has added a stylus auto-hold feature as well. Of course if you want to be able to customize how these things work then Lua is a perfectly valid way of getting that.
Editor, Skilled player (1202)
Joined: 9/27/2008
Posts: 1085
Clearly, I missed the auto-hold function when first looking into the config stuff. That mistake shows here. There's still the sticky stuff I threw into the script, which provides a a different sort of convenience, since it only sticks for one frame. If nothing else, this helps to maintain some level of practice for me.
Joined: 1/26/2009
Posts: 558
Location: Canada - Québec
I didn't test your script but an autohold feature could be a nice addition for people tasing with joystick on other emulator.
Editor, Skilled player (1202)
Joined: 9/27/2008
Posts: 1085
It depends on whether input.get can pick up the joystick.
gui.register(function ()
    local count= 0
    local keys= input.get()
    for k,v in pairs(keys) do
        count= count+1
        gui.text(1,count*10,k)
    end
end)
Run this. See if the joystick stuff makes text appear on screen. Seems doubtful, unless the JoyToKey I hear about works in this case.
Emulator Coder, Skilled player (1310)
Joined: 12/21/2004
Posts: 2687
FatRatKnight wrote:
It depends on whether input.get can pick up the joystick. ... Run this. See if the joystick stuff makes text appear on screen. Seems doubtful, unless the JoyToKey I hear about works in this case.
This sort of thing is why joypad.peek was created, so you can ask the emulator which game buttons are being held down and it will figure out which keyboard keys or joypad buttons to check and check them for you. There's no need to bother checking input keys directly, that's only for things that don't correspond with game buttons. Try this instead, it should work even with buttons assigned to a joypad:
gui.register(function ()
    local count = 0
    local buttons = joypad.peekdown()
    for k,v in pairs(buttons) do
        count = count+1
        gui.text(1,count*10,k)
    end
end)
Editor, Skilled player (1202)
Joined: 9/27/2008
Posts: 1085
Joy! I now see the lua documentation specific for Gens! Actually, I've not been able to find any documentation on some of the latest lua functions, such as the stylus table for DeSmuME or joypad.peek on some emulators. I just looked at _G and worked my way from there, and I can at least show that in my starting post. Though, one question I do have: Which emulators currently support input.get but not joypad.peek? I'll be taking a look at this stuff and see what I can find out. The amount of stuff I haven't kept up to date on has clearly been shown here.