Advertisement
Rochet2

Some Lua notes

Nov 23rd, 2014
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.79 KB | None | 0 0
  1. Some notes:
  2. - I find it somewhat interesting that ppl use `player:GetTeam() == 0` a lot instead of `player:IsAlliance()` or similar, maybe legacy from arcemu?
  3. Personally I think the code is more readable with IsAlliance and IsHorde than GetTeam. There is no other difference really.
  4. http://eluna.emudevs.com/Player/IsAlliance.html
  5. http://eluna.emudevs.com/Player/IsHorde.html
  6. http://eluna.emudevs.com/Player/GetTeam.html
  7.  
  8. - It seems ppl use the chat hook and not command hook to make commands.
  9. Note that onchat fires on all chat while oncommand triggers only on a command, making oncommand more efficient.
  10. The command hook also enables you to write commands starting with dot.
  11. PLAYER_EVENT_ON_COMMAND: https://github.com/ElunaLuaEngine/Eluna/blob/master/Hooks.h#L201
  12.  
  13. - ppl ignore database types, *which should be followed strictly*.
  14. The types in DB and C++ (and lua): http://pastebin.com/8nNsym4w
  15. This is an important example issue related to DB types https://github.com/ElunaLuaEngine/Eluna/issues/89#issuecomment-64121361
  16.  
  17. - ppl use global functions, variables and so on. While this may work, it can create issues with performance and collision of different scripts.
  18. All local variables outside of functions in a script are shared by all - for example - NPCs running the same script, they are locally global - be wary of such.
  19. All global variables are shared by all scripts
  20.  
  21. - ppl dont understand how tables work. Tables are at the very core of lua and they should and NEED to be understood to write almost any code.
  22. Lua tables can be compared to arrays and other containers like map.
  23. Table functions and tutorials:
  24. http://www.lua.org/manual/5.2/manual.html#6.5
  25. http://www.lua.org/manual/5.2/manual.html#4.3
  26. http://lua-users.org/wiki/TablesTutorial
  27. http://www.lua.org/pil/2.5.html
  28.  
  29. - ppl use slow DB queries to store data over time instead of tables. Possibly due to not understanding how tables work.
  30. DB should only be used for data that survives over restart.
  31. A model I myself use is to have data in a lua table and if I need to save it to database,
  32. I save it only when OnSave hook fires for player and remove flood entries on startup as well as on a timely manner once a day or week or when a player is deleted for example.
  33.  
  34. - ppl dont use loops (and tables) to do things creating unnecessary repetition and bloat of code.
  35. Possibly related to them being inexperienced or not understanding tables.
  36. Often scripts like this are created by beginners, but you can see others do it as well.
  37. http://lua-users.org/wiki/ControlStructureTutorial
  38. http://www.lua.org/manual/5.2/manual.html#3.3.5
  39.  
  40. - ppl dont check if DB query or any GetSelection or similar function returned nil. This creates nil errors and log and halts the execution, which creating possibly unexpected behavior.
  41. As a solution you should be prepared that things did not go expectedly. Make checks for nils!
  42. local target = creature:GetVictim(); if (target) then print(target:GetName().." exists!") end
  43.  
  44. - ppl tend to try do everything in lua. Lua is not an escape from C++. You should not try implement everything in lua, but try script special cases and scripts in lua.
  45. "dont code base functionality in a script language"
  46.  
  47. - ppl tend to do everything in a function call. Try to execute things on startup / file body. This will make executing scripts faster.
  48. However sometimes doing so would require storing things causing you to spend ram.
  49. That being said, some work is alright, but dont do excessive work in function calls if not needed, especially ones called often.
  50.  
  51. - ppl try to save userdata like player objects or pass them to functions that are trigged in a timed matter. This should not be done without guarantee of the object/player existing when used
  52. Few exceptions to this are the DB query result objects and world packets. The reason for this is that Lua creates them and also destorys them when no longer referenced from lua.
  53. If one wants to use userdata later on, like player or item, you should store the player GUID and later find the player by his guid for example.
  54. Often you have access to a WorldObject through a hook. You can use it to search for an object or get its map object and search with GetWorldObject
  55. http://eluna.emudevs.com/Map/GetWorldObject.html
  56.  
  57. - ppl tend to use bad variable and function names. Due to this it can be hard to know what hooks someone is using or what some variable or function does.
  58. A good way to name things is to name the hooked functions by the event they trigger on
  59. Other functions named after what they do
  60. Variables named by what the variable stands for
  61.  
  62. - ppl dont know how to use the lua string library. Things like patterns (like regex) are unknown to many and they are very useful and often needed.
  63. I recommend checking out http://www.wowwiki.com/Pattern_matching and http://lua-users.org/wiki/StringLibraryTutorial
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement