With the above patch you'll have additional functionality:
local player = getPlayer();
addText("Hello, " .. player:getName() .. "!\r\n");
addText("Your ID is " .. player:getId() .. "\r\n");
addText("The player object __tostring method returns: " .. player:__tostring());
sendOk();
resulting in:
Basically, getPlayer() returns a metatable + reference to the current player. Using getName() on this object in Lua, you can use the previously saved player reference (in the lua stack) to get the username.
The __tostring method is used for functions that support __tostring (for example, print()).
I've made a proof of concept to unbloat our Lua global scope, so you can do more object-oriented programming in Lua.
https://gist.github.com/diamondo25/02cf385903f955ffd0a07d611cfb7551
With the above patch you'll have additional functionality:
resulting in:
Basically,
getPlayer()
returns a metatable + reference to the current player. UsinggetName()
on this object in Lua, you can use the previously saved player reference (in the lua stack) to get the username. The__tostring
method is used for functions that support__tostring
(for example,print()
).