• PlayerCanHearPlayersVoice doesn't work
    17 replies, posted
So here is my PlayerCanHearPlayersVoice function: [lua] function GM:CanHearPlayersVoice( listener, talker ) for k, v in pairs( ents.FindInSphere( talker:GetPos(), 100 )) do if v.GetClass() == "blah1" then for k, v in pairs( ents.FindInSphere( listener:GetPos(), 100 )) do if v.GetClass() == "blah2" then return true end end end end return false end [/lua] if sv_alltalk on then it doesn't work, but if it's off then noone can hear voice of other players. Can this be because of my gamemode is team based or not? And how can i fix this?
it's v:GetClass() not v.GetClass().
[QUOTE=Megalan;16942781]So here is my PlayerCanHearPlayersVoice function: [lua] function GM:CanHearPlayersVoice( listener, talker ) for k, v in pairs( ents.FindInSphere( talker:GetPos(), 100 )) do if v.GetClass() == "blah1" then for k, v in pairs( ents.FindInSphere( listener:GetPos(), 100 )) do if v.GetClass() == "blah2" then return true end end end end return false end [/lua] if sv_alltalk on then it doesn't work, but if it's off then noone can hear voice of other players. Can this be because of my gamemode is team based or not? And how can i fix this?[/QUOTE] I'm assuming it's a proximity talk-type script. I haven't tested this snippet, but it should work: [code]function GM:CanHearPlayersVoice( listener, talker ) for _, v in pairs( ents.FindInSphere( listener:GetPos(), 100 ) ) do if ValidEntity(v) and v != listener and v:GetClass() == "blah1" then return true end end return false end[/code] Also, make sure to indent, it makes your code easier to read. EDIT: What Lexic said too. :P
Try this. It's probably more efficient than your code. [lua]function GM:Tick() local t1,t2 = {},{} for _,ent in ipairs(ents.GetAll()) do if ent:GetClass() == "bla1" then table.insert(t1,ent) elseif ent:GetClass() == "bla2" then table.insert(t2,ent) end end for _,ply in ipairs(player.GetAll()) do local pp = ply:GetPos() ply._CanBeHeard = false ply._CanHear = false for _,ent in ipairs(t1) do if ent:GetPos():Distance(pp) <= 100 then ply._CanBeHeard = true break end end for _,ent in ipairs(t2) do if ent:GetPos():Distance(pp) <= 100 then ply._CanHear = true break end end end end function GM:PlayerCanHearPlayersVoice(player, chatterbox) return player._CanHear and chatterbox._CanBeHeard end[/lua]
[QUOTE=Lexic;16943650]Try this. It's probably more efficient than your code. [lua]function GM:Tick() local t1,t2 = {},{} for _,ent in ipairs(ents.GetAll()) do if ent:GetClass() == "bla1" then table.insert(t1,ent) elseif ent:GetClass() == "bla2" then table.insert(t2,ent) end end for _,ply in ipairs(player.GetAll()) do local pp = ply:GetPos() ply._CanBeHeard = false ply._CanHear = false for _,ent in ipairs(t1) do if ent:GetPos():Distance(pp) <= 100 then ply._CanBeHeard = true break end end for _,ent in ipairs(t2) do if ent:GetPos():Distance(pp) <= 100 then ply._CanHear = true break end end end end function GM:PlayerCanHearPlayersVoice(player, chatterbox) return player._CanHear and chatterbox._CanBeHeard end[/lua][/QUOTE] Why not just do said checks in "PlayerCanHearPlayersVoice" hook rather than every tick?
[QUOTE=Unrealomega;16943734]Why not just do said checks in "PlayerCanHearPlayersVoice" hook rather than every frame?[/QUOTE] tick is called every 60 frames or so, and because to my knowledge, PlayerCanHearPlayersVoice is called every frame for every player.
[QUOTE=Lexic;16943795]tick is called every 60 frames or so, and because to my knowledge, PlayerCanHearPlayersVoice is called every frame for every player.[/QUOTE] Wiki states it's called every tick anyway.
[QUOTE=Unrealomega;16943810]Wiki states it's called every tick anyway.[/QUOTE] Right, for every player. So if you had 4 players on, it'd be called 16 times a tick. [code] player 1, player 1 player 1, player 2 player 1, player 3 player 1, player 4 player 2, player 1 player 2, player 2 player 2, player 3 player 2, player 4 player 3, player 1 player 3, player 2 player 3, player 3 player 3, player 4 player 4, player 1 player 4, player 2 player 4, player 3 player 4, player 4 [/code]
[QUOTE=Lexic;16943882]Right, for every player. So if you had 4 players on, it'd be called 16 times a tick. [code] player 1, player 1 player 1, player 2 player 1, player 3 player 1, player 4 player 2, player 1 player 2, player 2 player 2, player 3 player 2, player 4 player 3, player 1 player 3, player 2 player 3, player 3 player 3, player 4 player 4, player 1 player 4, player 2 player 4, player 3 player 4, player 4 [/code][/QUOTE] I said that the Wiki states that "PlayerCanHearPlayersVoice" is called on tick anyway, overriding Tick to do the same thing with PlayerCanHearPlayersVoice is rather redundant. Also, you're creating two new tables each tick, it'd be better to have the tabless outside the function.
[QUOTE=Unrealomega;16943925]I said that the Wiki states that "PlayerCanHearPlayersVoice" is called on tick anyway, overriding Tick to do the same thing with PlayerCanHearPlayersVoice is rather redundant. Also, you're creating two new tables each tick, it'd be better to have the tabless outside the function.[/QUOTE] So what you're saying is [lua]function GM:Tick() local t1,t2 = {},{} for _,ent in ipairs(ents.GetAll()) do if ent:GetClass() == "bla1" then table.insert(t1,ent) elseif ent:GetClass() == "bla2" then table.insert(t2,ent) end end for _,ply in ipairs(player.GetAll()) do local pp = ply:GetPos() ply._CanBeHeard = false ply._CanHear = false for _,ent in ipairs(t1) do if ent:GetPos():Distance(pp) <= 100 then ply._CanBeHeard = true break end end for _,ent in ipairs(t2) do if ent:GetPos():Distance(pp) <= 100 then ply._CanHear = true break end end end end[/lua] is just the same as [lua] function GM:Tick() for _, ply in ipairs(player.GetAll()) do for _, ply2 in ipairs(player.GetAll()) do local t1,t2 = {},{} for _,ent in ipairs(ents.GetAll()) do if ent:GetClass() == "bla1" then table.insert(t1,ent) elseif ent:GetClass() == "bla2" then table.insert(t2,ent) end end for _,ply in ipairs(player.GetAll()) do local pp = ply:GetPos() ply._CanBeHeard = false ply._CanHear = false for _,ent in ipairs(t1) do if ent:GetPos():Distance(pp) <= 100 then ply._CanBeHeard = true break end end for _,ent in ipairs(t2) do if ent:GetPos():Distance(pp) <= 100 then ply._CanHear = true break end end end end end end[/lua] ? Also the tables would have to be wiped every tick anyway, by replacing them with another table, so another table would be created every time but would remain there, full of ents, after the tick had ended.
[QUOTE=Lexic;16943987]So what you're saying is [lua]function GM:Tick() local t1,t2 = {},{} for _,ent in ipairs(ents.GetAll()) do if ent:GetClass() == "bla1" then table.insert(t1,ent) elseif ent:GetClass() == "bla2" then table.insert(t2,ent) end end for _,ply in ipairs(player.GetAll()) do local pp = ply:GetPos() ply._CanBeHeard = false ply._CanHear = false for _,ent in ipairs(t1) do if ent:GetPos():Distance(pp) <= 100 then ply._CanBeHeard = true break end end for _,ent in ipairs(t2) do if ent:GetPos():Distance(pp) <= 100 then ply._CanHear = true break end end end end[/lua] is just the same as [lua] function GM:Tick() for _, ply in ipairs(player.GetAll()) do for _, ply2 in ipairs(player.GetAll()) do local t1,t2 = {},{} for _,ent in ipairs(ents.GetAll()) do if ent:GetClass() == "bla1" then table.insert(t1,ent) elseif ent:GetClass() == "bla2" then table.insert(t2,ent) end end for _,ply in ipairs(player.GetAll()) do local pp = ply:GetPos() ply._CanBeHeard = false ply._CanHear = false for _,ent in ipairs(t1) do if ent:GetPos():Distance(pp) <= 100 then ply._CanBeHeard = true break end end for _,ent in ipairs(t2) do if ent:GetPos():Distance(pp) <= 100 then ply._CanHear = true break end end end end end end[/lua] ? Also the tables would have to be wiped every tick anyway, by replacing them with another table, so another table would be created every time but would remain there, full of ents, after the tick had ended.[/QUOTE] I'm saying that PlayerCanHearPlayersVoice is run every Tick. Anyway, you can just do table.Empty(). EDIT: Can't seem to get CanHearPlayersVoice to be called on my server. I have this code on autorun: [code]local Interval = {} Interval.Hear = 0 Interval.Tick = 0 function PlayerCanHear(plyOne, plyTwo) Interval.Hear = Interval.Hear + 1 print("Player", Interval.Hear, CurTime()) end hook.Add("CanHearPlayersVoice", "PlayerCanHear", PlayerCanHear) function TestTick() Interval.Tick = Interval.Tick + 1 print("Tick", Interval.Tick, CurTime()) end hook.Add("Tick", "TestTick", TestTick)[/code]
[QUOTE=Unrealomega;16944039]I'm saying that PlayerCanHearPlayersVoice is run every Tick. Anyway, you can just do table.Empty().[/QUOTE] So you think that each tick a different random pair of players is selected and PlayerCanHearPlayersVoice is run once with those two players? And table.Empty(tab) is (As far as I know) for key in pairs(tab) do tab[key] = nil end, which is a bit excessive. It is more efficient to create tables and destroy them as you need them than to keep them lying about and run expensive loops on them just so you can keep using one table.
[QUOTE=Lexic;16944095]So you think that each tick a different random pair of players is selected and PlayerCanHearPlayersVoice is run once with those two players? And table.Empty(tab) is (As far as I know) for key in pairs(tab) do tab[key] = nil end, which is a bit excessive. It is more efficient to create tables and destroy them as you need them than to keep them lying about and run expensive loops on them just so you can keep using one table.[/QUOTE] You'd be creating a new pair of tables each tick, even if it does get destroyed. Rather, having them recycled would be more efficient, but that's just based on opinion. Anyway, I think we've already remedied his problem.
[QUOTE=Unrealomega;16944039] EDIT: Can't seem to get CanHearPlayersVoice to be called on my server. I have this code on autorun: [code]local Interval = {} Interval.Hear = 0 Interval.Tick = 0 function PlayerCanHear(plyOne, plyTwo) Interval.Hear = Interval.Hear + 1 print("Player", Interval.Hear, CurTime()) end hook.Add("CanHearPlayersVoice", "PlayerCanHear", PlayerCanHear) function TestTick() Interval.Tick = Interval.Tick + 1 print("Tick", Interval.Tick, CurTime()) end hook.Add("Tick", "TestTick", TestTick)[/code][/QUOTE] [lua]local Interval = {} Interval.Hear = 0 Interval.Tick = 0 function PlayerCanHear(plyOne, plyTwo) Interval.Hear = Interval.Hear + 1 print("Player", Interval.Hear, CurTime()) assert(Interval.Hear < 200) end hook.Add("PlayerCanHearPlayersVoice", "PlayerCanHear", PlayerCanHear) function TestTick() Interval.Tick = Interval.Tick + 1 print("Tick", Interval.Tick, CurTime()) assert(Interval.Tick < 200) end hook.Add("Tick", "TestTick", TestTick)[/lua] [code] Player 121 140.81999206543 Player 122 140.81999206543 Player 123 140.81999206543 Player 124 140.81999206543 Player 125 140.81999206543 Player 126 140.81999206543 Tick 148 140.86499023438 Tick 149 140.91000366211 Tick 150 140.95500183105 Tick 151 141 Tick 152 141.04499816895 Tick 153 141.08999633789 Tick 154 141.13499450684 Player 127 141.13499450684 Player 128 141.13499450684 Player 129 141.13499450684 Player 130 141.13499450684 Player 131 141.13499450684 Player 132 141.13499450684 Tick 155 141.17999267578 Tick 156 141.22499084473 Tick 157 141.27000427246 Tick 158 141.31500244141 Tick 159 141.36000061035 Tick 160 141.4049987793 Tick 161 141.44999694824 Player 133 141.44999694824 Player 134 141.44999694824 Player 135 141.44999694824 Player 136 141.44999694824 Player 137 141.44999694824 Player 138 141.44999694824 Tick 162 141.49499511719 Tick 163 141.53999328613 Tick 164 141.58499145508 Tick 165 141.62998962402 Tick 166 141.67500305176 Tick 167 141.7200012207 Tick 168 141.76499938965 Player 139 141.76499938965 Player 140 141.76499938965 Player 141 141.76499938965 Player 142 141.76499938965 Player 143 141.76499938965 Player 144 141.76499938965 Tick 169 141.80999755859 Tick 170 141.85499572754 Tick 171 141.89999389648 Tick 172 141.94499206543 Tick 173 141.98999023438 Tick 174 142.03500366211 Tick 175 142.08000183105 Player 145 142.08000183105 Player 146 142.08000183105 Player 147 142.08000183105 Player 148 142.08000183105 Player 149 142.08000183105 Player 150 142.08000183105 Tick 176 142.125 Tick 177 142.16999816895 Tick 178 142.21499633789 Tick 179 142.25999450684 Tick 180 142.30499267578 Tick 181 142.34999084473 Tick 182 142.39500427246 Player 151 142.39500427246 Player 152 142.39500427246 Player 153 142.39500427246 Player 154 142.39500427246 Player 155 142.39500427246 Player 156 142.39500427246 Tick 183 142.44000244141 Tick 184 142.48500061035 Tick 185 142.5299987793 Tick 186 142.57499694824 Tick 187 142.61999511719 Tick 188 142.66499328613 Tick 189 142.70999145508 Player 157 142.70999145508 Player 158 142.70999145508 Player 159 142.70999145508 Player 160 142.70999145508 Player 161 142.70999145508 Player 162 142.70999145508 Tick 190 142.75498962402 Tick 191 142.80000305176 Tick 192 142.8450012207 Tick 193 142.88999938965 Tick 194 142.93499755859 Tick 195 142.97999572754 Tick 196 143.02499389648 Player 163 143.02499389648 Player 164 143.02499389648 Player 165 143.02499389648 Player 166 143.02499389648 Player 167 143.02499389648 Player 168 143.02499389648 Tick 197 143.06999206543 Tick 198 143.11499023438 Tick 199 143.16000366211 Tick 200 143.20500183105 Hook 'TestTick' Failed: :15: assertion failed! Player 169 143.33999633789 Player 170 143.33999633789 Player 171 143.33999633789 Player 172 143.33999633789 Player 173 143.33999633789 Player 174 143.33999633789 Player 175 143.6549987793 Player 176 143.6549987793 Player 177 143.6549987793 Player 178 143.6549987793 Player 179 143.6549987793 Player 180 143.6549987793 Player 181 143.9700012207 Player 182 143.9700012207 Player 183 143.9700012207 Player 184 143.9700012207 Player 185 143.9700012207 Player 186 143.9700012207 Player 187 144.28500366211 Player 188 144.28500366211 Player 189 144.28500366211 Player 190 144.28500366211 Player 191 144.28500366211 Player 192 144.28500366211 Player 193 144.59999084473 Player 194 144.59999084473 Player 195 144.59999084473 Player 196 144.59999084473 Player 197 144.59999084473 Player 198 144.59999084473 Player 199 144.91499328613 Player 200 144.91499328613 Hook 'PlayerCanHear' Failed: :8: assertion failed! ] status hostname: bla version : 1.0.0.59/14 3791 secure udp/ip : 192.168.0.9:27015 map : gm_construct at: 723 x, -358 y, -79 z players : 6 (8 max) # userid name uniqueid connected ping loss state adr # 3 "||VM|| Lexi" STEAM_0:1:16678762 03:03 28 0 active loopback # 4 "Bot01" BOT active # 5 "Bot02" BOT active # 6 "Bot03" BOT active # 7 "Bot04" BOT active # 8 "Bot05" BOT active [/code] What interesting results [editline]05:53PM[/editline] That said, if you change the print to [lua] print(plyOne,plyTwo,"Player", Interval.Hear, CurTime()) [/lua] you find out that it was only being called for me and not the bots. [code] Player [1][||VM|| Lexi] Player [1][||VM|| Lexi] Player 169 256.73999023438 Player [1][||VM|| Lexi] Player [2][Bot01] Player 170 256.73999023438 Player [1][||VM|| Lexi] Player [3][Bot02] Player 171 256.73999023438 Player [1][||VM|| Lexi] Player [4][Bot03] Player 172 256.73999023438 Player [1][||VM|| Lexi] Player [5][Bot04] Player 173 256.73999023438 Player [1][||VM|| Lexi] Player [6][Bot05] Player 174 256.73999023438 [/code]
Thanks to Lexic
pretty funny guys. [lua]function GM:CanHearPlayersVoice(listener, talker) return listener:GetPos():Distance(talker:GetPos()) <= 100 end[/lua]
[QUOTE=JetBoom;16946686]pretty funny guys. [lua]function GM:CanHearPlayersVoice(listener, talker) return listener:GetPos():Distance(talker:GetPos()) <= 100 end[/lua][/QUOTE] He's trying to develop a radio system where users can voice chat near a broadcaster and anyone near a receiver can hear them. (I assume, I see no other way the code he initially posted could be used.) Thus, your code is useless.
[QUOTE=Lexic;16948324]He's trying to develop a radio system where users can voice chat near a broadcaster and anyone near a receiver can hear them. (I assume, I see no other way the code he initially posted could be used.)[/QUOTE] Yeah, something like this
Sorry, you need to Log In to post a reply to this thread.