Hello,
I'm using the PostPlayerDraw hook, to draw some things above the player's head.
I'm at the point where I would need a serverside var inside this hook, to determine, whether a text should be drawn or not. I'm wondering how I could do this without performance issue (I think using the net library every PostPlayerDraw would be extremely awful).
So what would be the best way of doing this?
Thanks in advance,
Pascal
Ply:SetNWString, Ply:GetNWString
[QUOTE=iJohnny;49293476]Ply:SetNWString, Ply:GetNWString[/QUOTE]
Is there any way to set a var only clientside? Because this would sync between client and server, which would not be needed in my case.
You can register a message lets call it "ShouldDrawMessage" that just toggles some global variable, you send some update whenever it should draw with its flag.
In that receiver function you can can just even assign a stub function that does nothing if its off, thats performance wise the best you can get.
[QUOTE=P4sca1;49293523]Is there any way to set a var only clientside? Because this would sync between client and server, which would not be needed in my case.[/QUOTE]
Networked variables using SetNW<> and GetNW<> are not synced. If the client changes a value, it does not get sent to the server or updated for other clients. I would use this method. You don't need to set the NW<> all the time.
[QUOTE=Zeh Matt;49294404]You can register a message lets call it "ShouldDrawMessage" that just toggles some global variable, you send some update whenever it should draw with its flag.
In that receiver function you can can just even assign a stub function that does nothing if its off, thats performance wise the best you can get.[/QUOTE]
Can you give an example?
[QUOTE=P4sca1;49296368]Can you give an example?[/QUOTE]
Which version do you like:
Overblown but yet efficient and easily extendable to draw anything player specific:
[lua]
-- Replicated value, index is player ent index.
local playerDrawingData = {}
if SERVER then
util.AddNetworkString("PlayerDrawingCmd")
local function SendDrawingData(data)
-- Do nothing in here for now.
-- net.WriteInt(data.Var1)
end
local PLAYER = FindMetaTable("Player")
function PLAYER:SetDrawPlayerStuff(drawstuff, data)
data = data or {}
net.Start("PlayerDrawingCmd")
do
net.WriteBool(false) -- Not full update
net.WriteInt(self:EntIndex(), 16) -- Ent index.
net.WriteBool(drawStuff)
if drawStuff then
SendDrawingData(data)
playerDrawingData[self:EntIndex()] = data or {}
else
playerDrawingData[self:EntIndex()] = nil
end
end
net.Broadcast()
end
hook.Add("PlayerInitialSpawn", "PlayerDrawingStuff", function(ply)
-- New players are not aware of the previous messages so send it.
net.Start("PlayerDrawingCmd")
net.WriteBool(true) -- Full update
net.WriteInt(table.Count(playerDrawingData), 7) -- Count
-- Go over all of it and write the data.
for entIndex, data in pairs(playerDrawingData) do
net.WriteInt(entIndex, 16)
SendDrawingData(data)
end
net.Send(ply)
end)
else
local function ReadDrawingData()
local data =
{
-- Customize the data to be read.
}
return data
end
net.Receive("PlayerDrawingCmd", function(len)
local fullUpdate = net.ReadBool()
if fullUpdate then
playerDrawingData = {}
local entries = net.ReadInt(7)
while entries > 0 do
local entIndex = net.ReadInt(16)
local data = ReadDrawingData()
playerDrawingData[entIndex] = data
entries = entries - 1
end
else
local entIndex = net.ReadInt(16)
local enableDrawing = net.ReadBool()
if enableDrawing == true then
local data = ReadDrawingData()
playerDrawingData[entIndex] = data
else
-- Cleanup.
playerDrawingData[entIndex] = nil
end
end
end)
local function DrawPlayerStuff(ply, data)
-- Draw the stuff on player 'ply'
end
hook.Add("PostPlayerDraw", "PlayerHeadDrawing", function(ply)
local data = playerDrawingData[ply:EntIndex()]
if data ~= nil then
DrawPlayerStuff(ply, data)
end
end)
end[/lua]
Or the less complicated just draw some indication over the head then this will do:
[lua]if SERVER then
local PLAYER = FindMetaTable("Player")
function PLAYER:SetDrawPlayerStuff(drawStuff, data)
self:SetNWBool("DrawPlayerStuff", drawStuff)
end
else
local function DrawPlayerStuff(ply, data)
-- Draw the stuff on player 'ply'
end
hook.Add("PostPlayerDraw", "PlayerHeadDrawing", function(ply)
-- NOTE: Default value is set to false
if ply:GetNWBool("DrawPlayerStuff", false) then
local data = {} -- TODO: Set player data
DrawPlayerStuff(ply, data)
end
end)
end[/lua]
So in both variants you would draw within DrawPlayerStuff whatever you like.
If you marely want to display some kind of symbol over the head please use version 2, its access to NWVars are fast enough you won't notice any performance issue.
So at the end pick your poison, also amazing how bored I must be to write this.
Also note, I did not test any of this code but in theory should work.
You helped me a lot. I will try to mess around with the code, thanks!
Sorry, you need to Log In to post a reply to this thread.