Daren Page
Daren Page

Reputation: 1

LUA deprecated functions

I have a LUA script for MAME that rotates my screen using two .PS1 scripts to rotate a physical screen between two different orientations.

It took ages to get it running without duplicating commands, now it runs flawlessly, (or so I think).

The issue I have now is with deprecated commands: emu.register_start(function() emu.register_stop(function()

Which are replaced with: emu.add_machine_reset_notifier emu.add_machine_stop_notifier

I cant seem to get them to work without breaking my code.

Has anyone had any luck with this?

This is the current code:

local exports = {}
exports.name = "screenrotator"
exports.version = "0.0.1"
exports.description = "Rotate Display"
exports.license = "The BSD 3-Clause License"
exports.author = { name = "Daren" }

-- Define file locations for portrait and landscape scripts
local portraitScript = "D:\\Games\\MAME\\mame0263b_64bit\\plugins\\ScreenRotator\\portrait.ps1"
local landscapeScript = "D:\\Games\\MAME\\mame0263b_64bit\\plugins\\ScreenRotator\\landscape.ps1"

-- Flag to track if the plugin start function has been called
local startCalled = false

-- Define the plugin functions
function exports.startplugin()
    -- Check if the start function has already been called
    if not startCalled then
        -- Set flag to true to indicate that the start function has been called
        startCalled = true
        
        emu.register_start(function()
            local screenrotation = manager.machine.system.orientation
            print(screenrotation)
            if screenrotation == "rot270" or screenrotation == "rot90" then
                print("Vertical game. Rotating...")
                os.execute("powershell.exe -file " .. portraitScript)
            else
                print("Horizontal game. Not Rotating...")
            end
        end)

        emu.register_stop(function()
            local screenrotation = manager.machine.system.orientation
            print(screenrotation)
            if screenrotation == "rot270" or screenrotation == "rot90" then
                print("Vertical game. Rotating back...")
                os.execute("powershell.exe -file " .. landscapeScript)
            else
                print("Horizontal game. No need to rotate back.")
            end
        end)
    else
        print("Start function already called. Skipping...")
    end
end

-- Call the plugin initialization function
exports.startplugin()

return exports

Upvotes: 0

Views: 229

Answers (1)

Joe
Joe

Reputation: 1

You need to declare two variables outside the startplugin function to store the notifier functions. Within startplugin, assign these notifier functions to the variables.

As a side note, you can use relative paths instead of absolute paths so the plugin will continue to work even if the MAME folder location changes.

local exports = {}
exports.name = "screenrotator"
exports.version = "0.0.1"
exports.description = "Rotate Display"
exports.license = "The BSD 3-Clause License"
exports.author = { name = "Daren" }

-- Define file locations for portrait and landscape scripts
local portraitScript = "./plugins/ScreenRotator/portrait.ps1"
local landscapeScript = "./plugins/ScreenRotator/landscape.ps1"

-- Flag to track if the plugin start function has been called
local startCalled = false

local rotateScreenOnResetNotifier
local revertScreenOnStopNotifier

-- Define the plugin functions
function exports.startplugin()
    -- Check if the start function has already been called
    if not startCalled then
        -- Set flag to true to indicate that the start function has been called
        startCalled = true

        rotateScreenOnResetNotifier = emu.add_machine_reset_notifier(function()
            local screenrotation = manager.machine.system.orientation
            print(screenrotation)
            if screenrotation == "rot270" or screenrotation == "rot90" then
                print("Vertical game. Rotating...")
                os.execute("powershell.exe -file " .. portraitScript)
            else
                print("Horizontal game. Not Rotating...")
            end
        end)

        revertScreenOnStopNotifier = emu.add_machine_stop_notifier(function()
            local screenrotation = manager.machine.system.orientation
            print(screenrotation)
            if screenrotation == "rot270" or screenrotation == "rot90" then
                print("Vertical game. Rotating back...")
                os.execute("powershell.exe -file " .. landscapeScript)
            else
                print("Horizontal game. No need to rotate back.")
            end
        end)
    else
        print("Start function already called. Skipping...")
    end
end

-- Call the plugin initialization function
exports.startplugin()

return exports

Upvotes: 0

Related Questions