Page2_En.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <html>
  2. <head>
  3. <title>
  4. Bind a Dll to LuaEdit (Tutorial) - Setup Code Bases
  5. </title>
  6. <link rel="stylesheet" href="..\Tutorial.css" type="text/css">
  7. </head>
  8. <body bgcolor="#FFFFFF" vlink="silver" alink="navy" link="navy">
  9. <table width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
  10. <tr>
  11. <td valign="bottom">
  12. <div align="left">
  13. <b><font face="Tahoma" size="3" color="navy">Setup Code Bases</font></b>
  14. </div>
  15. </td>
  16. <td>
  17. <font face="Tahoma" size="1" color="silver">
  18. <div align="right" valign="top">
  19. <a href="http://www.lua.org">Lua homepage</a>
  20. </div>
  21. </font>
  22. </td>
  23. </tr>
  24. <tr valign="top">
  25. <td colspan="2">
  26. <hr size="1" color="#000000">
  27. <br>
  28. <br>
  29. <font face="Tahoma" size="2">
  30. <p style="text-align:justify">
  31. Before you start programming logics of the Simon<sup>®</sup> game, there are actually some basics
  32. code setups to do just like in any other script. Let's start by adding the following
  33. variables at the begenning of script:
  34. </p>
  35. <br>
  36. </font>
  37. </td>
  38. </tr>
  39. <tr>
  40. <td class="code" colspan="2">
  41. <br>
  42. <blockquote>
  43. -- Set "constants" for Simon game<br>
  44. local SIMON_NONE = 0<br>
  45. local SIMON_RED = 1<br>
  46. local SIMON_BLUE = 2<br>
  47. local SIMON_YELLOW = 3<br>
  48. local SIMON_GREEN = 4
  49. </blockquote>
  50. </td>
  51. </tr>
  52. <tr>
  53. <td colspan="2">
  54. <font face="Tahoma" size="2">
  55. <br>
  56. <br>
  57. <p style="text-align:justify">
  58. These variables will actually be used as if they were constants since the concept
  59. of constants does not really exist in Lua. This means that our code should never
  60. modify their content. Next, we will add four "actual" variables (their content will
  61. probably change during the execution of the script) to control the game's environment:
  62. </p>
  63. <br>
  64. </td>
  65. </tr>
  66. <tr>
  67. <td class="code" colspan="2">
  68. <br>
  69. <blockquote>
  70. -- Game handling variables<br>
  71. local GameState = true<br>
  72. local MainSequence = {}<br>
  73. local SequenceCount = 0<br>
  74. local UserSequenceCount = 0
  75. </blockquote>
  76. </td>
  77. </tr>
  78. <tr>
  79. <td colspan="2">
  80. <font face="Tahoma" size="2">
  81. <br>
  82. <br>
  83. <p style="text-align:justify">
  84. The GameState variable is a boolean which is worth false if the game is over or true if not. MainSequence is
  85. a table wich contains the whole sequence since the beginning of the new game. The variable SequenceCount is
  86. an integer containing the actual total sequence's length. Finally, UserSequenceCount is an integer
  87. containing the user's sequence length so far. Next, we will need to add four functions in wich we will add
  88. some code later. Those functions will be nested into the the table simon which will be created by the game
  89. engine (simon.dll) when the script will initialize through the Initializer function:
  90. </p>
  91. <br>
  92. </td>
  93. </tr>
  94. <tr>
  95. <td class="code" colspan="2">
  96. <br>
  97. <blockquote>
  98. function simon:OnButtonClick(ButtonIndex)<br>
  99. end<br><br>
  100. function simon:AddSequence(Sequence)<br>
  101. end<br><br>
  102. function simon:PlaySequence(Sequence)<br>
  103. end<br><br>
  104. function simon:Initialize()<br>
  105. end<br>
  106. </blockquote>
  107. </td>
  108. </tr>
  109. <tr>
  110. <td colspan="2">
  111. <font face="Tahoma" size="2">
  112. <br>
  113. <br>
  114. <p style="text-align:justify">
  115. The OnClick(ButtonIndex) function will be called every time the user
  116. will click on a button. In this function we will validate the clicked
  117. button in the current sequence. The functions simon:AddSequence(Sequence),
  118. simon:PlaySequence(Sequence) and simon:Initialize() are functions that are going
  119. to be called by our own script to handle different steps during the game.
  120. One last thing remains before the end of this step: creating the game's
  121. frame. To do so, a main loop at the end of the script will be required as
  122. follows:
  123. </p>
  124. <br>
  125. </td>
  126. </tr>
  127. <tr>
  128. <td class="code" colspan="2">
  129. <br>
  130. <blockquote>
  131. simon.Create()<br><br>
  132. -- Main processing loop<br>
  133. while simon.GetPowerStatus() == 1 do<br>
  134. &nbsp;&nbsp;&nbsp;&nbsp;-- Make sure the processor doesn't runs for no reason<br>
  135. &nbsp;&nbsp;&nbsp;&nbsp;Sleep(10)<br>
  136. end<br><br>
  137. simon.Destroy()<br>
  138. </blockquote>
  139. </td>
  140. </tr>
  141. <tr>
  142. <td colspan="2">
  143. <font face="Tahoma" size="2">
  144. <br>
  145. <br>
  146. <p style="text-align:justify">
  147. This loop will handle the game from the beginning to the end. An iteration
  148. in this loop will be executed many times per second when there is no need for
  149. it. That's why the game engine (simon.dll) has exported a function called Sleep()
  150. which tells the processor to sleep for a certain amount of time in miliseconds.
  151. In other words, by using this function we will ensure that the game isn't using 100% of the processor's resources
  152. since we don't need it in this particular case. Also, as you probably noticed already,
  153. this code snippet includes two function calls from the game engine: simon.Create() and
  154. simon.Destroy(). The simon.Create() function create the Simon<sup>®</sup> game interface to interact
  155. with the user when the simon.Destroy() function destroys this interface. In the next step, we
  156. will add some code into the previously added functions.
  157. </p>
  158. <br>
  159. </td>
  160. </tr>
  161. <tr>
  162. <td colspan="2">
  163. <font face="Tahoma" size="2">
  164. <b>Hints:</b>
  165. <ul type="square">
  166. <li>The reserved word "local" used in the variables declarations code above is not
  167. necessary for the code to work since this script isn't supposed to be loaded
  168. in any other scripts. On the other hand, since these variables are going to be
  169. used ONLY in the script's global scope, it would be recommended, to avoid any
  170. potential problems, to keep the "local" instruction even if not necessary. See
  171. section 2.6 of Lua 5.0 documentation for more details. (Available in LuaEdit
  172. Help menu)</li>
  173. <li>The Create() function has an optional argument you can pass to. This argument
  174. manages how the interface will be created. When this argument is worth 1, it creates
  175. the interface in a sizeable regular window. When it's worth 2, it creates the
  176. interface in a sizeable tool window. When the argument is worth anything else
  177. than 1 or 2, it creates the interface without any borders. In our example, this
  178. optional argument was worth nil since we didn't give any to this function call.</li>
  179. </ul>
  180. </p>
  181. </td>
  182. </tr>
  183. <tr>
  184. <td valign="bottom">
  185. <font face="Tahoma" size="2">
  186. <div align="left" valign="bottom">
  187. <a href=".\Page1_En.html">&lt;&lt; Previous</a>
  188. </div>
  189. </td>
  190. <td>
  191. <font face="Tahoma" size="2">
  192. <div align="right" valign="bottom">
  193. <a href=".\Page3_En.html">Next &gt;&gt;</a>
  194. </div>
  195. </font>
  196. </td>
  197. </tr>
  198. <tr>
  199. <td colspan="2">
  200. <font face="Tahoma" size="1" color="silver">
  201. <hr size="1" color="#000000">
  202. <div align="right">
  203. <a href="http://www.luaedit.org">www.luaedit.org</a>
  204. <br>
  205. © Copyright 2004-2005 LuaEdit
  206. <br>
  207. Bind a Dll to LuaEdit (Tutorial)
  208. </div>
  209. </font>
  210. </td>
  211. </tr>
  212. </table>
  213. </body>
  214. </html>