Jump to content

Clicking on stuff in a table


Recommended Posts

I was thinking about something lately, say if I was to create a table -

local t = {
  "First",
  "Second",
  "Third",
  "Fourth"
 } 

function drawText()
  for i, v in ipairs(t) do
    dxDrawText(v, x, y+150, w, h)
  end
end
addEventHandler("onClientRender", root, drawText)

That's a basic code, say if I was to click on the first Text which says "First", how would I make it so that it outputs this text, and gets the right one from the table instead of doing it all manually.

Link to comment

I'll give you some functions, if you don't know how to do it, just reply.

First, you'll have to use getCursorPosition.
If you have the cursor's position, you have to use some math, to calculate if that position is between the text's pos and the text's pos + width.
If it's, you can just output the current text in your forcycle.

  • Thanks 2
Link to comment

 

local t = {
  {"First", x, y, w, h},
  {"Second", x, y, w, h},
  {"Third", x, y, w, h},
  {"Fourth", x, y, w, h}
  }

addEventHandler("onClientRender", root, 
  function()
    for i, v in pairs(t) do
      dxDrawText(v[1], v[2]+20, v[3], v[4], v[5])
    end
end)

function detectClicks()
    for k, pos in pairs(t) do
       if isMouseInPosition(pos[2], pos[3], pos[4], pos[5])
        --
       end
    end
end
addEventHandler("onClientClick", root, detectClicks)

function isMouseInPosition ( x, y, width, height )
	if ( not isCursorShowing( ) ) then
		return false
	end
    local sx, sy = guiGetScreenSize ( )
    local cx, cy = getCursorPosition ( )
    local cx, cy = ( cx * sx ), ( cy * sy )
    if ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) then
        return true
    else
        return false
    end
end

 

Edited by raysmta
Link to comment

I mean, I dont think you want to return anything.

function detectClicks()
	for i, pos in pairs(t) do
		if isMouseInPosition(pos[2], pos[3], pos[4], pos[5]) then
			outputChatBox(pos[1])
			return
		end
	end
end
addEventHandler("onClientClick", root, detectClicks)

But yours should work too.

  • Thanks 1
Link to comment

Say if I write in one script e.g.

One folder is called - 

script1

And the other one is called -

script2

--

In c.lua in script1 folder I wrote at the end of the function - getRootElement().

So now say if I restart script2 it will also restart script1, and when I change it to root instead of getRootElement(), it works fine, any ideas?

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