1. Post #1

    May 2012
    12 Posts
    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
    I've tried "thisguy:ConCommand" but that didn't work.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 1 (list)

  2. Post #2
    Nexus435's Avatar
    July 2010
    1,414 Posts
    _R.Player:ConCommand should work fine.

  3. Post #3
    MoronYard
    _nonSENSE's Avatar
    May 2010
    1,261 Posts
    ENT.Touch is serverside. You don't need to use any console commands. Just run the function you want to run.
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Agree Agree x 4Disagree Disagree x 1 (list)

  4. Post #4
    taste the salty dong
    Remscar's Avatar
    September 2009
    1,715 Posts
    _R.Player:ConCommand should work fine.
    Can someone please tell me what the hell is this _R.XXX shit?
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 1 (list)

  5. Post #5
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    1,372 Posts
    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
    Reply With Quote Edit / Delete Reply Windows 7 Canada Show Events Agree Agree x 2Useful Useful x 1 (list)

  6. Post #6
    taste the salty dong
    Remscar's Avatar
    September 2009
    1,715 Posts
    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.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Disagree Disagree x 5Dumb Dumb x 3Agree Agree x 1 (list)

  7. Post #7
    Hentie's Avatar
    May 2010
    1,843 Posts
    not really
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Agree Agree x 3 (list)

  8. Post #8
    KatNotDinner's Avatar
    June 2011
    769 Posts
    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.
    Reply With Quote Edit / Delete Reply Windows 7 Greece Show Events Informative Informative x 1 (list)

  9. Post #9
    taste the salty dong
    Remscar's Avatar
    September 2009
    1,715 Posts
    not really
    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.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Winner Winner x 1 (list)

  10. Post #10
    Gold Member

    July 2006
    1,819 Posts
    If there's any difference - it's negligible. Just use what you think looks better.
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Agree Agree x 1Disagree Disagree x 1Dumb Dumb x 1 (list)

  11. Post #11
    Awesome Gmod Servers!
    brandonj4's Avatar
    September 2011
    1,372 Posts
    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.
    Reply With Quote Edit / Delete Reply Windows 7 Canada Show Events Disagree Disagree x 1 (list)

  12. Post #12
    Gold Member
    Feihc's Avatar
    October 2006
    1,016 Posts
    Can someone please tell me what the hell is this _R.XXX shit?
    Anytime I reference the player object like that it's to overload functions
    i.e.
    local oldSetHealth = _R.Player.SetHealth
    function _R.Entity:SetHealth(amount)
        print("Health of " .. self .. " has been set to " .. amount")
        oldSetHealth(amount)
    end
    

    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.
    Reply With Quote Edit / Delete Reply Windows 7 United States Show Events Dumb Dumb x 1Late Late x 1 (list)

  13. Post #13
    Gold Member
    rebel1324's Avatar
    December 2008
    887 Posts
    If you're lazy to set up findmetaplayer, then use _R.

    Simple.
    Reply With Quote Edit / Delete Reply Korea, Republic of Show Events Funny Funny x 3Disagree Disagree x 1 (list)

  14. Post #14
    MoronYard
    _nonSENSE's Avatar
    May 2010
    1,261 Posts
    Anytime I reference the player object like that it's to overload functions
    i.e.
    local oldSetHealth = _R.Player.SetHealth
    function _R.Entity:SetHealth(amount)
        print("Health of " .. self .. " has been set to " .. amount")
        oldSetHealth(amount)
    end
    

    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.
    You actually need to pass the reference as well:

    local oldSetHealth = _R.Player.SetHealth
    function _R.Entity:SetHealth(amount)
        print("Health of " .. self .. " has been set to " .. amount")
        oldSetHealth(self, amount)
    end
    
    Reply With Quote Edit / Delete Reply Windows 7 Germany Show Events Zing Zing x 1 (list)

  15. Post #15
    Map in a box's Avatar
    July 2009
    5,801 Posts
    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.

  16. Post #16
    Gold Member
    Feihc's Avatar
    October 2006
    1,016 Posts
    You actually need to pass the reference as well:

    local oldSetHealth = _R.Player.SetHealth
    function _R.Entity:SetHealth(amount)
        print("Health of " .. self .. " has been set to " .. amount")
        oldSetHealth(self, amount)
    end
    
    Ah woops I knew mine didnt look quite right, thanks.

  17. Post #17
    KatNotDinner's Avatar
    June 2011
    769 Posts
    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.
    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.

  18. Post #18
    Gold Member

    July 2006
    1,819 Posts
    @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.
    Reply With Quote Edit / Delete Reply Windows 7 Finland Show Events Agree Agree x 4 (list)

  19. Post #19
    Gold Member
    Kogitsune's Avatar
    September 2005
    2,468 Posts
    If we want to be nitpicky, the best approach is:

    Code:
    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.

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

  20. Post #20
    KatNotDinner's Avatar
    June 2011
    769 Posts
    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.
    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.