Jump to content

Clicking on stuff in a table


Recommended Posts

Posted

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.

Posted

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
Posted (edited)

Using a forcycle, where you cycle through all of the text with it's x, y position and it's width, height.

Edited by NeXuS™
  • Thanks 1
Posted (edited)

 

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
Posted (edited)

Yes, and thank you so much, I was kind of confused on something, you see the detect clicks part, how would I know which text is being pressed? Would I do something like

outputChatBox(pos[1])

I don't get that part, how do I only get a certain text that is clicked.

Edited by raysmta
Posted

I suppose you have different y position for each text. Try to imagine what your script does. If you don't understand this, just reply. :D

image.md.png

  • Thanks 1
Posted

So what I did is right?

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

 

Posted

Use return after the outputChatBox, the reason why, is because if you think about it. You found the one text you clicked on, and there won't be anymore, so if you stop the cycle there, you'll save some performance and a micro amount of time.

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

Like that?

Posted

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
Posted

And last thing, which one is the right one to use in addEventHandler, after the handler, so is it root or getRootElement() ? Because when I use getRootElement() sometimes, and when I start another script, it starts this one too.

Posted

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?

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