FlyingSpoon Posted August 5, 2017 Share Posted August 5, 2017 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
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 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. 2 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 I get the cursor position part but how would I detect which text the mouse is on. Link to comment
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 (edited) Using a forcycle, where you cycle through all of the text with it's x, y position and it's width, height. Edited August 5, 2017 by NeXuS™ 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 (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 August 5, 2017 by raysmta Link to comment
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 Yep, awesome. Do you need further help? 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 (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 August 5, 2017 by raysmta Link to comment
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 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. 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 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) Link to comment
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 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. 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 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? Link to comment
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 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. 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 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. Link to comment
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 "root" and getRootElement are the same. Quote root -- returns the root element of the server It's a predefined variable in MTA. What do you mean it starts this one too? 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 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
NeXuS™ Posted August 5, 2017 Share Posted August 5, 2017 So you have onResourceStart events. Use resourceRoot instead of root. 1 Link to comment
FlyingSpoon Posted August 5, 2017 Author Share Posted August 5, 2017 I mean root works perfectly fine, just getRootElement is the problem, and thanks I'll use resourceRoot too. 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