Ulysses

General => Developers Corner => Topic started by: LuaTenshi on January 23, 2014, 10:43:41 AM

Title: I am ready to get yelled at... (Making ULib parse player names in Lua.)
Post by: LuaTenshi on January 23, 2014, 10:43:41 AM
The idea is to make is so that you can target players by just typing their name in your code thus making thins like "ulx luarun" a bit more interesting to use.

The metamethod is tried only when key is not present in table. Meaning that the code below will only effect values that are not there meaning that if you have some thing like... Entity(3):Slay() then it won't try to parse "Entity(3)" as a player name even if there is a player under that name. (Correct me if I am wrong.)



Code: [Select]
_G.__index = function(t, k)
     local tr = ULib.getUser(tostring(k),true,nil)
     if tr then
          return tr
     else
          return nil
     end
end
setmetatable(_G, _G)

or if you like you're code in one line...

Code: [Select]
_G.__index = function(t, k) local tr = ULib.getUser(tostring(k),true,nil) if tr then return tr else return nil end end setmetatable(_G, _G)


I have also made "ulx exlua" (http://forums.ulyssesmod.net/index.php/topic,6943.msg34386.html#msg34386) which pretty much does the same thing without messing with meta tables, and also returns errors cleanly.