Jump to content

[Solved] Tables and Render


iAxel

Recommended Posts

Hello everybody!

Help solve problem...

There is empty tables

  
local sw, sh = guiGetScreenSize() 
local move = false 
--tables 
local rpos = {} -- rectangle 
local ipos = {} -- image 
  

Render the dxElements

  
addEventHandler('onClientRender', root, function () 
    local rx = 0 
    for i = 1, 4 do -- first loop 
        rpos[i] = {x = 100 + rx, y = 100} -- insert dxElement coordinates in table 
        dxDrawRectangle(rpos[i].x, rpos[i].y, 64, 64, tocolor(100, 100, 100, 225)) 
        rx = rx + 64 
    end 
    for i = 1, 2 do -- second loop 
        ipos[i] = rpos[i] -- copy the dxElement coordinates 
        dxDrawImage(ipos[i].x, ipos[i].y, 64, 64, 'image.jpg') 
    end 
    if (move) then -- for moving dxElement 
        local cx, cy = getCursorPosition() 
        dxDrawRectangle(sw * cx, sh * cy, 64, 64, tocolor(100, 100, 100, 225)) 
        dxDrawImage(sw * cx, sh * cy, 64, 64, 'image.jpg') 
    end 
end) 
  
addEventHandler('onClientClick', root, function (button, state, x, y) -- for moving dxElement 
    if (button == 'left' and state == 'up') then 
        for i = 1, #ipos do 
            -- Using the table "ipos" since are stored in it the coordinates 1 and 2 dxElement 
            if (x >= ipos[i].x and x <= ipos[i].x + 64) and (y > ipos[i].y and y < ipos[i].y + 64) then 
                if (not move) then 
                    move = true 
                end 
                outputChatBox('Click dxElement') 
            else 
                if (move) then 
                    move = false 
                end 
            end 
        end 
    end 
end) 
  

Problem is that if use of the loop

  
for i = 1, #ipos do 
    -- todo 
end 
  

We can move only the first dxElement, but if use another loop

  
for i, coord in pairs(ipos) do 
    -- todo 
end 
  

We can move only the second dxElement, but the message of the clicked displayed in both cases.

Please help me...

Edited by Guest
Link to comment
  • Moderators

When you work with offset instead of absolute positions, you won't have that problem.

Also I do not know how this code should be looking. Maybe it is better to make a screenshot of it and a photoshopped version of how it should be working.

Link to comment

How do you know which one you are actually moving?

What i think is, you are actually moving them both, only you see second as its the last one rendered (over the first one at same pos)

EDIT: note that im still newbie at lua and mta, so i may be completely wrong (not taht i dont know how to read the code)

Link to comment

I can't understand what do you mean or what do you want to do

but you can take this code as example

local sX, sY = guiGetScreenSize() 
  
local rectangles = {} 
table.insert(rectangles,{x = 100,y = 100,move = false}) 
table.insert(rectangles,{x = 200,y = 100,move = false}) 
addEventHandler('onClientRender', root, function() 
    local rx = 0 
    for i = 1,#rectangles do 
        if rectangles[i].move then 
            local x,y = getCursorPosition() 
            dxDrawRectangle(sX*x -32,sY*y -32,64,64,tocolor(100, 100, 100, 225)) 
        else 
            dxDrawRectangle(rectangles[i].x,rectangles[i].y,64,64,tocolor(100, 100, 100, 225)) 
        end 
    end 
end) 
  
  
  
addEventHandler('onClientClick', root, function (button, state, x, y) 
    if (button == 'left' and state == 'up') then 
        for i = 1, #rectangles do 
            if (x >= rectangles[i].x and x <= rectangles[i].x + 64) and (y > rectangles[i].y and y < rectangles[i].y + 64) then 
                rectangles[i].move = true 
            else 
                if rectangles[i].move == true then 
                    rectangles[i].x = x-32 
                    rectangles[i].y = y-32 
                    rectangles[i].move = false 
                end 
            end 
        end 
    end 
end) 

Link to comment
I can't understand what do you mean or what do you want to do

but you can take this code as example

local sX, sY = guiGetScreenSize() 
  
local rectangles = {} 
table.insert(rectangles,{x = 100,y = 100,move = false}) 
table.insert(rectangles,{x = 200,y = 100,move = false}) 
addEventHandler('onClientRender', root, function() 
    local rx = 0 
    for i = 1,#rectangles do 
        if rectangles[i].move then 
            local x,y = getCursorPosition() 
            dxDrawRectangle(sX*x -32,sY*y -32,64,64,tocolor(100, 100, 100, 225)) 
        else 
            dxDrawRectangle(rectangles[i].x,rectangles[i].y,64,64,tocolor(100, 100, 100, 225)) 
        end 
    end 
end) 
  
  
  
addEventHandler('onClientClick', root, function (button, state, x, y) 
    if (button == 'left' and state == 'up') then 
        for i = 1, #rectangles do 
            if (x >= rectangles[i].x and x <= rectangles[i].x + 64) and (y > rectangles[i].y and y < rectangles[i].y + 64) then 
                rectangles[i].move = true 
            else 
                if rectangles[i].move == true then 
                    rectangles[i].x = x-32 
                    rectangles[i].y = y-32 
                    rectangles[i].move = false 
                end 
            end 
        end 
    end 
end) 

Thank you! Solved

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...