• ENT:Touch
    19 replies, posted
  • When a player bumps into or "Touches" a prop I need a console command to run on him. ENT:Touch is ran on the server though...so how do I tell the server to run a command on the player that bumps into it? [code] function ENT:Touch( thisguy ) if ( thisguy:IsValid() and thisguy:IsPlayer() ) then thisguy:Notify("Fuck you."); end end [/code] I've tried "thisguy:ConCommand" but that didn't work.
  • Avatar of _nonSENSE
  • ENT.Touch is serverside. You don't need to use any console commands. Just run the function you want to run.
  • [QUOTE=Nexus435;36388845]_R.Player:ConCommand should work fine.[/QUOTE] Can someone please tell me what the hell is this _R.XXX shit?
  • Avatar of brandonj4
  • [QUOTE=Remscar;36390394]Can someone please tell me what the hell is this _R.XXX shit?[/QUOTE] I think it's a prefix for the metatable that you want to use instead of using: local player = FindMetaTable("Player") function player:Myfunc() print("meta") end you can use function _R.Player:Myfunc() print("returns metatables") end
  • [QUOTE=brandonj4;36390661]I think it's a prefix for the metatable that you want to use instead of using: local player = FindMetaTable("Player") function player:Myfunc() print("meta") end you can use function _R.Player:Myfunc() print("returns metatables") end[/QUOTE] So they do the same thing, however the first way is more performance efficient.
  • Avatar of KatNotDinner
  • if you localize the _R.XXX it's more efficent than FindMetaTable. Try not localizing them and you'll see that _R.XXX is slightly faster.
  • [QUOTE=Hentie;36392565]not really[/QUOTE] I made the assumption that that exact code structure would be used and "_R.Player" would not be localized while FindMetaTable("Player") would be. However as KatNotDinner pointed out above me, you can localize _R.XXX (Which i did not think of, my brain having the dumb lately), which is more efficient than using FindMetaTable() and localizing the results. I stand corrected.
  • Avatar of brandonj4
  • What I read on the actual lua wiki was that, localizing variables makes the code run faster. Having that said, I don't see the point while localizing so many variables in lua when it already runs fast on its own.
  • Avatar of Feihc
  • [QUOTE=Remscar;36390394]Can someone please tell me what the hell is this _R.XXX shit?[/QUOTE] Anytime I reference the player object like that it's to overload functions i.e. [lua] local oldSetHealth = _R.Player.SetHealth function _R.Entity:SetHealth(amount) print("Health of " .. self .. " has been set to " .. amount") oldSetHealth(amount) end [/lua] Then anytime you call Entity:SetHealth() it does that print. I don't really care about the performance shit of all this, but it works.
  • Avatar of _nonSENSE
  • [QUOTE=Feihc;36403177]Anytime I reference the player object like that it's to overload functions i.e. [lua] local oldSetHealth = _R.Player.SetHealth function _R.Entity:SetHealth(amount) print("Health of " .. self .. " has been set to " .. amount") oldSetHealth(amount) end [/lua] Then anytime you call Entity:SetHealth() it does that print. I don't really care about the performance shit of all this, but it works.[/QUOTE] You actually need to pass the reference as well: [lua] local oldSetHealth = _R.Player.SetHealth function _R.Entity:SetHealth(amount) print("Health of " .. self .. " has been set to " .. amount") oldSetHealth(self, amount) end [/lua]
  • I was working on a Lua based fix because garry's __index checks the metatable before it checks for any data in the table, but it kept breaking the server. So you can't override any of the functions on specific entities. Which sucks.
  • Avatar of Feihc
  • [QUOTE=_nonSENSE;36431137]You actually need to pass the reference as well: [lua] local oldSetHealth = _R.Player.SetHealth function _R.Entity:SetHealth(amount) print("Health of " .. self .. " has been set to " .. amount") oldSetHealth(self, amount) end [/lua][/QUOTE] Ah woops I knew mine didnt look quite right, thanks.
  • Avatar of KatNotDinner
  • [QUOTE=Map in a box;36432696]I was working on a Lua based fix because garry's __index checks the metatable before it checks for any data in the table, but it kept breaking the server. So you can't override any of the functions on specific entities. Which sucks.[/QUOTE] You should be able to bypass thre check using rawget(table, key), just like you bypass __newindex using rawset(table, key, value). Hope this helps. @Feigc at the first line it should be _R.Player.oldSetHealth @DarkSunrise it doesn't matter if the difference is slight. The more optimized code, the better. Just because we have faster machines doesn't mean that we can realy on speed and produce shit code because it looks better or because we are lazy. I've been optimizing my script lately and have learned from my mistakes.
  • [QUOTE=KatNotDinner;36451685]@DarkSunrise it doesn't matter if the difference is slight. The more optimized code, the better. Just because we have faster machines doesn't mean that we can realy on speed and produce shit code because it looks better or because we are lazy. I've been optimizing my script lately and have learned from my mistakes.[/QUOTE] I'm not saying that you should intentionally produce shit code, but to balance optimization and how easy the code is to read. The latter is usually more important than shaving off a couple of microseconds from the execution time. Optimization might be important in networking or in a function that gets called every tick, but not in something like this where it's only called once when the script is run.
  • Avatar of Kogitsune
  • If we want to be nitpicky, the best approach is: [code] local meta = _R.Player [/code] Garbage collection is the most expensive thing you can do in Lua, followed by invoking a function. This is an obsessive micro-optimization that doesn't yield a great deal of result - it takes around one million executions to even reach the 1/5 of a second difference. [code]local t = { } for k = 1, 1000000 do t[ k ] = math.sin( k ) end z = os.clock( ) for k = 1, 1000000 do q = math.sin( k ) end delta = os.clock( ) - z z = os.clock( ) for k = 1, 1000000 do q = t[ k ] end delta2 = os.clock( ) - z print( string.format( "1,000,000 math.sin calls: ~%d miliseconds", delta * 1000 ) ) print( string.format( "1,000,000 table indexes: ~%d miliseconds", delta2 * 1000 ) )[/code] The number is going to vary, of course, but I get output in the area of [code]1,000,000 math.sin calls: ~297 miliseconds 1,000,000 table indexes: ~108 miliseconds[/code] The lesson is, class, that you should instead focus on not tripping the garbage collector as much as possible and recycle tables when you can to conserve performance. One of the fastest ways to trip it is to create a new table each frame for surface.DrawPoly, and thus at least three more subtables, as an example.
  • Avatar of KatNotDinner
  • [QUOTE=DarKSunrise;36452691]I'm not saying that you should intentionally produce shit code, but to balance optimization and how easy the code is to read. The latter is usually more important than shaving off a couple of microseconds from the execution time. Optimization might be important in networking or in a function that gets called every tick, but not in something like this where it's only called once when the script is run.[/QUOTE] Yeah I agree. It's jsut that the OP seems to be new to GLua (and programming itself) and there are other people like him who are probably reading this thread. New people have to learn that they have to optimize their code, now, because it's hard to learn and get used to it after you've made some habbits.