Snow-Man Posted September 24, 2022 Share Posted September 24, 2022 (edited) Hello everybody, i have started on new script but faced some troubles on showing data through index of the tables, didnt know the right calculation for that and trying to make it only shows 8 data at once here part of my code where i found ther problem on it if i ~= index then dxDrawBorderedText(1," "..skinTable[index].name.." "..skinTable[index].price.." $", (985/x)*sx, ((356*y)/sy)*((index*52)/#skinTable), (1258/x)*sx, (646*y)/sy, tocolor(255, 255, 255, 255), 1.00, "bankgothic", "left", "top", false, false, false, false, false) else iprint(((356*y)/sy)*((index*30)/#skinTable)) dxDrawBorderedText(1," "..skinTable[index].name.." "..skinTable[index].price.." $", (985/x)*sx, ((356*y)/sy)*((index*52)/#skinTable), (1258/x)*sx, (646*y)/sy, tocolor(255, 255, 0, 255), 1.2, "bankgothic", "left", "top", false, false, false, false, false) end explaining some variables i = 1 index starts from 1 and follow i but that works fine and #skinTable, the result for that is 385 its a long table this is the result of my code https://imgur.com/Ly2OqZW and what i expected to have https://imgur.com/yFAw6or Edited September 24, 2022 by Snow-Man Link to comment
Tails Posted September 27, 2022 Share Posted September 27, 2022 Hi Snow-Man, Just do something like this: local startIndex = 1 local maxShownItems = 3 addEventHandler('onClientRender', root, function() for i=startIndex, startIndex+maxShownItems do local y = (i-startIndex)*20 dxDrawText(skinTable[i].name, 0, y, 100, 100) end end) And then add or subtract 1 to the startIndex on a key press addEventHandler('onClientKey', root, function(key, p) if key == 'arrow_u' and p then if startIndex > 1 then startIndex = startIndex - 1 end elseif key == 'arrow_d' and p then if startIndex+maxShownItems < #skinTable then startIndex = startIndex + 1 end end end) Hope this helps. Link to comment
Snow-Man Posted September 27, 2022 Author Share Posted September 27, 2022 Thank you it's works but had another problem it doesn't select last 8 data from the table It selects only lower than #skinTable-maxShownItem In my case local maxShownItem = 8 Link to comment
Tails Posted September 29, 2022 Share Posted September 29, 2022 Maybe use + in your calculation instead. Or paste your loop here so we can look at it. Link to comment
Snow-Man Posted September 29, 2022 Author Share Posted September 29, 2022 I have changed from the code that you already posted to local maxShownItem = 8 And table contains almost 385 data Link to comment
Snow-Man Posted September 30, 2022 Author Share Posted September 30, 2022 this is the part of the loop and now i have an error when i reach the end of the table this error attempt to index field "?" (a nil value ) exactly the error comes from "i" this one -> for i=startIndex, startIndex+maxShownItems do local startIndex = 1 local maxShownItems = 8 addEventHandler("onClientRender", root, function() for i=startIndex, startIndex+maxShownItems do local xy = (i-startIndex)*29 if i ~= startIndex then dxDrawBorderedText(1,(" "..skinTable[i].name.." "..skinTable[i].price.."$"), (985/x)*sx, (sy*0.40)+xy, (1258/x)*sx, (646*y)/sy, tocolor(255, 255, 255, 255), scale, "bankgothic", "left", "top", false, false, false, false, false) else dxDrawBorderedText(1,(" "..skinTable[i].name.." "..skinTable[i].price.."$"), (985/x)*sx, (sy*0.40)+xy, (1258/x)*sx, (646*y)/sy, tocolor(255, 255, 0, 255), scale, "bankgothic", "left", "top", false, false, false, false, false) end end end ) i have changed this part and it works better and having the result what i wanted for addEventHandler( "onClientKey", root, function(b, state) if b and validKeys[b] then if b == "arrow_u" and state then if startIndex-1 > 0 then startIndex = startIndex - 1 else startIndex = #skinTable end playSoundFrontEnd(1) elseif b == "arrow_d" and state then if startIndex+1 <= #skinTable then startIndex = startIndex + 1 else startIndex = 1 end playSoundFrontEnd(1) end end end ) Link to comment
Tails Posted September 30, 2022 Share Posted September 30, 2022 I think what you're looking for is a continuous list addEventHandler( "onClientKey", root, function(b, state) if b == "arrow_u" and state then table.insert(skinTable, 1, table.remove(skinTable)) playSoundFrontEnd(1) elseif b == "arrow_d" and state then table.insert(skinTable, table.remove(skinTable, 1)) playSoundFrontEnd(1) end end) local x,y = 0, 0 local sx, sy = guiGetScreenSize() local maxShownItems = 7 local selectedItem = 4 --use middle item as the current selected item addEventHandler("onClientRender", root, function() for i=1, maxShownItems do local xy = (i)*55 -- draw rectangle behind text dxDrawRectangle(x, y+xy, 200, 50, tocolor(0, 0, 0, 125)) if i == 4 then -- selected item dxDrawRectangle(x, y+xy, 200, 50, tocolor(255, 0, 0, 125)) dxDrawText(" "..skinTable[i].name.." "..skinTable[i].price.."$", x, y+xy, x+200, y+xy+50, tocolor(255, 255, 255, 255), 1.5, 'default', 'center', 'center', false, false, true, true) else -- the other items dxDrawText(" "..skinTable[i].name.." "..skinTable[i].price.."$", x, y+xy, x+200, y+xy+50, tocolor(255, 255, 255, 255), 1, 'default', 'center', 'center', false, false, true, true) end end end) This is pretty standard for these types of lists (ignore the styling) 1 Link to comment
Snow-Man Posted October 1, 2022 Author Share Posted October 1, 2022 thank you so much for helping, i have changed a lot on the code and mixed between what you write here and my code and works well even selectedItem = 1 now it works on continuous list 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