ENT:Touch

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?



function ENT:Touch( thisguy )
	if ( thisguy:IsValid() and thisguy:IsPlayer() ) then
		thisguy:Notify("Fuck you.");
	end
end


I’ve tried “thisguy:ConCommand” but that didn’t work.

_R.Player:ConCommand should work fine.

ENT.Touch is serverside. You don’t need to use any console commands. Just run the function you want to run.

Can someone please tell me what the hell is this _R.XXX shit?

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

So they do the same thing, however the first way is more performance efficient.

not really

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.

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.

If there’s any difference - it’s negligible. Just use what you think looks better.

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.

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.

If you’re lazy to set up findmetaplayer, then use _R.

Simple.

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.

Ah woops I knew mine didnt look quite right, thanks.

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.

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.

If we want to be nitpicky, the best approach is:



local meta = _R.Player


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.


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 ) )

The number is going to vary, of course, but I get output in the area of


1,000,000 math.sin calls: ~297 miliseconds
1,000,000 table indexes: ~108 miliseconds

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.

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.