Jump to content

Distance to the hunter.


proracer

Recommended Posts

Hello guys.I have recently made script which calculates distance to the hunter .. but the problem is that it gets buggy when next map/s start (it duplicates again).

I have already tried removing text when player dies but still nothing, please help. :P

function distToHunter ( ) 
    dxDrawText ("Distance To Hunter:",1199.0,1025.0,1405.0,1044.0,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                local hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
                local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
                dxDrawText( distFromVehicleToHunter .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) -- Draw it on the screen (gets buggy when new map starts) 
            end 
        end 
    end 
end 
  
addEventHandler ( "onClientRender", root, distToHunter ) 
  
function destroyDXText ( ) 
    removeEventHandler ( "onClientRender", root, distToHunter ) -- Force to remove it when player dies 
end 
  
addEventHandler ( "onClientPlayerWasted", getLocalPlayer ( ), destroyDXText ) 
  

Edit: Now it doesn't duplicate text but the text just wont appear on screen when next map starts.

Link to comment

I have done that already.

Here is new code:

addEvent("onClientMapStarting") 
addEvent("onClientMapStopping") 
function distToHunter ( ) 
    dxDrawText ("Distance To Hunter:",1199.0,1025.0,1405.0,1044.0,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                local hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
                local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
                dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) -- Draw it on the screen (gets buggy when new map starts) 
            end 
        end 
    end 
end 
  
addEventHandler ( "onClientRender", root, distToHunter ) 
addEventHandler ( "onClientMapStarting", getLocalPlayer ( ), distToHunter ) 
  
function destroyDXText ( ) 
    removeEventHandler ( "onClientRender", root, distToHunter ) -- Force to remove it when player dies 
end 
  
addEventHandler ( "onClientMapStopping", getLocalPlayer ( ), destroyDXText ) 
  

Link to comment

Sorry for double post, its buggy now only when it start other map but not the same as loaded the one before.

I used race element data 's but I don't know if I used correctly because I can't found anywhere how it's called and what are their values...

--[[ 
  
                ** Distance To Hunter v1.0 - Made by Pr0RaC3R ** 
  
--]] 
  
  
  
function distToHunter ( ) 
    dxDrawText ("Distance To Hunter:",1199.0,1025.0,1405.0,1044.0,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                local hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
                local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
                dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) -- Draw it on the screen and format it to output without decimals 
            end 
        end 
    end 
end 
  
addEventHandler ( 'onClientRender', root, distToHunter ) 
  
--[[ 
  
- More simpler way is to just recreate it again with calling function again 
  
--]] 
  
function recreateDXText ( ) 
    if (getElementData (getLocalPlayer( ), 'race.LoadingMap') == 'true') then 
        distToHunter ( ) 
    end 
end 
  
addEventHandler ( 'onClientElementDatachange', root, recreateDXText ) 
  
  

Link to comment
  • Moderators

Oh I found the problem I think :

In your loop, the client found a second pickup which changes the vehicle to an hunter.

So you have to destroy the pickup of the previous map ( If this pickup was not destroyed )

Or destroy all pickups which changes the vehicle to an hunter before each new maps.

Link to comment
--[[ 
  
                ** Distance To Hunter v1.0 - Made by Pr0RaC3R ** 
  
--]] 
  
  
addEvent('onClientMapStarting') 
addEvent('onClientMapStopping') 
  
function distToHunter ( ) 
    dxDrawText ("Distance To Hunter:",1199.0,1025.0,1405.0,1044.0,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                local hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
                local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
                dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) -- Draw it on the screen and format it to output without decimals 
            end 
        end 
    end 
end 
  
function handleRender ( ) 
    addEventHandler ( 'onClientRender', root, distToHunter ) 
end 
  
addEventHandler ( 'onClientMapStarting', root, handleRender ) 
  
function delRender ( ) 
    removeEventHandler ( 'onClientRender', root, distToHunter ) 
end 
  
addEventHandler ( 'onClientMapStopping', root, delRender ) 
  

Shouldn't this delete text after map finished and then create it when map starts again? But it doesn't work .. still duplicates text (look my above post)

Edit: Citizen, thx .. I will try it.

Link to comment
addEvent('onClientMapStarting') 
addEvent('onClientMapStopping') 
  
hunterX, hunterY, hunterZ = 0,0,0 
  
function loadHunterPosition () 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                break 
            end 
        end 
    end 
end 
  
function drawDistance ( ) 
    local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
    local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
    dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) 
end 
  
function handleRender ( ) 
    loadHunterPosition () 
    addEventHandler ( 'onClientRender', root, drawDistance ) 
end 
  
addEventHandler ( 'onClientMapStarting', root, handleRender ) 
  
function delRender ( ) 
    removeEventHandler ( 'onClientRender', root, drawDistance ) 
end 
  
addEventHandler ( 'onClientMapStopping', root, delRender ) 
  

Link to comment
  • Moderators
but I found the problem that if there are more than 1 hunter pickup it duplicates text

What ?! YOU found the problem ? I never said that before you ??

You should say:

Citizen you right, I tested and when there are 2 hunter pickups, the text is duplicated

Yeah H5N1 or just add your break here ( the full code ):

    --[[ 
  
                ** Distance To Hunter v1.0 - Made by Pr0RaC3R ** 
  
--]] 
  
  
  
function distToHunter ( ) 
    dxDrawText ("Distance To Hunter:",1199.0,1025.0,1405.0,1044.0,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                local hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
                local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
                dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) -- Draw it on the screen and format it to output without decimals 
                break 
            end 
        end 
    end 
end 
addEventHandler ( 'onClientRender', root, distToHunter ) 
  
--[[ 
  
- More simpler way is to just recreate it again with calling function again 
  
--]] 
  
function recreateDXText ( ) 
    if (getElementData (getLocalPlayer( ), 'race.LoadingMap') == 'true') then 
        distToHunter ( ) 
    end 
end 
addEventHandler ( 'onClientElementDatachange', root, recreateDXText ) 

@proracer: Be careful next time I really hate that :evil:

Link to comment
but I found the problem that if there are more than 1 hunter pickup it duplicates text

What ?! YOU found the problem ? I never said that before you ??

You should say:

Citizen you right, I tested and when there are 2 hunter pickups, the text is duplicated

Yeah H5N1 or just add your break here ( the full code ):

    --[[ 
  
                ** Distance To Hunter v1.0 - Made by Pr0RaC3R ** 
  
--]] 
  
  
  
function distToHunter ( ) 
    dxDrawText ("Distance To Hunter:",1199.0,1025.0,1405.0,1044.0,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                local hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
                local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
                dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",1426.0,1026.0,1554.0,1045.0,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) -- Draw it on the screen and format it to output without decimals 
                break 
            end 
        end 
    end 
end 
addEventHandler ( 'onClientRender', root, distToHunter ) 
  
--[[ 
  
- More simpler way is to just recreate it again with calling function again 
  
--]] 
  
function recreateDXText ( ) 
    if (getElementData (getLocalPlayer( ), 'race.LoadingMap') == 'true') then 
        distToHunter ( ) 
    end 
end 
addEventHandler ( 'onClientElementDatachange', root, recreateDXText ) 

@proracer: Be careful next time I really hate that :evil:

Yeah but i optimize this code. Now script doesnt get hunter position every frame.

Link to comment

Sorry if I'm so annoying but there is some small problem about DX Text and Image, player's are reporting that text and image are in mess (because of their resolution), I used frequently used method with optimizing text and image for every resolution but still nothing.

So the conclusion: Problem is that people see text and image in wrong position that it needs to be...

Here is the code:

Important lines: 30-37

--[[ 
  
                ** Distance To Hunter v1.0 - Made by Pr0RaC3R ** 
                ** Special credits to Citizen & H5N1 from MTA Forum for helping me with some issues! ** 
--]] 
  
  
addEvent('onClientMapStarting') 
  
hunterX, hunterY, hunterZ = 0,0,0 
local sWidth,sHeight = guiGetScreenSize ( ) 
  
function loadHunterPosition () 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                break 
            end 
        end 
    end 
end 
  
function drawDistance ( ) 
    loadHunterPosition ( ) 
    local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
    local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
    dxDrawText ("Distance To Hunter:",sWidth-721,sHeight-55,sWidth-515,sHeight-36,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    dxDrawImage( 
    (sWidth-699), 
    (sHeight-31), 
    (265), 
    (30), 
    "images/DistToHunter-Logo.png",0.0,0.0,0.0,tocolor(255,255,255,255),false) 
    dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",sWidth-494,sHeight-54,sWidth-366,sHeight-35,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) 
end 
  
function handleRender ( ) 
    addEventHandler ( 'onClientRender', root, drawDistance ) 
end 
  
addEventHandler ( 'onClientMapStarting', root, handleRender ) 
  
function delRender ( ) 
    removeEventHandler ( 'onClientRender', root, drawDistance ) 
end 
  
addEventHandler ( 'onClientPlayerWasted', getLocalPlayer(), delRender ) 
  
function delRenderForDeath ( ) 
    if ( getElementHealth ( getLocalPlayer() ) == '0' ) then 
        removeEventHandler ( 'onClientRender', root, drawDistance ) 
    end 
end 
  

Link to comment
  • Moderators

Of course, if you make like this:

sWidth-494, you backward the text from 494 pixels, and 494 pixels are not the same in 1366*768 and 800*600 do you understand ?

I made a converter for this problem, but I need your resolution when you make this script ( the MTA resolution of course ).

Link to comment
  • Moderators

Ok I finished this is the full code:

--[[ 
  
                ** Distance To Hunter v1.0 - Made by Pr0RaC3R ** 
                ** Special credits to Citizen & H5N1 from MTA Forum for helping me with some issues! ** 
--]] 
  
  
addEvent('onClientMapStarting') 
  
hunterX, hunterY, hunterZ = 0,0,0 
local sWidth,sHeight = guiGetScreenSize ( ) 
  
function loadHunterPosition () 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
                break 
            end 
        end 
    end 
end 
  
function drawDistance ( ) 
    local x, y, sx, sy 
    loadHunterPosition ( ) 
    local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
    local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
    x, y = AbsoluteToRelativ(1920-721, 1080-55) 
    sx, sy = AbsoluteToRelativ( 1920-515,1080-36 ) 
    dxDrawText ("Distance To Hunter:",x, y, sx, sy,tocolor(0,252,255,255),0.6,"bankgothic","left","top",false,false,false) -- Create DX Text 
    x, y = AbsoluteToRelativ(1920-699, 1080-31) 
    sx, sy = AbsoluteToRelativ( 265, 30 ) 
    dxDrawImage( x, y, sx, sy,"images/DistToHunter-Logo.png",0.0,0.0,0.0,tocolor(255,255,255,255),false) 
    x, y = AbsoluteToRelativ(1920-494, 1080-54) 
    sx, sy = AbsoluteToRelativ( 1920-366,1080-35 ) 
    dxDrawText( string.format("%.0f",distFromVehicleToHunter) .. " m ",x, y, sx, sy,tocolor(252,160,15,255),0.5,"bankgothic","left","top",false,false,false) 
end 
  
function handleRender ( ) 
    addEventHandler ( 'onClientRender', root, drawDistance ) 
end 
  
addEventHandler ( 'onClientMapStarting', root, handleRender ) 
  
function delRender ( ) 
    removeEventHandler ( 'onClientRender', root, drawDistance ) 
end 
  
addEventHandler ( 'onClientPlayerWasted', getLocalPlayer(), delRender ) 
  
function delRenderForDeath ( ) 
    if ( getElementHealth ( getLocalPlayer() ) == '0' ) then 
        removeEventHandler ( 'onClientRender', root, drawDistance ) 
    end 
end 
  
function AbsoluteToRelativ( X, Y ) -- this coverter transforms the gui position and size for the resolution of the player  
    local rX, rY = guiGetScreenSize() 
    local x = math.floor(X*rX/1920) 
    local y = math.floor(Y*rY/1080) 
    return x, y  
end 

Not tested but I think it works

Link to comment
sWidth,sHeight = guiGetScreenSize () 
textX, textY = 0,100 
textSX, textSY = 2, 102 
outputChatBox (tostring(textSY)) 
imgX, imgY = sWidth / 2 - 265 / 2, sHeight / 2 - 30 / 2 
hunterX, hunterY, hunterZ = 0,0,0 
  
addEvent("onClientMapStarting", true) 
     
function startRendering ( ) 
    loadHunterPosition () 
end 
addEventHandler ( "onClientMapStarting", getRootElement(), startRendering ) 
  
function delRender ( ) 
    removeEventHandler ( "onClientRender", getRootElement(), drawDistance ) 
end 
addEventHandler ( "onClientPlayerWasted", getLocalPlayer(), delRender ) 
  
function loadHunterPosition () 
    for _,e in pairs ( getElementsByType ( "racepickup") or {} ) do -- Loop through race pickup elements (nitro, repair, vehiclechange) 
        local t = getElementData (e, "type") 
        if t and t == "vehiclechange" then 
            local v = getElementData (e,"vehicle") 
            if v and tonumber(v) == 425 then -- If it finds hunter pickup... 
                hunterX, hunterY, hunterZ = getElementPosition ( e ) -- Get hunter position 
            end 
        end 
    end 
end 
  
function drawDistance ( ) 
    local playerX, playerY, playerZ = getElementPosition ( getLocalPlayer( ) ) -- Get player position 
    local distFromVehicleToHunter = getDistanceBetweenPoints3D ( playerX, playerY, playerZ, hunterX, hunterY, hunterZ ) -- Calculate distance 
    dxDrawText ("Distance To Hunter: " .. tostring(string.format("%.0f",distFromVehicleToHunter)) .. "m ",textSX, textSY, sWidth, 200,tocolor(0,0,0,255),0.6,"bankgothic","center","center",false,false,false) 
    dxDrawText ("Distance To Hunter: " .. tostring(string.format("%.0f",distFromVehicleToHunter)) .. "m ",textX, textY, sWidth, 200,tocolor(0,252,255,255),0.6,"bankgothic","center","center",false,false,false) 
end 
addEventHandler ( "onClientRender", getRootElement(), drawDistance ) 
  

optimized. Added shadow to text.

Edited by Guest
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...