My aim is to make a SWEP that requires one to hold down a button (right mouse button in this case), to complete a “task”.
When the players has been holding down the key for a certain amount of time, a function is called.
But if the player stops holding down the key, the progress stops, and is reset to allow them to do this again.
How would I go about doing this? I’d imagine it being too intensive to do checks in the Think hook, especially as I have to check if the player is looking at a prop (which would require me to do an eye trace every Think).
If you use ply:GetEyeTrace() it is cached per frame/tick so no matter how many other functions call it that frame the trace is only ran once.
I would do something like this :
On primary attack check the entity they are looking at. If it’s valid for your purposes then store it and set a variable indicating you are progressing. Also store the time so you can check when progress is complete.
In think, if you are currently progressing, check the players trace entity against the stored entity. If they aren’t the same, cancel progressing. If they are the same, check if you reached the time needed to complete progress.
[editline]20th August 2016[/editline]
Shit I left out also checking if they still holding the attack button. Like
Think function pseudo code
If progressing then
If holding attack then
If trace entity == stored entity then
If time passed >= time needed to complete then
Aimed at something else