Jump to content

[Help] dx calculation


Snow-Man

Recommended Posts

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 by Snow-Man
Link to comment

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

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

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)

EvBd2tC.png

This is pretty standard for these types of lists (ignore the styling)

  • Thanks 1
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...