Jump to content

[HELP] setElementVisibleTo


aliensky

Recommended Posts

Hello, i need help. Why does this script is not working?

I can't see it when i'm in other team. It's okay but when i join to Criminal team, i can't see it again. What should i do? Thanks.

local crMarker = createMarker(1863.6999511719,-1598.6999511719,12.89999961853, 'cylinder', 2, 255, 0,0, 150)

setElementVisibleTo (crMarker,getTeamFromName ("Criminal"), true)

setElementVisibleTo (crMarker, root, false )

Link to comment
  • Moderators
local crMarker = createMarker(1863.6999511719,-1598.6999511719,12.89999961853, 'cylinder', 2, 255, 0,0, 150) 
setElementVisibleTo (crMarker, root, false ) 
for k, v in ipairs(getPlayersInTeam(getTeamFromName ("Criminal"))) do 
setElementVisibleTo (crMarker, v, true) 
end 
  

Totally pointless, it will do the exact same result. Here is the reason:

When the setElementVisibleTo is called you (aliensky) get the "Criminal" team element with his members as children.

So imagine you are "Player1" and you are not in the Criminal team, but there is "Player2" and "Player3" in the Criminal team, when this piece of code is called, it will set the visibility to true for only "Player2" and "Player3" and then it's over ! It won't update automatically if "Player1" joins the team !

So you have to set the visibility to true for anyone who will join this team, and to false for anyone who will leave that team.

A good way to do that would be to listen for an event such as "onPlayerTeamChange" but this event doesn't exist.

So I did this instead:

-- Table that will store the teams that have elements only visible for their members. 
local teamVisibilityBindings = {} 
  
-- Copying the original setElementVisibleTo function into _setElementVisibleTo: 
local _setElementVisibleTo = setElementVisibleTo 
  
-- Replacing setElementVisibleTo function by our own for the whole resource: 
_G.setElementVisibleTo = function ( theElement, visibleTo, visible ) 
    if isElement(visibleTo) and getElementType(visibleTo) == "team" then 
        addTeamVisibilityBinding(visibleTo, theElement, visible) 
    end 
end 
  
local function addTeamVisibilityBinding(team, element, visibility) 
    local bindings = teamBindings[team] 
    if not bindings then 
        teamBindings[team] = {} 
        bindings = teamBindings[team] 
    end 
    local notFound = true 
    for elem, vis in pairs(bindings) do 
        if elem == element then -- if there is already an entry for that team and element 
            local notFound = false 
            if vis ~= visibility then -- if we are changing the visibility 
                teamBindings[team][elem] = visibility -- Update it 
                refreshVisibility(true) 
            end 
        end 
    end 
    -- We checked all the bindings for that team 
    if notFound then -- It's a new element to bind its visibility to a team 
        teamBindings[team][element] = visibility -- Add it 
        refreshVisibility(true) 
    end 
end 
  
local teamMembersCache = {} 
function refreshVisibility( forced ) 
    for team, bindings in pairs(teamVisibilityBindings) do 
        local nbMembers = #getPlayersInTeam(team) 
        if teamMembersCache[team] ~= nil or nbMembers ~= teamMembersCache[team] or forced then --if the number of members changed or we are forcing it 
            for elem, vis in pairs(bindings) do 
                _setElementVisibleTo(elem, root, not vis) 
                _setElementVisibleTo(elem, team, vis) 
            end 
        end 
    end 
end 
setTimer(refreshVisibility, 1000, 0) 

This should work but I didn't tested it (I can't for now). Here is what your code should now looks like:

local crMarker = createMarker(1863.6999511719,-1598.6999511719,12.89999961853, 'cylinder', 2, 255, 0,0, 150) 
setElementVisibleTo (crMarker, getTeamFromName ("Criminal"), true) 

Link to comment
local crMarker = createMarker(1863.6999511719,-1598.6999511719,12.89999961853, 'cylinder', 2, 255, 0,0, 150) 
setElementVisibleTo (crMarker, root, false ) 
for k, v in ipairs(getPlayersInTeam(getTeamFromName ("Criminal"))) do 
setElementVisibleTo (crMarker, v, true) 
end 
  

Totally pointless, it will do the exact same result. Here is the reason:

When the setElementVisibleTo is called you (aliensky) get the "Criminal" team element with his members as children.

So imagine you are "Player1" and you are not in the Criminal team, but there is "Player2" and "Player3" in the Criminal team, when this piece of code is called, it will set the visibility to true for only "Player2" and "Player3" and then it's over ! It won't update automatically if "Player1" joins the team !

So you have to set the visibility to true for anyone who will join this team, and to false for anyone who will leave that team.

A good way to do that would be to listen for an event such as "onPlayerTeamChange" but this event doesn't exist.

So I did this instead:

-- Table that will store the teams that have elements only visible for their members. 
local teamVisibilityBindings = {} 
  
-- Copying the original setElementVisibleTo function into _setElementVisibleTo: 
local _setElementVisibleTo = setElementVisibleTo 
  
-- Replacing setElementVisibleTo function by our own for the whole resource: 
_G.setElementVisibleTo = function ( theElement, visibleTo, visible ) 
    if isElement(visibleTo) and getElementType(visibleTo) == "team" then 
        addTeamVisibilityBinding(visibleTo, theElement, visible) 
    end 
end 
  
local function addTeamVisibilityBinding(team, element, visibility) 
    local bindings = teamBindings[team] 
    if not bindings then 
        teamBindings[team] = {} 
        bindings = teamBindings[team] 
    end 
    local notFound = true 
    for elem, vis in pairs(bindings) do 
        if elem == element then -- if there is already an entry for that team and element 
            local notFound = false 
            if vis ~= visibility then -- if we are changing the visibility 
                teamBindings[team][elem] = visibility -- Update it 
                refreshVisibility(true) 
            end 
        end 
    end 
    -- We checked all the bindings for that team 
    if notFound then -- It's a new element to bind its visibility to a team 
        teamBindings[team][element] = visibility -- Add it 
        refreshVisibility(true) 
    end 
end 
  
local teamMembersCache = {} 
function refreshVisibility( forced ) 
    for team, bindings in pairs(teamVisibilityBindings) do 
        local nbMembers = #getPlayersInTeam(team) 
        if teamMembersCache[team] ~= nil or nbMembers ~= teamMembersCache[team] or forced then --if the number of members changed or we are forcing it 
            for elem, vis in pairs(bindings) do 
                _setElementVisibleTo(elem, root, not vis) 
                _setElementVisibleTo(elem, team, vis) 
            end 
        end 
    end 
end 
setTimer(refreshVisibility, 1000, 0) 

This should work but I didn't tested it (I can't for now). Here is what your code should now looks like:

local crMarker = createMarker(1863.6999511719,-1598.6999511719,12.89999961853, 'cylinder', 2, 255, 0,0, 150) 
setElementVisibleTo (crMarker, getTeamFromName ("Criminal"), true) 

You are right. I didnt understood that part when he asks for the blip to get shown when he joins the team

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