Rockbox Technical Forums

Support and General Use => Plugins/Viewers => Topic started by: HolyRoller on May 20, 2020, 11:47:56 AM

Title: Trying to make a hit point counter
Post by: HolyRoller on May 20, 2020, 11:47:56 AM
I'm trying to make a hit-point counter for RPG and card games.  I get a bit stuck when trying to incorporate the Clip Zip buttons. All I want is to have a stat on each line that I would define, so on my clip it would look like:

Health: 20
Mana: 15

and any others I would add. Up-down would highlight the different stats and left-right would increase and decrease the number, allowing negatives as well.  Would this be possible?  Thanks in advance!
Title: Re: Trying to make a hit point counter
Post by: Bilgus on May 20, 2020, 01:03:27 PM
This is a perfect use case for lua
see the example scripts provided on device and the wiki

https://www.rockbox.org/wiki/PluginLua
Title: Re: Trying to make a hit point counter
Post by: HolyRoller on May 20, 2020, 08:07:26 PM
Thanks Bilgus!  I thought I had learned a bit about Rockbox LUA but it seems I'm a total newb lol.  I thought it would be simple but I don't even know where to start in those example scripts. Can you recommend a good place to start learning how to program for Rockbox starting at beginner level?
Title: Re: Trying to make a hit point counter
Post by: Bilgus on May 22, 2020, 10:39:30 AM
Copy and paste any of those examples to a file name it test.lua copy to your device and run it from the file browser

There are also some more advanced examples that are supplied in the dev versions of rockbox
you can run them from Plugins>Demos>lua_scripts

I think the menu example will be the easiest for you to alter to get what you want

Code: [Select]
--It's a good example to learn how to work with the menu API, fundamental part of almost any Rockbox plugin.
-- Example script to demonstrate the development of a simple Rockbox plugin in LUA.
-- Written by Gabriel Maia (gbl08ma on the Rockbox community).
-- Licensed under GNU GPL v2 or, at your choice, any later version.

-- Function to display the main menu of this script
function ShowMainMenu() -- we invoke this function every time we want to display the main menu of the script
    mainmenu = {"Item 1", "Item 2", "Item 3", "Exit"} -- define the items of the menu

    while true do -- don't exit of program until user selects Exit
        s = rb.do_menu("Test", mainmenu, nil, false) -- actually tell Rockbox to draw the menu
       
        -- In the line above: "Test" is the title of the menu, mainmenu is an array with the items
        -- of the menu, nil is a null value that needs to be there, and the last parameter is
        -- whether the theme should be drawn on the menu or not.
        -- the variable s will hold the index of the selected item on the menu.
        -- the index is zero based. This means that the first item is 0, the second one is 1, etc.
        if     s == 0 then ShowSubmenu1()
        elseif s == 1 then rb.splash(2 * rb.HZ, "You selected Item 2") --show a message for two seconds
        elseif s == 2 then rb.splash(2 * rb.HZ, "You selected Item 3")
        elseif s == 3 then os.exit() -- User selected to exit
        elseif s == -2 then os.exit() -- -2 index is returned from do_menu() when user presses the key to exit the menu (on iPods, it's the left key).
                                      -- In this case, user probably wants to exit (or go back to last menu).
        else rb.splash(2 * rb.HZ, "Error! Selected index: " .. s) -- something strange happened. The program shows this message when
                                                                  -- the selected item is not on the index from 0 to 3 (in this case), and displays
                                                                  -- the selected index. Having this type of error handling is not
                                                                  -- required, but it might be nice to have specially while you're still
                                                                  -- developing the plugin.
        end
    end
end

-- Submenu 1
function ShowSubmenu1() -- function to draw the submenu 1, which will be displayed when
                        -- user selects Item 1 on main menu
    submenu1 = {"Sub Item 1", "Sub Item 2", "Return"} -- define the items of the menu

    while true do -- don't exit of program until user selects Exit
        s1 = rb.do_menu("Item 1 menu", submenu1, nil, false)

        if     s1 == 0 then rb.splash(2 * rb.HZ, "You selected Sub Item 1") --show a message for two seconds
        elseif s1 == 1 then rb.splash(2 * rb.HZ, "You selected Sub Item 2")
        elseif s1 == 2 then ShowMainMenu() -- return to the main menu, display it again
        elseif s1 == -2 then ShowMainMenu() -- user seems to want to go back
        else rb.splash(2 * rb.HZ, "Error! Selected index: " .. s)
        end
    end
end

-- code written out of any function will be executed when the file is "played" on the file browser.
-- if you don't put anything out of a function (not even a call to the main function, like we do here),
-- the script will simply do nothing.
ShowMainMenu() -- start the program by displaying the main menu of the script.