Vana icon indicating copy to clipboard operation
Vana copied to clipboard

[POC] Using lua metatables for more object-oriented scripts

Open diamondo25 opened this issue 8 years ago • 0 comments

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:

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

diamondo25 avatar Oct 17 '16 22:10 diamondo25