Dzsozi (h03) Posted January 27, 2022 Share Posted January 27, 2022 (edited) Hello! I made a script which can render menus with infinite sub menus in it. I am using a table for that. Everything seems to be working fine, but when I was trying to create the Back button for the menus, including sub menus, I crash into a small problem. I don't really know how should I make this step back function for all of the sub menus, since I can step back to only the previous one, not the one before that. Here is an example table for the menus: MENU = { [1] = { ["category"] = "options1", ["sub"] = { [1] = { ["category"] = "options1-1", ["sub"] = { [1] = { ["category"] = "options1-1-1", ["sub"] = { [1] = {option = "change1"}, }, }, }, }, [2] = { ["category"] = "options1-2", ["sub"] = { [1] = {option = "change2"}, }, }, }, }, [2] = { ["category"] = "options2", ["sub"] = { [1] = { ["category"] = "options2-1", ["sub"] = { [1] = {option = "change1"}, }, }, [2] = { ["category"] = "options2-2", ["sub"] = { [1] = { ["category"] = "options2-2-1", ["sub"] = { [1] = {option = "change2"}, }, }, [2] = { ["category"] = "options2-2-2", ["sub"] = { [1] = {option = "change2"}, }, }, [3] = { ["category"] = "options2-2-3", ["sub"] = { [1] = {option = "change2"}, }, }, }, }, }, }, } And here's how I handle the rendering and clicking: local sx, sy = guiGetScreenSize() local RENDER = {} RENDER.state = true RENDER.hover = {} RENDER.hover.menuID = 0 RENDER.hover.itemID = 0 RENDER.hover.back = nil local currentMenu = MENU local currentMenuID = 0 local previousMenu = currentMenu local previousMenuID = currentMenuID function render() if RENDER.state then RENDER.hover.menuID = 0 RENDER.hover.itemID = 0 RENDER.hover.back = nil local menuX, menuY = sx/2-200, sy/2 for menuID, menuData in ipairs(currentMenu) do menuY = sy/2+(30*menuID) if isMouseInPosition(menuX, menuY, 100, 25) then RENDER.hover.menuID = menuID RENDER.hover.itemID = menuData.option and menuID or 0 end local hovering = RENDER.hover.menuID == menuID dxDrawRectangle(menuX, menuY, 100, 25, hovering and tocolor(100, 100, 100, 150) or tocolor(0, 0, 0, 150)) dxDrawText(menuData["category"] or menuData.option, menuX+10, menuY+5, 0, 0, tocolor(255, 255, 255, 255)) end if currentMenuID ~= 0 then local backX, backY = menuX, sy/2 local backHover = isMouseInPosition(backX, backY, 100, 25) dxDrawRectangle(backX, backY, 100, 25, backHover and tocolor(200, 0, 0, 150) or tocolor(100, 0, 0, 150)) dxDrawText("< Back", backX+10, backY+5, 0, 0, tocolor(255, 255, 255, 255)) if backHover then RENDER.hover.back = true end end end end addEventHandler("onClientRender", root, render) function stepBackInMenu() previousMenu = currentMenu previousMenuID = currentMenuID return true end addEventHandler("onClientClick", root, function(button, state) if button == "left" then if state == "down" then if RENDER.hover.menuID ~= 0 then if RENDER.hover.itemID == 0 then previousMenuID = currentMenuID previousMenu = currentMenu currentMenuID = RENDER.hover.menuID currentMenu = currentMenu[currentMenuID]["sub"] end end if RENDER.hover.back then stepBackInMenu() end end end end) What would be the most optional way to handle every sub menu's back button to step back in to the actual previous menu? I can't seem to figure it out by myself so I would really appreciate some help from you. Thank you in advance! Edited January 27, 2022 by Dzsozi (h03) Link to comment
Dzsozi (h03) Posted January 27, 2022 Author Share Posted January 27, 2022 (edited) Sorry I messed up one function above and couldn't edit the post anymore, here's the working one: function stepBackInMenu() currentMenu = previousMenu currentMenuID = previousMenuID return true end Edited January 27, 2022 by Dzsozi (h03) Link to comment
The_GTA Posted January 27, 2022 Share Posted January 27, 2022 Hello Dzsozi (h03), your script appears to be missing the path of menus that you are visiting. The path of menus is defined as a list of menu displays d1...dn where d1 is the initially displayed menu, di is the menu that was selected in menu d(i-1). Then switching back to the previous menu is defined as removing the top-most menu entry dn and setting the menu entry d(n-1) as the current menu display. With that idea in your head, I hope that you can figure out it's implementation. Feel free to ask. 1 Link to comment
Dzsozi (h03) Posted January 27, 2022 Author Share Posted January 27, 2022 So do I make a new table with a variable name let's say for example previousMenu, then set the previousMenu[menuID] to be a new table, then repeat this process each time a button gets clicked? If I understand you correctly, I am not so sure. I would be really grateful if you could provide me an example of what you are suggesting, if I am not correct. Link to comment
The_GTA Posted January 27, 2022 Share Posted January 27, 2022 local menu_path = { MENU } local function visit_menu(m) table.insert(menu_path, m) end local function get_current_menu() if (#menu_path == 0) then return false end return menu_path[#menu_path] end local function unvisit_current_menu() table.remove(menu_path) end Look at lua-users wiki: Table Library Tutorial 1 Link to comment
Dzsozi (h03) Posted January 27, 2022 Author Share Posted January 27, 2022 OOOOOOO I get it now, thank you so much for your quick response and time! I managed to do it, it is working now as expected. Have a nice day/night, thanks again @The_GTA! Link to comment
The_GTA Posted January 27, 2022 Share Posted January 27, 2022 Just now, Dzsozi (h03) said: OOOOOOO I get it now, thank you so much for your quick response and time! I managed to do it, it is working now as expected. Have a nice day/night, thanks again @The_GTA! Glad to hear that you got it working! You're welcome. Come back if you have any further questions. 1 Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now