Scoreboard : Get MAX OPS e2, and Player prop count !
5 replies, posted
Hey :)
Actually i made a scoreboard for my server and i want to put the max ops of the e2 of player, and, the prop count of this player.
For the e2, i dont understand how i can do that, but for the prop count, i use this :
[CODE]
...
for Key, Player in pairs( player.GetAll() ) do
...
NbProp = Player:GetCount( "props" )
...
end
[/CODE]
Its ok if the player spawn or remove the prop, but if he cleaned up, the Nbprop does not change and i dont know why...
You can help me ?
Thx
hmmmm.... I don't know much about coding but perhaps run the function on a loop so it is constantly changing.
[QUOTE=Naografix;45610874]Its ok if the player spawn or remove the prop, but if he cleaned up, the Nbprop does not change and i dont know why...[/QUOTE]
I believe this is a side effect of the way Garry coded PLAYER:GetCount/AddCount: [URL]https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/sandbox/gamemode/player_extension.lua#L23[/URL]
To my understanding/experience with those functions, the player has to spawn another prop out before the networked prop count will be accurate.
E.g.,
- Player spawns 3 props (GetCount shows 3)
- Player uses Q->Cleanup->Cleanup Everything (GetCount shows 2, which is inaccurate; should be 0)
- Player spawns 1 prop (GetCount shows 1, accurate)
Concerning E2 ops, retrieving that is not as straightforward. The value is stored on the E2 entity serverside ( inside ent.context.[B]prfbench [/B]) and to the best of my knowledge is not networked to the client [I]except [/I]through the overlay text that shows up when you look at an E2.
You would probably have to detect when the entity is created (OnEntityCreated, serverside) and check if ent:GetClass() == "gmod_wire_expression2". You'd need to network the ops somehow, which would probably involve creating a timer for each E2 entity that does something like [B]ent:SetNWInt( "Ops", ent.context.prfbench )[/B] every x seconds.
You'd also probably need to network the owner of the E2 ( if an admin mod isn't already doing it for you ) and have some kind of function like this [B]on the client in your scoreboard[/B]:
[LUA]function GetHighestOps( ply )
local highest = 0
local ops = 0
for _, ent in pairs( ents.FindByClass( "gmod_wire_expression2" ) do
if ( ply ~= ent:GetNWEntity( "Owner" ) ) then continue end -- grab the owner (however you networked it) and make sure we only look at e2s owned by the supplied player
ops = ent:GetNWInt( "Ops" )
if ( ops > highest ) then highest = ops end
end
return highest
end[/LUA]
You'd need to use something like this function in a timer that gets updated every once in a while to get the scoreboard row's player's highest ops.
[QUOTE=Mista Tea;45612321]To my understanding/experience with those functions, the player has to spawn another prop out before the networked prop count will be accurate.[/QUOTE]
Hum okay, how i can fix this shit ?
Right now Naografix and I are corresponding over PM, but I feel this should probably be posted here in case anyone else has an interest in networking E2 ops:
[LUA]hook.Add( "OnEntityCreated", "e2.ops", function( ent )
if ( ent:GetClass() ~= "gmod_wire_expression2" ) then return end -- ignore non-e2s
local id = ent:EntIndex() -- get the unique index
timer.Create( "e2.ops." .. id, 0.33, 0, function() -- run ~3 times a second
if ( !IsValid( ent ) ) then timer.Destroy( "e2.ops." .. id ) return end -- if the e2 was removed, destroy this timer
ent:SetNWInt( "Ops", ( ent.context and ent.context.prfbench ) or 0 ) -- the e2 takes a while to load the .context table, so this will show the ops as 0 until it has been fully initialized
end )
end )[/LUA]
This will create a timer for every E2 that gets spawned and begin networking the ops 3 times a second (you can use a much lower value to be less network intensive).
When the e2 is removed, the timer will get destroyed. I also forgot to mention that the E2 takes a while to initialize, so the ent.context table isn't initialized for a while.
ent:SetNWInt( "Ops", ( ent.context and ent.context.prfbench ) or 0 ) ensures that we don't try indexing the context table before it's actually initialized.
Yep, thx, its fixed :) !
Now, the clean up ! I'll try some stuff !
Sorry, you need to Log In to post a reply to this thread.