How would i make this render.DrawSprite function run while firing
9 replies, posted
function SWEP:PrimaryAttack()
if ( game.SinglePlayer() ) then self:CallOnClient( "PrimaryAttack" ) end
if (SERVER) then return end
local pos, material, white = Vector(0,0,50), Material( "sprites/splodesprite" ), Color( 255, 255, 255, 255 )
cam.Start3D()
render.SetMaterial( material )
render.DrawSprite( pos, 16, 16, white )
cam.End3D()
end
It only seems to work in SWEP:DrawHUD
Again, please do not use PrimaryAttack for guaranteed client code - calling it on the client in single-player is just a waste, as well. render functions only work in render hooks, which DrawHUD is but PrimaryAttack is not. You should also cache Material calls outside of the function. Here's an example of how your code should be structured:
if (SERVER) then
util.AddNetworkString("MyWeapon_AddSprite")
util.AddNetworkString("MyWeapon_RemoveSprite")
else
-- Cache the material
local matSprite = Material("sprites/splodesprite")
local vSpritePos = Vector(0, 0, 50)
local cSprite = Color(255, 255, 255, 255)
function SWEP:DrawHUD()
if (self.m_bDrawSprite) then
cam.Start3D()
render.SetMaterial(material)
render.DrawSprite(vDrawPos, 16, 16, cSprite)
cam.End3D()
end
end
net.Receive("MyWeapon_AddSprite", function()
local pWeapon = net.ReadEntity()
-- Verify the weapon exists on the client
if (pWeapon:IsValid()) then
pWeapon.m_bDrawSprite = true -- Draw the sprite
end
end)
net.Receive("MyWeapon_RemoveSprite", function()
local pWeapon = net.ReadEntity()
if (pWeapon:IsValid()) then
pWeapon.m_bDrawSprite = false -- Stop drawing the sprite
end
end)
end
function SWEP:PrimaryAttack()
-- Network to the client to draw the sprite
if (SERVER) then
net.Start("MyWeapon_AddSprite")
net.WriteEntity(self)
net.Send(self:GetOwner())
end
-- Set shared for prediction
self:SetNextPrimaryFire(CurTime() + 1)
end
function SWEP:SecondaryAttack()
-- Network to the client to stop drawing the sprite
if (SERVER) then
net.Start("MyWeapon_RemoveSprite")
net.WriteEntity(self)
net.Send(self:GetOwner())
end
self:SetNextSecondaryFire(CurTime() + 1)
end
I've seen literally NOTHING in the wiki about certain commands only working in certain hooks besides clientside commands only working in clientside hooks and sever commands in server hooks.
https://i.imgur.com/OFYXixT.png
https://i.imgur.com/YXfC8XF.png
"at any time" only refers to render hooks, regardless of their context.
But if I'm using cam.start3D() why would it matter which hook I put it in
It's all due to the order that the client calls functions/hooks:
Client frame starts
Keyboard is polled for user input
Client sends this data off to the server for movement processing
Client does its own form for movement/physics processing for smoothness (prediction). This includes firing weapons.
Frame begins drawing
Render Order
Frame data is sent to a library that displays it on the monitor
Frame data is cleared now that it's no longer needed
Client frame ends, waiting for the next frame to begin
This is a vast oversimplification and may not be an accurate representation of the actions every frame, but it does reveal why drawing outside of a render hook won't ever display: everything you draw will just be drawn over. PrimaryAttack is called before the frame has begun drawing, and thus will just be overwritten when the world is drawn on the entire screen.
Where the hell do they expect you to learn all this?
Experience - a lot of these details are very specific and implementation-dependant, so documenting them would be messy and not have a fit place to reside.
Oh well, maybe I'm not cut out for this
It's always tough at first - reading other people's code is always a good start for learning.
Sorry, you need to Log In to post a reply to this thread.