Jump to content

Crosshair


Recommended Posts

function crosshair()
    local hitX,hitY,hitZ = getPedTargetEnd ( getLocalPlayer() );
    local screenX1, screenY1 = getScreenFromWorldPosition ( hitX,hitY,hitZ );
    dxDrawImage(screenX1-(32/2), screenY1-(32/2), 32, 32, "files/crosshair.png")
end
bindKey("aim_weapon", "both", function(key, keyState)
    if keyState == "down" then
        addEventHandler("onClientRender", root, crosshair)
    else
        removeEventHandler("onClientRender", root, crosshair)
    end
end)

So. Here is my crosshair script. How can i make that, if the weapon id is 34 or 35 or 36  then remove this crosshair, and draw the default?

Link to comment
  • Discord Moderators
function crosshair()
    local hitX,hitY,hitZ = getPedTargetEnd ( getLocalPlayer() );
    local screenX1, screenY1 = getScreenFromWorldPosition ( hitX,hitY,hitZ );
    dxDrawImage(screenX1-(32/2), screenY1-(32/2), 32, 32, "files/crosshair.png")
end
bindKey("aim_weapon", "both", function(key, keyState)
    local weaponID = getPedWeapon(localPlayer)
    if (weaponID == 34) or (weaponID == 35) or (weaponID == 36) then
        return
    end
    if keyState == "down" then
        addEventHandler("onClientRender", root, crosshair)
    else
        removeEventHandler("onClientRender", root, crosshair)
    end
end)

You get the current wielded weapon ID with getPedWeapon, and you use a return statement if you want to use default crosshair. The return statement means the rest of the code in the block is not executed.

 

This code might fail if the weapon is changed to 34,35 or 36 while scoped in, as "onClientRender" will be running but won't stop then. I'm not sure if that's a problem. If it is, you can either implement a check to see if the event handler is added, and remove it, or you can add a check in onClientRender to not render.

Link to comment

screenX? Don't you mean screenX1? I guess that's because getScreenFromWorldPosition failed and returned false.

Try this:

function crosshair()
	local hitX,hitY,hitZ = getPedTargetEnd ( getLocalPlayer() );
	local screenX1, screenY1 = getScreenFromWorldPosition ( hitX,hitY,hitZ );
	if screenX1 then
		dxDrawImage(screenX1-(32/2), screenY1-(32/2), 32, 32, "files/crosshair.png")
	end
end

It might not draw it sometimes but maybe that's gonna happen for just a couple of frames and you might not see it, just try and see.

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