Page3_En.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <html>
  2. <head>
  3. <title>
  4. Bind a Dll to LuaEdit (Tutorial) - Managing the Game
  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">Managing the Game</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. Now the script contains a solid basic frame/structure around which we will add
  32. code to actually handle the game. This means sequence randomization algorythms,
  33. key validations, point system logic, etc. Let's start by the game initialization
  34. routine.
  35. </p>
  36. <br>
  37. </font>
  38. </td>
  39. </tr>
  40. <tr>
  41. <td colspan="2">
  42. <font face="Tahoma" size="2">
  43. <p style="text-align:justify">
  44. The simon:Initialize() function previously added will be called right before a new game is
  45. requested by the player. Our main goal in this function is to reset all of our game handling variables
  46. previously created and intialize random logic. In the world of computers, generating
  47. random is not an easy thing to achieve. Let's be realistic, it's impossible to recreate
  48. exactly what random is by definition. On the other hand, we can generate something
  49. so huge in terms of quantity of possibilities, that statistically and humanly speaking, we can
  50. call this random. Lua's pseudo-random engine works the same way as in C++.
  51. In fact, "behind the scene", Lua uses the C++ pseudo-random engine. This engine works
  52. with seeds to generate "random" sequences of numbers. A seed is a number which will
  53. be used to generate "random" numbers. In other words, two sequences using the same seed will
  54. be identical. To avoid this problem, many scripts/programs use the system's current time
  55. as the random seed, which pratically makes it impossible for the user to get or even remember
  56. having the same sequence. Here is how the simon:Initialize() function should look like:
  57. </p>
  58. <br>
  59. </td>
  60. </tr>
  61. <tr>
  62. <td class="code" colspan="2">
  63. <br>
  64. <blockquote>
  65. -- Initialize the game<br>
  66. function simon:Initialize()<br>
  67. &nbsp;&nbsp;&nbsp;&nbsp;-- Initalize variables<br>
  68. &nbsp;&nbsp;&nbsp;&nbsp;simon:SetScore(0)<br>
  69. &nbsp;&nbsp;&nbsp;&nbsp;GameState = true<br>
  70. &nbsp;&nbsp;&nbsp;&nbsp;UserSequenceCount = 0<br>
  71. &nbsp;&nbsp;&nbsp;&nbsp;SequenceCount = 0<br>
  72. &nbsp;&nbsp;&nbsp;&nbsp;MainSequence = {}<br><br>
  73. &nbsp;&nbsp;&nbsp;&nbsp;-- Initialize random engine<br>
  74. &nbsp;&nbsp;&nbsp;&nbsp;math.randomseed(os.time())<br>
  75. &nbsp;&nbsp;&nbsp;&nbsp;math.random()<br>
  76. &nbsp;&nbsp;&nbsp;&nbsp;math.random()<br>
  77. &nbsp;&nbsp;&nbsp;&nbsp;math.random()<br>
  78. end<br>
  79. </blockquote>
  80. </td>
  81. </tr>
  82. <tr>
  83. <td colspan="2">
  84. <font face="Tahoma" size="2">
  85. <br>
  86. <br>
  87. <p style="text-align:justify">
  88. In this previous code example, math.random() function is called three times because under
  89. some operating systems (at least in Windows 2k<sup>®</sup>) the first random number you get is not really
  90. "randomized". (Source taken from the <a href="http://lua-users.org/wiki/MathLibraryTutorial">lua-users wiki</a> website)
  91. To get better pseudo-random numbers, we just pop some random numbers before using
  92. them for real. The other lines of code in that previous example initialize the variables previously
  93. declared in the <a href=".\Page2_En.html">step 2</a> of this tutorial. Next, let's add some code
  94. in the simon:AddSequence(Sequence) function. The goal of this function is to append a new item to
  95. the sequence that the player is going to have to reproduce later. To do so, we will increment by 1
  96. the global variable SequenceCount and we will "randomize" a number from 1 to 4. That generated number
  97. is going to represent the light to turn on in the sequence. Here is how the function should looks like:
  98. </p>
  99. <br>
  100. </td>
  101. </tr>
  102. <tr>
  103. <td class="code" colspan="2">
  104. <br>
  105. <blockquote>
  106. -- Add one more part to the game's sequence<br>
  107. function simon:AddSequence()<br>
  108. &nbsp;&nbsp;&nbsp;&nbsp;SequenceCount = SequenceCount + 1<br>
  109. &nbsp;&nbsp;&nbsp;&nbsp;MainSequence[SequenceCount] = math.random(4)<br>
  110. end<br>
  111. </blockquote>
  112. </td>
  113. </tr>
  114. <tr>
  115. <td colspan="2">
  116. <font face="Tahoma" size="2">
  117. <br>
  118. <br>
  119. <p style="text-align:justify">
  120. Next, we will fill the simon:PlaySequence(Sequence) function. The main goal of
  121. this function is to play the sequence previously built via calls to the
  122. Simon<sup>®</sup> game's engine exported functions. In this function, we should
  123. parse the sequence table item by item and turn on the light that this item is worth.
  124. Let's be more specific:
  125. </p>
  126. <br>
  127. </td>
  128. </tr>
  129. <tr>
  130. <td class="code" colspan="2">
  131. <br>
  132. <blockquote>
  133. -- Play the game's sequence<br>
  134. function simon:PlaySequence(Sequence)<br>
  135. &nbsp;&nbsp;&nbsp;&nbsp;local v = nil<br>
  136. &nbsp;&nbsp;&nbsp;&nbsp;local i = nil<br><br>
  137. &nbsp;&nbsp;&nbsp;&nbsp;-- Lock controls from user to make sure no conflicts happened while palying sequence<br>
  138. &nbsp;&nbsp;&nbsp;&nbsp;simon.LockControls()<br><br>
  139. &nbsp;&nbsp;&nbsp;&nbsp;-- Play all sequence<br>
  140. &nbsp;&nbsp;&nbsp;&nbsp;repeat<br>
  141. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;simon.SetLight(SIMON_NONE)<br>
  142. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleep(300)<br>
  143. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i, v = next(Sequence, i)<br>
  144. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;simon.SetLight(v)<br><br>
  145. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if i ~= nil then<br>
  146. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sleep(500)<br>
  147. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>
  148. &nbsp;&nbsp;&nbsp;&nbsp;until i == nil<br><br>
  149. &nbsp;&nbsp;&nbsp;&nbsp;simon.UnlockControls()<br>
  150. &nbsp;&nbsp;&nbsp;&nbsp;simon.SetLight(SIMON_NONE)<br>
  151. end<br>
  152. </blockquote>
  153. </td>
  154. </tr>
  155. <tr>
  156. <td colspan="2">
  157. <font face="Tahoma" size="2">
  158. <br>
  159. <br>
  160. <p style="text-align:justify">
  161. The two local variables v and i are going to be use to retrieve the result
  162. of our calls to the next() function. The next() function is used to retrieve the
  163. next element in a lua table by specifiying the actual table and the index of
  164. the current element as parameters. The simon.LockControls() and simon.UnlockControls() functions
  165. are exported from the game's engine and their main goal is to lock/unlock the
  166. controls from the player to avoid any conflict in the code. In the previous example
  167. of code, the calls to the simon.LockControls() and simon.UnlockControls() functions will be made before and after the loop that is actually
  168. displaying the sequence to the player. Also, in the code sample the "repeat...until"
  169. statement was used to allow the code to enter in the loop for the first iteration
  170. everytime it has to but any other loop syntax would have been a proper use as well.
  171. The first chunk in the loop is a call to the simon.SetLight() function using
  172. SIMON_NONE as first and only parameter. This instruction will turn off all the lights
  173. in the game because we want to make sure that for each item no other light but the one
  174. of the current item will be on. Next, we have a Sleep() call of 300 miliseconds as
  175. the argument. This call, unlike the one in <a href=".\Page2_En.html">step 2</a> of
  176. this tutorial, has the same effect on the computer but is more used as a "pause"
  177. feature than a CPU "slower" kind of feature. With what has been clarify so far,
  178. you should be able to understand the remaining lines that I didn't detailed.
  179. </p>
  180. <p style="text-align:justify">
  181. To finalize this step we will add some code to the simon:OnButtonClick(ButtonIndex)
  182. function. This function will be called by the Simon<sup>®</sup> game's engine every time
  183. the player pushes one of the four buttons in the game. Our main goal in that function
  184. is to validate the pushed button and start managing points for an eventual victory.
  185. To do so, the functions simon.GetScore() and simon.SetScore() will be used. View the
  186. simplicity of this function and the advancement in the tutorial, this function will
  187. not be explained in details. Just remember what we've dicussed so far in the turorial:
  188. </p>
  189. <br>
  190. </td>
  191. </tr>
  192. <tr>
  193. <td class="code" colspan="2">
  194. <br>
  195. <blockquote>
  196. -- Event handler called by simon.dll when any of the colored buttons are clicked<br>
  197. function simon:OnButtonClick(ButtonIndex)<br>
  198. &nbsp;&nbsp;&nbsp;&nbsp;local Result = 0<br>
  199. &nbsp;&nbsp;&nbsp;&nbsp;UserSequenceCount = UserSequenceCount + 1<br><br>
  200. &nbsp;&nbsp;&nbsp;&nbsp;if MainSequence[UserSequenceCount] == ButtonIndex then<br>
  201. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;simon.SetScore(simon.GetScore() + 10)<br>
  202. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Result = 1<br>
  203. &nbsp;&nbsp;&nbsp;&nbsp;end<br><br>
  204. &nbsp;&nbsp;&nbsp;&nbsp;return Result<br>
  205. end<br>
  206. </blockquote>
  207. </td>
  208. </tr>
  209. <tr>
  210. <td colspan="2">
  211. <font face="Tahoma" size="2">
  212. <br>
  213. <br>
  214. <p style="text-align:justify">
  215. In the next step of this tutorial, we will discuss more about controling the
  216. game in the main loop and linking the functions from the C/C++/Delphi dll file.
  217. </p>
  218. <br>
  219. </td>
  220. </tr>
  221. <tr>
  222. <td valign="bottom">
  223. <font face="Tahoma" size="2">
  224. <div align="left" valign="bottom">
  225. <a href=".\Page2_En.html">&lt;&lt; Previous</a>
  226. </div>
  227. </td>
  228. <td>
  229. <font face="Tahoma" size="2">
  230. <div align="right" valign="bottom">
  231. <a href=".\Page4_En.html">Next &gt;&gt;</a>
  232. </div>
  233. </font>
  234. </td>
  235. </tr>
  236. <tr>
  237. <td colspan="2">
  238. <font face="Tahoma" size="1" color="silver">
  239. <hr size="1" color="#000000">
  240. <div align="right">
  241. <a href="http://www.luaedit.org">www.luaedit.org</a>
  242. <br>
  243. © Copyright 2004-2005 LuaEdit
  244. <br>
  245. Bind a Dll to LuaEdit (Tutorial)
  246. </div>
  247. </font>
  248. </td>
  249. </tr>
  250. </table>
  251. </body>
  252. </html>