retep998 / Vana

Git mirror of the svn repo for Vana, a MapleStory private server written in C++.
GNU General Public License v2.0
75 stars 34 forks source link

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

Open diamondo25 opened 7 years ago

diamondo25 commented 7 years ago

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