Advertisement
Terrah

nwn2 lua example

Oct 4th, 2016
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. #include "nwnx_lua"
  2.  
  3. int Setup(){
  4.  
  5. /*
  6. function WriteToTestFile(str)
  7. local file = assert(io.open("D:/test.txt","a"));
  8. file:write(str);
  9. file:close();
  10. end
  11. */
  12.  
  13. //This defines a function that will open a file in append mode and write a string to it and then close
  14. //We only need to run this once to define the function, once its done its in memory
  15. RunLuaString("function WriteToTestFile(str) local file = assert(io.open('D:/test.txt','a'));file:write(str);file:close();end");
  16. }
  17.  
  18. int WriteToFile(string str){
  19.  
  20. //Here we call the same function we previously defined
  21. RunLuaString("WriteToTestFile([=["+str+"]=])");
  22. }
  23.  
  24. void SetLuaString(string key, string value){
  25.  
  26. /*
  27. --Ensures the sub table exists
  28. LOCAL = LOCAL or {};
  29. --Add the value
  30. LOCAL['key']='value';
  31. */
  32.  
  33. RunLuaString("LOCAL = LOCAL or {};LOCAL['"+key+"']='"+value+"';");
  34. }
  35.  
  36. string GetLuaString(string key, string def){
  37.  
  38. /*
  39. --Ensures the sub table exists
  40. LOCAL = LOCAL or {};
  41. --Return the value from the table to the caller
  42. return LOCAL['key'];
  43. */
  44.  
  45. string sReturn = RunLuaString("LOCAL = LOCAL or {}; return tostring(LOCAL['"+key+"'])");
  46.  
  47. //"nil" in lua means null, it didnt exist, return the default instead
  48. if(sReturn == "nil")
  49. return def;
  50.  
  51. return sReturn;
  52. }
  53.  
  54. void DeleteLuaString(string key){
  55.  
  56. //nil = nothing, setting a value to nil deletes it
  57. RunLuaString("LOCAL = LOCAL or {};LOCAL['"+key+"']=nil;");
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement