Advertisement
Rochet2

Lua safe objects Eluna

Jan 26th, 2014
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.48 KB | None | 0 0
  1. -- Safe player pointers
  2. -- Returns nil if player is not available
  3.  
  4. local players = {}
  5. local function OnLogin(event, player)
  6.     local guid = player:GetGUIDLow()
  7.     if (players[guid] == false) then -- use a small trick to use false as true here, while using it as false in SafeGetPlayer
  8.         players[guid] = player -- renew
  9.     end
  10. end
  11. local function OnLogout(event, player)
  12.     players[player:GetGUIDLow()] = false -- false marks that the player should be saved back to store on login
  13. end
  14.  
  15. RegisterPlayerEvent(3, OnLogin)
  16. RegisterPlayerEvent(4, OnLogout)
  17.  
  18.  
  19. -- Functions for scripts:
  20. -- Store player (from now on)
  21. function Player:Store() -- Can be used as player:Store()
  22.     players[self:GetGUIDLow()] = self
  23. end
  24. -- Remove player from store
  25. function Player:Remove(guidlow) -- Can be used as player:Remove() or player.Remove(guidlow) or Player.Remove(guidlow) or Player:Remove(guidlow)
  26.     players[guidlow or self:GetGUIDLow()] = nil
  27. end
  28. -- Get player or nil
  29. function SafeGetPlayer(guidlow) -- Can be used as SafeGetPlayer(guidlow)
  30.     return players[guidlow] or nil
  31. end
  32.  
  33.  
  34.  
  35. -- example:
  36.  
  37. local function OnChat(event, player, msg, lang, typ, misc)
  38.     if (msg == "store") then
  39.         player:Store()
  40.     end
  41.     if (msg == "remove") then
  42.         player:Remove()
  43.     end
  44.     if (msg == "test") then
  45.         local guid = player:GetGUIDLow()
  46.         CreateLuaEvent(function() print(guid, SafeGetPlayer(guid)) end, 500, 0)
  47.     end
  48. end
  49.  
  50. RegisterPlayerEvent(18, OnChat)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement