Rockbox Technical Forums
Rockbox Development => Starting Development and Compiling => Topic started by: HolyRoller on June 30, 2019, 07:29:28 PM
-
I'm trying to make a simple LUA script that rolls 4 dice simultaneously (one 6-sided, two 10-sided, and one 100-sided). I can get the following script to work in a LUA compiler, but not on Rockbox for the Sansa Clip Zip:
print(math.random(1,6), math.random(1,10), math.random(1,10), math.random(1,100))
Could someone help me get this corrected? I'd appreciate it!
-
Never used lua, but there is no console on rockbox, so I'm not sure you can print. Take a look at the helloworld program which implements its own printf function:
https://git.rockbox.org/?p=rockbox.git;a=blob;f=apps/plugins/helloworld.lua;h=aa2ef446d3d422c27be7358383764c7cf92d638a;hb=HEAD#l47
-
https://www.rockbox.org/wiki/PluginDice
-
Our lua interpreter has quite a few differences from standard lua
your script should work as follows:
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory
math.randomseed(os.time()) -- seeds the random number generator
print.clear()
print.f("%d %d %d %d", math.random(1,6), math.random(1,10), math.random(1,10), math.random(1,100))
rb.sleep(rb.HZ * 5) --waits 5 seconds
I have a few examples floating around on the forum
this one will be very important: it prints the API in a easily browsed list
http://forums.rockbox.org/index.php/topic,51935.msg240367.html
here is a couple advanced examples:
http://forums.rockbox.org/index.php/topic,52352.msg242367.html
and one I forgot:
http://forums.rockbox.org/index.php/topic,52895.0.html
-
Thanks for your help guys! I attempted to run the code there but it crashed with an error. I then noticed that I don't have a print.lua in that directory. Can I download that somewhere?
-
Aha, I just needed the latest dev build. Thanks!
-
If I could trouble you guys for one more. How could I modify my code to let me hit the back or ok button to exit instead of waiting for the sleep?
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory
local r = math.random
local items = {
"sword",
"shield",
"potion"
}
math.randomseed(os.time()) -- seeds the random number generator
print.clear()
print.f("%s", items[r(1, #items)])
rb.sleep(rb.HZ * 5) --waits 5 seconds
-
require "buttons"
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory
local line = 1, max_line, w, h
local button = 0
local btn = rb.buttons
local r = math.random
local items = {
"sword",
"shield",
"potion"
}
math.randomseed(os.time()) -- seeds the random number generator
print.clear()
repeat
if button >= 0 and bit.band(button, btn.BUTTON_REL) == 0 then -- make sure to skip sys_events ( < 0 ) and button release
if line == max_line then
print.clear()
line = 1
end
line, max_line, w, h = print.f("%d. %s", line, items[r(1, #items)])
end
button = rb.button_get(true) -- true = block till pressed
until(button == btn.BUTTON_BACK)
-
Alright I ran this code and every button seems to re-roll the result but none exit the program. Had to do a hard reset. Do I modify the local button line?
Also, is there a word wrap feature or code? With long results, it will display all of it on one line and autoscroll. I'd like it to wrap if possible.
Thanks for your help, I'm learning a lot!
-
your DAP probably doesn't have button_back mapped so use actions instead
-
here you go this uses actions and has a word wrap function I just threw it together so tweak it till you get what you want
require "actions"
local print = require "print" -- print.lua is in the /rockbox/rocks/viewers/lua directory
local line = 1, max_line, w, h
local act = rb.actions
local action = act.NONE
local r = math.random
local TIMEOUT_BLOCK = -1
local TIMEOUT_NOBLOCK = 0
local left_margin = rb.font_getstringsize("W ", font or rb.FONT_UI)
local items = {
"sword",
"shield",
"potion",
"really long text item that needs to word wrap to another line",
"really really long text item that needs to word wrap to another line and then to additional lines after that"
}
math.randomseed(os.time()) -- seeds the random number generator
print.opt.overflow("none")
print.settings = print.opt.get()
print.clear()
local function word_wrap(item, max_width)
local item_disp=""
local chars = 1
repeat --remove what has alread been displayed
item_disp = string.sub(item, 1, chars)
chars = chars + 1
until(rb.font_getstringsize(item_disp, font or rb.FONT_UI) >= max_width - left_margin)
local fsp = string.find(item, "%s", chars - 3) or 0x0
--rb.splash(100, chars .. " " .. fsp)
if fsp ~= 0 and fsp < chars then chars = fsp + 1 end
item = string.sub(item, chars)
line, max_line, w, h = print.f("%s", item)
if w > max_width then
return word_wrap(item, max_width) -- function tailcall
end
end
repeat
if action == act.PLA_SELECT or action == act.NONE then
if line == max_line then
print.clear()
line = 1
end
local item = items[r(1, #items)]
line, max_line, w, h = print.f("%s", item)
if w > print.settings.width then
word_wrap(item, print.settings.width)
end
end
action = rb.get_plugin_action(TIMEOUT_BLOCK) -- block till pressed
until(action == act.PLA_EXIT or action == act.PLA_CANCEL)
-
Fantastic that works! Is there a way I can tweak the cut off? I noticed that when it wraps, it will repeat the previous 3 or so characters. Sorta like:
really reall
ally long t
g text line
-
push the text through the word_wrap function every time and tweak it to do what you want...
teach a man to fish and whatnot