• Hook for when an entity receives another player's EyeTrace?
    4 replies, posted
Is there a hook/event that gets called when an entity is hit by a player's EyeTrace? That is, when a player "looks at" the entity. I would like to hook into this and do some stuff, but unfortunately I can't find an event that handles it. I did find [URL="https://wiki.garrysmod.com/page/Player/GetEyeTrace"]Player:GetEyeTrace[/URL] but the only way I could think of to make use of it was to setup a function that will set a custom property in my code for every entity in the world, e.g. (pseudocode) [CODE]for every_single_entity in the_world do entity.hit_by_trace = false end[/CODE] then have it perform the trace for all players, and set the hit_by_trace for the entities hit by the trace to true, and call the function constantly almost like a Think hook... But then it would fire constantly... Yeah, this whole thing would need some work, would involve a huge overhead on the server by being constantly processed, and probably wouldnt even work right... So yeah, I'm stumped by this one. Anyone have some advice? Maybe I'm missing something glaringly obvious. Hopefully it's possible. Thank you!
[url]https://facepunch.com/showthread.php?t=1362830&p=43879082&viewfull=1#post43879082[/url] [url]https://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index1026.html[/url] Do either of these help in any way?
findme: Thank you for the, well, the finds :D Much appreciated. Checked out both of those posts... It looks as though these functions would still need to be actively (and frequently) monitored in order to make it work... I suppose I'm looking for an event trigger, which is why my brain went straight to a hook. E.g. when a player's EyeTrace hits an entity, it "calls" something like "EyeTraceReceived" on that entity which could then be hooked into. Failing that though those functions will definitely prove useful. Perhaps I could help myself by supplying more info on what I'm trying to do. I'm working with the Prop Hunt gamemode and am trying to make some fun stuff happen when a hunter has been staring at a prop for a few moments. Think of it like the "I'm not sure if this is someone on the props team or n--HEY! GET BACK HERE!" moment we all love. :D Ultimately I want to detect when a hunter is looking at a player's prop (of course hasn't killed it yet or this doesn't really apply) and then hook into movement for the prop so that if/when they start running away, I can do stuff programmatically. My thoughts were to either hook into some kind of entity event like I mentioned, "EyeTraceOnYou" for example, or hook into the eye tracing for each hunter and detect when a Player entity gets hit by it. Use that to set a flag on that entity, something like [CODE]ent.a_hunter_is_staring_at_you_menacingly_so_act_natural = true[/CODE] Now the entity has that property set, I would make use of other internals (possibly even the same function/hook) to track it and handle if the hunter loses interest/goes away. But then, if the player starts running away, all I'd have to do is check if that property is set to true and then I can go from there.
I wrote a big long post, but only when I was done with it did I realise it wasn't really answering your needs. Here it is anyway: [url]http://pastebin.com/Zs7fUsrj[/url] Your actual two needs seem to be: a need for hunters to mark suspicious objects as maybe being players, and then running custom functions if the suspicious entity runs off a need for prop players to be alerted when someone is staring at them in the end your best bet is to loop all eye traces in one form or another to infer what you want i hope something of this helps?
These posts actually did help, and @bitches: your pastebin actually got me pointed in the right direction. After accepting/assuming there just isn't any event or hook to trigger it with, I expanded upon the code you have there and got it working! :D I had to add a few things in though to get it working right. I employed your Think hook and the table tracking per Hunter player, with some extra checks to see if a round is in progress, if the hunters have been released, entities are valid, etc.... And from there, I hooked into FinishMove with a handler function that watches the props and sets a timer on them if a hunter has stared at them for 4+ seconds. Anyways, thank you both find me and bitches for your help! :D I'll post my code minus my own adaptations, maybe others will be able to benefit from/use it. (Note: the HUNTERS_UNBLINDED variable you see at line 2 here is a global tracker I coded that gets set to true when the hunters are released from their blind at the start of a round, other than that I see no reason this wouldn't work straight out-of-the-box with an otherwise unmodified prop hunt gamemode) [lua]function Prop_FinishMoveHandler(pl, moveData) if GAMEMODE:InRound() and HUNTERS_UNBLINDED and IsValid(pl) then if pl:Alive() and pl:Team() == TEAM_PROPS then for _, pl2 in pairs(player.GetAll()) do if IsValid(pl2) then if pl2:Team() == TEAM_HUNTERS and pl2:Alive() and pl2.EyeTraceData then local ent = pl2.EyeTraceData.Entity if IsValid(ent) then if pl:GetClass() == "ph_prop" and IsValid(ent.ph_prop) then ent = ent.ph_prop end -- 1.) Within 100...uhh...distance units -- 2.) Been staring at us for 4 seconds if pl:GetPos():Distance(pl2:GetPos()) < 100 and pl2.EyeTraceData.StareTime >= 4 and ent == pl then timer.Create("RunawayPropCheck_" .. pl:Name() .. "_" .. pl2:Name(), 0.25, 0, function() RunawayPropCheck(pl, pl2) end) end end end end end end end return nil end function RunawayPropCheck(pl, pl2) if !pl.runaway_lastpos then pl.runaway_lastpos = pl:GetPos() else if pl.runaway_lastpos:Distance(pl:GetPos()) > 3 then -- TRIGGERED! if timer.Exists("RunawayPropCheck_" .. pl:Name() .. "_" .. pl2:Name()) then timer.Remove("RunawayPropCheck_" .. pl:Name() .. "_" .. pl2:Name()) end -- Do fun "runaway prop" stuff here elseif pl2.EyeTraceData.Entity != pl then -- Untriggered, hunter no longer cares about us... stop checking if timer.Exists("RunawayPropCheck_" .. pl:Name() .. "_" .. pl2:Name()) then timer.Remove("RunawayPropCheck_" .. pl:Name() .. "_" .. pl2:Name()) end end end end hook.Add("FinishMove", "Prop_FinishMoveHandler", Prop_FinishMoveHandler)[/lua]
Sorry, you need to Log In to post a reply to this thread.