patternMinor
Garry's Mod custom spectate player
Viewed 0 times
garryplayercustommodspectate
Problem
I have been making my own scoreboard, and needed a custom spectate function for it, so I made this:
Is there anything I could improve on? Am I over-complicating anything?
util.AddNetworkString( "spectatePlayer" )
local playerLocal
local isSpectating = false
net.Receive("spectatePlayer", function(length, client)
if isSpectating then return end
isSpectating = true
playerLocal = client
print(client:Nick() .. "Started spectating: " .. net.ReadEntity():Nick())
client:Spectate(5)
client:SpectateEntity(net.ReadEntity())
end)
hook.Add("Tick", "keydown", function()
if not isSpectating then return end
if (playerLocal:KeyDown(IN_FORWARD)) then
isSpectating = false
playerLocal:UnSpectate()
print(playerLocal:Nick().."Finished Spectating")
end
end)Is there anything I could improve on? Am I over-complicating anything?
Solution
There's a few things to comment on:
I'm not a Gmod developer, so I'm not familiar with the spectating abilities built in, but, I'd assume that if any exist, they're probably better to use than re-inventing the wheel.
There's only two states: not spectating, and spectating. What happens if I want to simply switch between players? Do I have to stop spectating, and then select a new player to spectate?
Why can I not just press lmb and go to the next?
You've got extraneous whitespace in your brackets.
String concatenation joins the strings, as they are. Meaning, it would read like:
QuillStarted spectating: James Heald.
QuillFinished Spectating
Add some whitespace in there.
Oh, and you should be keeping the cases (upper and lower) consistent in your messages:
I'm not a Gmod developer, so I'm not familiar with the spectating abilities built in, but, I'd assume that if any exist, they're probably better to use than re-inventing the wheel.
There's only two states: not spectating, and spectating. What happens if I want to simply switch between players? Do I have to stop spectating, and then select a new player to spectate?
Why can I not just press lmb and go to the next?
util.AddNetworkString( "spectatePlayer" )You've got extraneous whitespace in your brackets.
print(client:Nick() .. "Started spectating: " .. net.ReadEntity():Nick())
print(playerLocal:Nick().."Finished Spectating")String concatenation joins the strings, as they are. Meaning, it would read like:
QuillStarted spectating: James Heald.
QuillFinished Spectating
Add some whitespace in there.
print(client:Nick() .. " Started spectating: " .. net.ReadEntity():Nick())
print(playerLocal:Nick().." Finished Spectating")Oh, and you should be keeping the cases (upper and lower) consistent in your messages:
"Started spectating: "
^
"Finished Spectating"
^Code Snippets
util.AddNetworkString( "spectatePlayer" )print(client:Nick() .. "Started spectating: " .. net.ReadEntity():Nick())
print(playerLocal:Nick().."Finished Spectating")print(client:Nick() .. " Started spectating: " .. net.ReadEntity():Nick())
print(playerLocal:Nick().." Finished Spectating")"Started spectating: "
^
"Finished Spectating"
^Context
StackExchange Code Review Q#40563, answer score: 3
Revisions (0)
No revisions yet.