TestScript.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #if 0
  2. #include "LuaScript.h"
  3. #include "TestScript.h"
  4. #include "Utilities.h"
  5. TestScript::TestScript (void)
  6. {
  7. RunTestScripts();
  8. }
  9. /**
  10. Because callbacks are handled in Lua, they still have to look like a Lua
  11. callback. The Script wrapper class can't "rename" them.
  12. **/
  13. int
  14. TestScript::Script_PrintNumber (lua_State* state)
  15. {
  16. // But we can enable Script support using a special constructor.
  17. Script script(state);
  18. // Retrieve the number passed on the stack.
  19. Script::Object numberObj = script.GetObject(1);
  20. // Verify it is a number and print it.
  21. if (numberObj.IsNumber())
  22. printf("%f\n", numberObj.GetNumber());
  23. // No return values.
  24. return 0;
  25. }
  26. /**
  27. Because callbacks are handled in Lua, they still have to look like a Lua
  28. callback. The Script wrapper class can't "rename" them.
  29. **/
  30. int
  31. TestScript::Script_Add(lua_State* state)
  32. {
  33. // But we can enable Script support using a special constructor.
  34. Script script(state);
  35. // Retrieve the numbers passed on the stack.
  36. Script::Object number1Obj = script.GetObject(1);
  37. Script::Object number2Obj = script.GetObject(2);
  38. // Verify it is a number and print it.
  39. if (number1Obj.IsNumber() && number2Obj.IsNumber())
  40. {
  41. script.PushNumber(number1Obj.GetNumber() + number2Obj.GetNumber());
  42. }
  43. else
  44. {
  45. script.PushNumber(0.0f);
  46. }
  47. // 1 return value.
  48. return 1;
  49. }
  50. /**
  51. Demonstrate the use of the simple .ini script-like functions for writing
  52. values to a script.
  53. **/
  54. void
  55. TestScript::DoScriptIniWriteTest()
  56. {
  57. Script script;
  58. script.ConfigSetReal("Environment", "WindSpeed", 55.0);
  59. script.ConfigSetString("Environment", "TypeStr", "Overcast");
  60. script.ConfigSetInteger("AnotherGroup", "IntegerValue", 1);
  61. script.SaveText("ScriptIniTest.dmp");
  62. }
  63. /**
  64. Demonstrate the use of the simple .ini script-like functions for reading
  65. values from a script.
  66. **/
  67. void
  68. TestScript::DoScriptIniReadTest()
  69. {
  70. Script script;
  71. script.DoFile("ScriptIniTest.dmp");
  72. float windSpeed = script.ConfigGetReal("Environment", "WindSpeed", 0.0);
  73. const char* typeStr = script.ConfigGetString("Environment", "TypeStr", "Clear");
  74. int integerValue = script.ConfigGetInteger("AnotherGroup", "IntegerValue");
  75. printf("windSpeed: %f, typeStr: %s, intValue: %d\n", windSpeed, typeStr, integerValue);
  76. }
  77. /**
  78. Demonstrate registering callback functions for the Lua script.
  79. **/
  80. void
  81. TestScript::DoScriptCallbackTest()
  82. {
  83. Script script;
  84. script.Register("PrintNumber", Script_PrintNumber);
  85. script.Register("Add", Script_Add);
  86. script.DoFile("ScriptCallbackTest.scr");
  87. }
  88. /**
  89. Demonstrate reading and saving a script.
  90. **/
  91. void
  92. TestScript::DoScriptSaveTest()
  93. {
  94. Script script;
  95. script.DoFile("ScriptSaveTest.scr");
  96. script.SaveText("ScriptSaveTest.dmp");
  97. }
  98. /**
  99. Demonstrates walking an array table.
  100. **/
  101. void
  102. TestScript::DoScriptArrayTest()
  103. {
  104. Script script;
  105. script.DoFile("ScriptArrayTest.scr");
  106. Script::Object testTableObj = script.GetGlobals().GetByName("TestArray");
  107. // or script.GetGlobal("TestArray");
  108. for (int i = 1; ; ++i)
  109. {
  110. Script::AutoBlock block(script); // Automatic stack fixups.
  111. Script::Object entryObj = testTableObj.GetByIndex(i);
  112. if (entryObj.IsNil())
  113. break;
  114. if (entryObj.IsNumber()) // IsNumber() must ALWAYS come first.
  115. printf("%f\n", entryObj.GetNumber());
  116. else if (entryObj.IsString()) // IsString() returns true for a number or string.
  117. printf("%s\n", entryObj.GetString());
  118. }
  119. }
  120. void
  121. TestScript::RunTestScripts (void)
  122. {
  123. DoScriptIniWriteTest();
  124. DoScriptIniReadTest();
  125. DoScriptCallbackTest();
  126. DoScriptSaveTest();
  127. DoScriptArrayTest();
  128. }
  129. #endif