You should never *ever* do that. If you need something in a function, pass it as a parameter to the function. That's what parameters are for. And as a bonus it's also cheaper than reading + writing to a global value. (When i say "cheaper" i mean it's a measurable difference. Even if that difference is very very small indeed.)
All LuaSomething objects are actually wrapper tables that the game constructs on the fly. Constructing them isn't free, albeit obviously also not expensive. But whenever you have a reference ("table") to a LuaSomething (LuaPlayer in this case) you should try to pass it on if it's not super-difficult to do so.Pi-C wrote: βThu Jun 06, 2019 8:08 pm By the way, what is cheaper: Passing on the whole player (complete local table) and using player.print in the other function, or reading/passing on the player.player_index and using game.get_player(index).print? My gut feeling tells me I should've used the index β¦ :-)
+1. Works fine "on my machine".
If you *do* need the player for something else then obviously you can't shorten it :). In that case the local reference is the right way to do it. And as eduran already explained, there's a big difference between "reference" and "copy". References to the same object will change the object.Pi-C wrote: βThu Jun 06, 2019 8:08 pmI need to check whether the player is admin, and I need to pass on the table to the function; I think I read somewhere that you should make a local copy of a table if you access it more than once.eradicator wrote: βThu Jun 06, 2019 6:19 pm If you don't need the player for anything else you can shorten it to:
Code: Select all
local x = {a = 1}
local b = x
b.a = 2
game.print(x.a) --prints "2"