Hello, I'm very new to pico-8 (and fairly inexperienced with coding in general) and I need a bit of help with some menu code. I'd like to have a menu that has two sections to it that you can move between with the left and right arrows. I'm working off of pixelcod's menu code example, here is what I have so far:
--menu system
function init_menu()
menu={"menu1","menu2"}
menu.y=12
menu1 = {}
menu1.x=25
menu1.options={"πΈπ’ππ¬","π΅π’ππ¬","π΅π’π¬π¦","π¨πͺπ·π¦","πͺπ―π·."}
menu2 = {}
menu2.x=88
menu2.options={"πΈπ©π’π±","π¬πͺπ΄π΄","π₯πͺπ΄π΄","π§π³π¦π΅","π΄π΅π―πΊ"}
menu.amt=0
menu.sel=1
menu.swp=false
for i in all(menu1.options,menu2.options) do
menu.amt+=1
end
end
function update_menu()
update_cursor()
end
function draw_menu()
for i=1,menu.amt do
oset=i*8
end
if menu.swp==true then
if i==menu.sel then
spr(39,menu2.x,menu.y+oset,2,1)
print(menu2.options[i],menu2.x,menu.y+oset+1,10)
else
spr(55,menu2.x,menu.y+oset,2,1)
print(menu2.options[i],menu2.x,menu.y+oset+1,9)
end
else
if i==menu.sel then
spr(39,menu1.x,menu.y+oset,2,1)
print(menu1.options[i],menu1.x,menu.y+oset+1,10)
else
spr(55,menu1.x,menu.y+oset,2,1)
print(menu1.options[i],menu1.x,menu.y+oset+1,9)
end
end
end
function update_cursor()
if (btnp(β¬οΈ)) menu.sel-=1 sfx(1)
if (btnp(β¬οΈ)) menu.sel+=1 sfx(1)
if (btnp(β¬
οΈ)) menu.swp=false sfx(1)
if (btnp(β‘οΈ)) menu.swp=true sfx(1)
if (btnp(β)) sfx(1)
if (menu.sel>menu.amt) menu.sel=1
if (menu.sel<=0) menu.sel=menu.amt
end
|
With this, it has the two menus that you can swap between left and right but the menu options don't show up/come up as [NIL]. At this point, I'm sure its something simple that I'm just not understanding! I'm also willing to more or less start over if it means I get the result I need (if you've ever played it, I am making a Princess Tomato in Salad Kingdom homage and want it to look like this:
). Any help I can get will be greatly appreciated!
In init_menu():
- "menu1" and "menu2" in
menu={"menu1","menu2"}are strings and don't refrence the menu1 and 2 tables. I would move the menu creation below menu1 and 2 and reference them directly likemenu={menu1,menu2}, then to get a specific menu like menu1, you can domenu[1]. for i in all(menu1.options,menu2.options) do-allonly takes one argument. You can get a quick reference and syntax for keywords by moving the cursor over one and hitting CTRL+U.
Also, you can use # to count elements in a table like this:menu.amt = #menu1.options + #menu2.options. menu1.options refers to the options table in menu1, and # asks to count the number of elements in the table.
In draw_menu():
for i=1,menu.amt do oset=i*8 end- oset will be set to the last value of i when the loop ends.- You probably want to use a FOR loop for each side menu to draw each option. Something like
for i=1,#menu1.options do ... end
Hope that helps!
This is super helpful and definitely has gotten me closer to what I'm aiming for but I have a few questions!
"for i in all(menu1.options,menu2.options) do - all only takes one argument." I actually knew this and forgot to mention that I just did that to remember where I needed to have those haha! I'm not sure how to change it so that it displays both menu.options lists. Should I use something other than a for statement?
Similarly, when I change my if then statements to for, what should I put where I have the elses? i.e. when i doesn't equal 1/isn't displaying the highlighted option?
I hope I'm explaining myself well enough, I genuinely am a know-nothing when it comes to lua/pico-8 but I'm really really enjoying learning!
> I'm not sure how to change it so that it displays both menu.options lists.
> Should I use something other than a for statement?
Considering you have five options in the menu1.options table, it would be more efficient to use a FOR loop to print each option instead of calling the print command five times.
Same thing with the draw_menu(); instead of writing five IF ELSE's testing if each option is selected or not, you could put a single IF ELSE inside a FOR loop that goes through each one.
Here's a relatively simple (& perfectly normal) menu example:
function _init()
sel=1
menu={"start", "options", "high scores", "quit", "don't quit", "lazer eyes", "declare war against gophers"}
mx=10
my=20
sep=8
end
function _update()
if (btnp(β¬οΈ)) sel -= 1
if (btnp(β¬οΈ)) sel += 1
if (sel < 1) sel = #menu
if (sel > #menu) sel = 1
end
function _draw()
cls()
for i=1,#menu do
if i == sel then
-- highlight text
print(menu[i], mx, my+sep*i, 10)
else
-- draw text normally
print(menu[i], mx, my+sep*i, 6)
end
end
end |
In _draw(), i equals 1 at the start of the FOR loop, and because sel=1 at the start it enters the first IF block and prints the first menu element in yellow. It continues for the rest of the menu items by printing them in light grey text.
FOR loops (and loops in general) are a really useful tool for any programmer's toolbelt, and if you're not feeling confident using them I recommend looking up guides like nerdyteachers - LOOP and trying the examples.
[Please log in to post a comment]




