Jump to content

Player Blips


Bilal135

Recommended Posts

I want the color of player's blips to be their team color, if they have a team, otherwise random. I tried editing the playerblips resource, but it just sets everyone's blip color to red. This is my code.

function team(source) 
    local r, g, b 
    local playerTeam = getPlayerTeam( source ) 
  
    if ( playerTeam ) then 
        r, g, b = getTeamColor ( playerTeam ) 
        setBlipColor(blip, r, g, b) 
end 
end 
addEventHandler("onPlayerLogin", resourceRoot, team)  
addEventHandler("onResourceStart", resourceRoot, team) 
  
-- Add console command to print out your team information 
addCommandHandler ( "teamInfo", teamInfo ) 
  
addEventHandler("onResourceStart", root, team) 
  
-- needs configurable blip colors, and team support 
root = getRootElement () 
color = { r, g, b } 
players = {} 
resourceRoot = getResourceRootElement ( getThisResource () ) 
  
function onResourceStart ( resource ) 
    for id, player in ipairs( getElementsByType ( "player" ) ) do 
        if ( players[player] ) then 
            blip = createBlipAttachedTo ( player, 0, 2, players[source][1], players[source][2], players[source][3] ) 
        else 
            blip = createBlipAttachedTo ( player, 0, 2, color[1], color[2], color[3] ) 
        end 
    end 
end 
  
function onPlayerSpawn ( spawnpoint ) 
    if ( players[source] ) then 
        blip = createBlipAttachedTo ( source, 0, 2, players[source][1], players[source][2], players[source][3] ) 
    else 
        blip = createBlipAttachedTo ( source, 0, 2, color[1], color[2], color[3] ) 
    end 
end 
  
function onPlayerQuit () 
    destroyBlipsAttachedTo ( source ) 
end 
  
function onPlayerWasted ( totalammo, killer, killerweapon ) 
    destroyBlipsAttachedTo ( source ) 
end 
  
function setBlipsColor ( source, commandName, r, g, b ) 
    if ( tonumber ( b ) ) then 
        color = { tonumber ( r ), tonumber ( g ), tonumber ( b ) } 
        for id, player in ipairs( getElementsByType ( "player" ) ) do 
            destroyBlipsAttachedTo ( player ) 
            if ( players[player] ) then 
                blip = createBlipAttachedTo ( player, 0, 2, players[source][1], players[source][2], players[source][3] ) 
            else 
                blip = createBlipAttachedTo ( player, 0, 2, color[1], color[2], color[3] ) 
            end 
        end 
    end 
end 
  
function setBlipColor ( source, commandName, r, g, b ) 
    if ( tonumber ( b ) ) then 
        destroyBlipsAttachedTo ( source ) 
        players[source] = { tonumber ( r ), tonumber ( g ), tonumber ( b ) } 
        createBlipAttachedTo ( source, 0, 2, players[source][1], players[source][2], players[source][3] ) 
    end 
end 
  
addCommandHandler ( "setblipscolor", setBlipsColor ) 
addCommandHandler ( "setblipcolor", setBlipColor ) 
addEventHandler ( "onResourceStart", resourceRoot, onResourceStart ) 
addEventHandler ( "onPlayerSpawn", root, onPlayerSpawn ) 
addEventHandler ( "onPlayerQuit", root, onPlayerQuit ) 
addEventHandler ( "onPlayerWasted", root, onPlayerWasted ) 
  
function destroyBlipsAttachedTo(player) 
    local attached = getAttachedElements ( player ) 
    if ( attached ) then 
        for k,element in ipairs(attached) do 
            if getElementType ( element ) == "blip" then 
                destroyElement ( element ) 
            end 
        end 
    end 
end 

Thanks in advance.

Link to comment
  • Moderators
Try detecting whenever the player's team changes, it's much more efficient than using an infinite timer.

True, but since there is no "onPlayerTeamChange" event, he has to search the whole dayz script to find places where the team has been set. And after that(+ adding exports), he can only use it for the dayz gamemode. That's why I suggested it that way.

Woow, 2750 posts I am now a Foot Soldier O_o"

Link to comment
  • Moderators
There's a pull request for it on GitHub though: https://github.com/multitheftauto/mtasa-blue/pull/10

Myeah, but it won't be merged untill I fix the case when a team is destroyed/deleted.

In the mean time you can use my Lua implementation of the onPlayerTeamChange I made in reply of another thread:

------- Logic for the onPlayerTeamChange event ------- 
addEvent("onPlayerTeamChange", true) 
local _playersTeamCache = {} 
function onPlayerTeamChangeWatcher() -- DO NOT MODIFY 
    for i, player in ipairs(getElementsByType("player")) do 
        local pTeam = getPlayerTeam(player) 
        local oldTeam = _playersTeamCache[player] or false 
        if pTeam and pTeam ~= oldTeam then -- If he changed team 
            triggerEvent("onPlayerTeamChange", player, oldTeam, pTeam) 
            _playersTeamCache[player] = pTeam 
        end 
    end 
end 
setTimer(onPlayerTeamChangeWatcher, 600, 0) --[[ you can change the "sensibility"  
of the team change detection: the lower the timer is, the faster the event will be triggered]] 
  
addEventHandler("onPlayerQuit", root, function () 
    _playersTeamCache[source] = nil 
end) 

Of course I had to use an infinite timer for that.

And here is the how you can use it to solve your problem:

function addBlipToPlayer(player) -- will create an attached blip for a player 
    local teamColorR, teamColorG, teamColorB = 255, 255, 255 -- default will be white 
    local team = getPlayerTeam(player) 
    if team then 
        teamColorR, teamColorG, teamColorB = getTeamColor(team) 
    end 
     
    local blip = createBlipAttachedTo(player, 0, 2, teamColorR, teamColorG, teamColorB) 
    setElementData(player, "teamBlip", blip) -- store the blip in the player element under the key "teamBlip" 
end 
  
function addBlipToPlayersOnStart() -- will create an attached blip for everyone 
    for k, player in ipairs( getElementsByType("player") ) do 
        addBlipToPlayer(player) 
    end 
end 
addEventHandler("onResourceStart", root, addBlipToPlayersOnStart) 
  
function addBlipToPlayerOnJoin() -- will create an attached blip for anyone who joins the server 
    addBlipToPlayer(source) 
end 
addEventHandler("onPlayerJoin", root, addBlipToPlayerOnJoin) 
  
function updatePlayerBlipColor(prevTeam, newTeam) -- will update the player's blip color whenever he changes team (includes: Join, Leave or Switching) 
    -- defining the RGBA color the blip will take 
    local teamColorR, teamColorG, teamColorB = 255, 255, 255 -- default will be white 
    local blipAlpha = 255 
    if newTeam then 
        teamColorR, teamColorG, teamColorB = getTeamColor(newTeam) 
    end 
    -- getting back the attached blip to that player and set the new color 
    local blip = getElementData(source, "teamBlip") -- getting the blip back from the player element using the key 
    if blip then 
        setBlipColor(blip, teamColorR, teamColorG, teamColorB, blipAlpha) 
    end 
end 
addEventHandler("onPlayerTeamChange", root, updatePlayerBlipColor) 

I didn't test it but I have a good feeling about this code ;)

By the way if you want to set a random color for the players who are not part of a team, then you have to check that it's not a color already used by a team or too close of another team color which will be kinda painfull to code. So I would stick with a predefined default color (I used white but you can change it of course).

Best regards,

Citizen

Link to comment

I even tried this, but still doesnt work.

  
function destroyBlipAttachedTo(player) 
    local attachedElements = getAttachedElements(player) 
    if attachedElements then 
        for i, attachedElement in ipairs(attachedElements) do 
            if getElementType(attachedElement) == "blip" then 
                destroyElement(attachedElement) 
            end 
        end 
    end 
end 
addEventHandler("onPlayerQuit", root, destroyBlipAttachedTo) 

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