Jump to content

Vehicle crosshair (scope)


Xwad

Recommended Posts

Hi. I want to make a script that makes possible that when i press mouse2 in the rhino then it will aim with a scope. Here is a script but its not working:/ Whats the problem??

  
local textures = {};  
  
local function render_crosshair () 
    local screenw, screenh = guiGetScreenSize ();  
    local s = screenw * 0.5;  
    local u,v = (screenw - s)*0.5, (screenh - s)*0.5; 
    local black = tocolor (0, 0, 0, 255); 
     
    dxDrawRectangle (0, 0, screenw, v, black); 
    dxDrawRectangle (0, 0, u, screenh, black); 
    dxDrawRectangle (0, screenh, screenw, -v, black); 
    dxDrawRectangle (screenw, screenh, -u, -screenh, black); 
    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair_background"]); 
    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair"]); 
end; 
  
local function on_weapon_aimed (key, state) 
if isPedInVehicle(source) then 
if (getElementModel(source) == 432) then 
        if state == "down" then  
            showPlayerHudComponent ("crosshair", false); 
            addEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
        else  
            removeEventHandler ("onClientHUDRender", getRootElement (), render_crosshair);  
        end; 
    end; 
end;  
  
  
addEventHandler ("onClientResourceStart", getResourceRootElement (getThisResource ()), 
    function ()  
        -- load textures.  
        textures ["crosshair"] = dxCreateTexture ("images/PKS-07_crosshair.png", "argb", true, "wrap");  
        if not textures ["crosshair"] then  
            outputDebugString ("failed to load \"images/PKS-07_crosshair.png\" texture", 1);  
            return; 
        end; 
        textures ["crosshair_background"] = dxCreateTexture ("images/PKS-07_crosshair_background.png", "argb", true, "wrap");  
        if not textures ["crosshair_background"] then 
            outputDebugString ("failed to load \"images/PKS-07_crosshair_background.png\" texture", 1);  
            return;      
        end; 
     
        bindKey ("aim_weapon", "both", on_weapon_aimed); 
        addEventHandler ("onClientPlayerWeaponSwitch", getRootElement (), 
            function (previousWeaponSlot) 
                local weaponslot_type_sniper = 6;  
                if previousWeaponSlot == weaponslot_type_sniper then  
                    if not isPlayerHudComponentVisible ("crosshair") then  
                        showPlayerHudComponent ("crosshair", true); 
                    end; 
                end;  
            end); 
    end);  
     
addEventHandler ("onClientResourceStop", getResourceRootElement (getThisResource ()), 
    function () 
        unbindKey ("aim_weapon", "both", on_weapon_aimed);  
        if not isPlayerHudComponentVisible ("crosshair") then 
            showPlayerHudComponent ("crosshair", true); 
        end; 
    end); 
     
  

Link to comment
local textures = {}; 
  
local function render_crosshair () 
    local screenw, screenh = guiGetScreenSize (); 
    local s = screenw * 0.5; 
    local u,v = (screenw - s)*0.5, (screenh - s)*0.5; 
    local black = tocolor (0, 0, 0, 255); 
    
    dxDrawRectangle (0, 0, screenw, v, black); 
    dxDrawRectangle (0, 0, u, screenh, black); 
    dxDrawRectangle (0, screenh, screenw, -v, black); 
    dxDrawRectangle (screenw, screenh, -u, -screenh, black); 
    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair_background"]); 
    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair"]); 
end; 
  
local function on_weapon_aimed (key, state) 
if isPedInVehicle(source) then 
if (getElementModel(source) == 432) then 
        if state == "down" then 
            showPlayerHudComponent ("crosshair", false); 
            addEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
        else 
            removeEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
        end; 
    end; 
end 
end; 
  
  
addEventHandler ("onClientResourceStart", getResourceRootElement (getThisResource ()), 
    function () 
        -- load textures. 
        textures ["crosshair"] = dxCreateTexture ("images/PKS-07_crosshair.png", "argb", true, "wrap"); 
        if not textures ["crosshair"] then 
            outputDebugString ("failed to load \"images/PKS-07_crosshair.png\" texture", 1); 
            return; 
        end; 
        textures ["crosshair_background"] = dxCreateTexture ("images/PKS-07_crosshair_background.png", "argb", true, "wrap"); 
        if not textures ["crosshair_background"] then 
            outputDebugString ("failed to load \"images/PKS-07_crosshair_background.png\" texture", 1); 
            return;     
        end; 
    
        bindKey ("aim_weapon", "both", on_weapon_aimed); 
        addEventHandler ("onClientPlayerWeaponSwitch", getRootElement (), 
            function (previousWeaponSlot) 
                local weaponslot_type_sniper = 6; 
                if previousWeaponSlot == weaponslot_type_sniper then 
                    if not isPlayerHudComponentVisible ("crosshair") then 
                        showPlayerHudComponent ("crosshair", true); 
                    end; 
                end; 
            end); 
    end); 
    
addEventHandler ("onClientResourceStop", getResourceRootElement (getThisResource ()), 
    function () 
        unbindKey ("aim_weapon", "both", on_weapon_aimed); 
        if not isPlayerHudComponentVisible ("crosshair") then 
            showPlayerHudComponent ("crosshair", true); 
        end; 
    end); 

Link to comment

Well, that is really a self-explanatory thing. It says you forgot to put an 'end' to close the function which starts at line 19.

In that case, the below code will work:

  
local textures = {}; 
  
local function render_crosshair () 
    local screenw, screenh = guiGetScreenSize (); 
    local s = screenw * 0.5; 
    local u,v = (screenw - s)*0.5, (screenh - s)*0.5; 
    local black = tocolor (0, 0, 0, 255); 
    
    dxDrawRectangle (0, 0, screenw, v, black); 
    dxDrawRectangle (0, 0, u, screenh, black); 
    dxDrawRectangle (0, screenh, screenw, -v, black); 
    dxDrawRectangle (screenw, screenh, -u, -screenh, black); 
    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair_background"]); 
    dxDrawImage (u, v, screenw-2*u, screenh-2*v, textures ["crosshair"]); 
end; 
  
local function on_weapon_aimed (key, state) 
if isPedInVehicle(source) then 
    if (getElementModel(source) == 432) then 
        if state == "down" then 
            showPlayerHudComponent ("crosshair", false); 
            addEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
        else 
            removeEventHandler ("onClientHUDRender", getRootElement (), render_crosshair); 
        end; 
    end; 
end; 
end; 
  
  
addEventHandler ("onClientResourceStart", getResourceRootElement (getThisResource ()), 
    function () 
        -- load textures. 
        textures ["crosshair"] = dxCreateTexture ("images/PKS-07_crosshair.png", "argb", true, "wrap"); 
        if not textures ["crosshair"] then 
            outputDebugString ("failed to load \"images/PKS-07_crosshair.png\" texture", 1); 
            return; 
        end; 
        textures ["crosshair_background"] = dxCreateTexture ("images/PKS-07_crosshair_background.png", "argb", true, "wrap"); 
        if not textures ["crosshair_background"] then 
            outputDebugString ("failed to load \"images/PKS-07_crosshair_background.png\" texture", 1); 
            return;     
        end; 
    
        bindKey ("aim_weapon", "both", on_weapon_aimed); 
        addEventHandler ("onClientPlayerWeaponSwitch", getRootElement (), 
            function (previousWeaponSlot) 
                local weaponslot_type_sniper = 6; 
                if previousWeaponSlot == weaponslot_type_sniper then 
                    if not isPlayerHudComponentVisible ("crosshair") then 
                        showPlayerHudComponent ("crosshair", true); 
                    end; 
                end; 
            end); 
    end); 
    
addEventHandler ("onClientResourceStop", getResourceRootElement (getThisResource ()), 
    function () 
        unbindKey ("aim_weapon", "both", on_weapon_aimed); 
        if not isPlayerHudComponentVisible ("crosshair") then 
            showPlayerHudComponent ("crosshair", true); 
        end; 
    end); 

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