Xwad Posted February 22, 2019 Share Posted February 22, 2019 function test() for i, v in ipairs(getElementsByType("vehicle")) do showCursor(true) local vx,vy,vz = getElementPosition(v) local sx,sy = getScreenFromWorldPosition ( vx,vy,vz ) local screenx, screeny, worldx, worldy, worldz = getCursorPosition() if sx > screenx*w and sx < px*w or sx < screenx*w and sx > px*w then -- error with this line outputChatBox("test") end end end bindKey ("mouse2", "down", test) ERROR: attempt to compare number with bolean. Any idea? Link to comment
Moderators Patrick Posted February 22, 2019 Moderators Share Posted February 22, 2019 (edited) This script is horrible and resource demanding. What you want to do? Do something when the player is right clicked on a car? If so, try this: function onVehicleRightClick(vehicleElement) outputChatBox("You are right clicked on " .. getVehicleName(vehicleElement)) end addEventHandler("onClientClick", root, function(button, state, _, _, _, _, _, clickedElement) if button == "right" and state == "up" and isElement(clickedElement) and getElementType(clickedElement) == "vehicle" then onVehicleRightClick(clickedElement) end end) But the answer to your question is, you are trying to multiply a boolean with a number. The boolean are the sx and sy, because getScreenFromWorldPosition returns false. (getScreenFromWorldPosition returns false when the element dont showing on your screen.) Edited February 22, 2019 by stPatrick Link to comment
Xwad Posted February 23, 2019 Author Share Posted February 23, 2019 No, what i am trying to do is a rectangle select like in strategy games when you select units. I get the screen position of the element and if its inside the rectangles area then do somethink function selecting() if getKeyState("mouse1") == true then local screenx, screeny, worldx, worldy, worldz = getCursorPosition() local x = screenx*w - px*w local y = screeny*h - py*h dxDrawLinedRectangle( px*w,py*h, x,y, tocolor ( 255,255,255, 255 ), 2, true ) for i, v in ipairs(getElementsByType("vehicle")) do local vx,vy,vz = getElementPosition(v) local sx,sy = getScreenFromWorldPosition ( vx,vy,vz ) local screenx, screeny, worldx, worldy, worldz = getCursorPosition() if sx > screenx*w and sx < px*w or sx < screenx*w and sx > px*w then if sy > screeny*h and sy < py*h or sy < screeny*h and sy > py*h then outputChatBox("ELEMENT SELECTED (ELEMENT IS INSIDE THE RECTANGLE") end end end else setCursorAlpha(255) removeEventHandler("onClientRender", root, selecting) end end function select_event() px, py, worldx, worldy, worldz = getCursorPosition() setCursorAlpha(0) addEventHandler("onClientRender", root, selecting) end bindKey ("mouse1", "down", select_event) Link to comment
Moderators Patrick Posted February 24, 2019 Moderators Share Posted February 24, 2019 -- CLIENT SIDE local w, h = 25, 25 local screenWidth, screenHeight = guiGetScreenSize() function selecting() if isCursorShowing() then local cursorX, cursorY = getCursorPosition() -- Its returns relative positions local cursorX, cursorY = cursorX*screenWidth, cursorY*screenHeight -- so you need to multiply with your screen sizes (So this is now the absolute positions, the pixels.) dxDrawLinedRectangle(cursorX - w/2, cursorY - h/2, w, h, tocolor(255,255,255,255), 2) for i, v in ipairs(getElementsByType("vehicle")) do local vx, vy, vz = getElementPosition(v) local sx, sy = getScreenFromWorldPosition(vx, vy, vz) if (cursorX >= (sx - w/2) and cursorX <= (sx + w/2) and cursorY >= (sy - h/2) and cursorY <= (sy + h/2)) then dxDrawLinedRectangle(sx - w/2, sy - h/2, w, h, tocolor(255,50,50,255), 2) -- Cursor in rectrangle else dxDrawLinedRectangle(sx - w/2, sy - h/2, w, h, tocolor(255,255,255,255), 2) end end end end function dxDrawLinedRectangle( x, y, width, height, color, _width, postGUI ) _width = _width or 1 dxDrawLine ( x, y, x+width, y, color, _width, postGUI ) -- Top dxDrawLine ( x, y, x, y+height, color, _width, postGUI ) -- Left dxDrawLine ( x, y+height, x+width, y+height, color, _width, postGUI ) -- Bottom return dxDrawLine ( x+width, y, x+width, y+height, color, _width, postGUI ) -- Right end function select_event() addEventHandler("onClientRender", root, selecting) end bindKey("mouse1", "down", select_event) function select_event_disable() removeEventHandler("onClientRender", root, selecting) end bindKey("mouse1", "up", select_event_disable) Something like that? Link to comment
Xwad Posted February 24, 2019 Author Share Posted February 24, 2019 (edited) No, the rectangles size is not fixed. It can be changed by moving the cursor, just like in strategy games or when you hold mouse 1 on windows desktop and move your mouse. But my problem is not with the rectangle. That is working fine. My problem is with the detecting of vehicles inside the rectangle. But anyway, i tested your script and its also not detecting the elements inside the rectangle best example: this is what im trying to do Edited February 24, 2019 by Xwad Link to comment
Moderators Patrick Posted February 24, 2019 Moderators Share Posted February 24, 2019 53 minutes ago, Xwad said: No, the rectangles size is not fixed. It can be changed by moving the cursor, just like in strategy games or when you hold mouse 1 on windows desktop and move your mouse. But my problem is not with the rectangle. That is working fine. My problem is with the detecting of vehicles inside the rectangle. But anyway, i tested your script and its also not detecting the elements inside the rectangle best example: this is what im trying to do I see. local sx, sy = guiGetScreenSize() local selectStartCursorX, selectStartCursorY = false, false function getElementsWithinScreenZone(elementType, x, y, w, h) local streamedElements = getElementsByType(elementType, root, true) local inZoneElements = {} for i, element in ipairs(streamedElements) do local elementX, elementY, elementZ = getElementPosition(element) local screenX, screenY = getScreenFromWorldPosition(elementX, elementY, elementZ) if (screenX >= x and screenX <= x+w and screenY >= y and screenY <= y+h) then table.insert(inZoneElements, element) end end return inZoneElements end function render() if isCursorShowing() then local cx, cy = getCursorPosition() local cx, cy = cx * sx, cy * sy local selectZoneX, selectZoneY, selectZoneW, selectZoneH if (cx < selectStartCursorX) then selectZoneX, selectZoneW = cx, selectStartCursorX - cx else selectZoneX, selectZoneW = selectStartCursorX, cx - selectStartCursorX end if (cy < selectStartCursorY) then selectZoneY, selectZoneH = cy, selectStartCursorY - cy else selectZoneY, selectZoneH = selectStartCursorY, cy - selectStartCursorY end dxDrawRectangle(selectZoneX, selectZoneY, selectZoneW, selectZoneH, tocolor(20,20,20,80)) local selectedVehicles = getElementsWithinScreenZone("vehicle", selectZoneX, selectZoneY, selectZoneW, selectZoneH) for i, vehicle in ipairs(selectedVehicles) do local elementX, elementY, elementZ = getElementPosition(vehicle) local screenX, screenY = getScreenFromWorldPosition(elementX, elementY, elementZ) dxDrawCircle(screenX, screenY, 6) end end end function startSelect() local cx, cy = getCursorPosition() selectStartCursorX, selectStartCursorY = cx * sx, cy * sy addEventHandler("onClientRender", root, render) end function stopSelect() removeEventHandler("onClientRender", root, render) end addEventHandler("onClientKey", root, function(key, state) if key == "mouse1" then if state and isCursorShowing() then startSelect() else stopSelect() end end end) Link to comment
Xwad Posted February 24, 2019 Author Share Posted February 24, 2019 (edited) Works, thanks a lot! Edited February 24, 2019 by Xwad Link to comment
Moderators Patrick Posted February 24, 2019 Moderators Share Posted February 24, 2019 2 hours ago, Xwad said: Works, thanks a lot! You are welcome! 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