Jump to content

Custom HUD


FuriouZ

Recommended Posts

Hey!

What would be the easyest way to show & hide my custom hud for the players ?

When player joins with the server, then HUD will be hitten, when he logs in, then hud will be visible?

I've already tried with elementData..

Anyways here's one of the ways I tried it, but yeah.. This code is kinda messy, I know..I haven't scripting quite long time, so I don't remember how it goes

  
local showHUD = true 
  
   function drawHUD() 
         --dxDrawings... 
        end 
  
    addEvent("showTheHUD", true) 
    addEventHandler("showTheHUD", root, 
        function() 
            showHUD = true 
            addEventHandler("onClientRender", root, drawHUD)             
        end); 
     
    addEvent("hideTheHUD", true) 
    addEventHandler("hideTheHUD", root, 
        function() 
            showHUD = false 
            removeEventHandler("onClientRender", root, drawHUD)          
        end)     
  
    addEventHandler("onClientPlayerSpawn", root, 
        function() 
            if ( showHUD == true ) then  
                cancelEvent() 
            else 
                triggerEvent("showTheHUD", localPlayer) 
            end 
        end) 
  
   addEventHandler("onClientPlayerWasted", root, 
        function() 
            if ( showHUD == true ) then  
                triggerEvent("hideTheHUD", localPlayer) 
            end 
        end)     
         
   addEventHandler("onClientPlayerJoin", getRootElement(), 
        function() 
            if ( showHUD == true ) then  
                triggerEvent("hideTheHUD", localPlayer) 
            end 
        end) 
  
    addEventHandler("onClientResourceStart", resourceRoot, 
        function() 
            triggerEvent("showTheHUD", localPlayer) 
            setPlayerHudComponentVisible ( "all", false ) 
            setPlayerHudComponentVisible ( "crosshair", true )           
            setPlayerHudComponentVisible ( "radar", true )           
        end);    
  

If the player joins the server, then the HUD is still visible..

Link to comment
  
-- These functions will be called once the client-sided code is loaded itself, there's no specific need to run these within a function linked to 'onClientResourceStart' 
setPlayerHudComponentVisible("all", false) 
setPlayerHudComponentVisible("crosshair", true) 
setPlayerHudComponentVisible("radar", true) 
---- 
  
addEventHandler("onClientRender", root, 
    function() 
        local shown = isPedDead(localPlayer) -- Check whether the player is dead or not 
        -- In case of the 'Play/Freeroam' game mode, the player won't be alive until he's automatically 'spawned', so the HUD won't be shown until then 
        if not shown then -- in case the player is NOT dead 
            -- dxDrawings... 
        end 
    end 
) 
  

Link to comment

I can't test it right now, but hope this helps.

  
local hudIsVisible = false 
local hudButton = "" -- If you want to you can write a button here, so you can bind the visibility changing of the hud. If you don't want to bind it then just leave it blank. 
  
function toggleHud (state) 
    if (state == true) then 
        if (not hudIsVisible) then 
            addEventHandler('onClientRender', root,  render) -- Don't forget to change the function's name, I just wrote render, but change it to your's function name! 
            hudIsVisible = true 
        else 
            hudIsVisible = false 
            outputChatBox("You've #FF0000disabled #FFFFFFthe hud!", 255, 255, 255, true) 
        end 
    elseif (state == false) then 
        if (hudIsVisible) then 
            removeEventHandler('onClientRender', root,  render) -- Change function name here as well. 
            hudIsVisible = false 
        else 
            hudIsVisible = true 
            outputChatBox("You've #00FF00enabled #FFFFFFthe hud!", 255, 255, 255, true) 
        end 
    end 
end 
  
local bool = true 
toggleHud(bool) 
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), function () 
    if hudButton ~= "" then 
        bindKey(hudButton, "down", function () 
            bool = (not hudIsVisible) 
            toggleHud(bool) 
        end) 
    end 
end 
) 
  

If you want to make an export function, to disable and enable the hud from other resources you can add this line to the meta.xml:

  
function="toggleHud" type="client" /> 
  

So this will look like this in other resources:

  
exports.hudresourcename:toggleHud(false) -- Disables the hud, true will enable it. 
  

Link to comment
  
-- These functions will be called once the client-sided code is loaded itself, there's no specific need to run these within a function linked to 'onClientResourceStart' 
setPlayerHudComponentVisible("all", false) 
setPlayerHudComponentVisible("crosshair", true) 
setPlayerHudComponentVisible("radar", true) 
---- 
  
addEventHandler("onClientRender", root, 
    function() 
        local shown = isPedDead(localPlayer) -- Check whether the player is dead or not 
        -- In case of the 'Play/Freeroam' game mode, the player won't be alive until he's automatically 'spawned', so the HUD won't be shown until then 
        if not shown then -- in case the player is NOT dead 
            -- dxDrawings... 
        end 
    end 
) 
  

Yeah, but there isn't a event which checks if player is spawned or not ?

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