lua_scripting.rst 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. ================
  2. Scripting in Lua
  3. ================
  4. All gameplay code in Crown is written in Lua. This section will give you a basic
  5. understanding of how Lua is used and can be used in Crown.
  6. What is Lua
  7. -----------
  8. Lua is a simple, fast and lightweight scripting language which is easy to learn
  9. and supports all features needed to write any type of game.
  10. Lua is widely known in the games industry and has been used for decades to write
  11. many successful commercial games.
  12. Runtime entry points
  13. --------------------
  14. When Crown runs, it calls a small set of predefined global functions at
  15. specific times:
  16. - init()
  17. - shutdown()
  18. - update(dt)
  19. - render(dt)
  20. The init() function is called right after the runtime starts. I can be used to
  21. initialize global state etc. while shutdown() is called just before the runtime
  22. exits.
  23. update(dt) and render(dt) are called periodically as part of the game loop. You
  24. can use them to implement any gameplay logic, input handling and other per-frame
  25. updates your game may need.
  26. The boot script
  27. ---------------
  28. The boot script is the first Lua script that is executed when Crown starts.
  29. In the boot script you will typically define the global entry points we
  30. mentioned before and possibly require() additional lua scripts, like you would
  31. do in regular Lua programs. One difference is that Crown expects the .lua
  32. extension to be omitted (i.e. require("foo/bar") instead of "foo/bar.lua"). This
  33. is for consistency with how resources are referenced elsewhere.
  34. Projects may contain multiple boot scripts for different purposes (for example,
  35. separate boot scripts for the editors and the game). You can specify which boot
  36. script to use in the :doc:`Boot Config <../reference/boot_config>` file.
  37. The GameBase framework
  38. ----------------------
  39. Crown automatically generates a ``main.lua`` script in the root folder of
  40. :doc:`new projects <../getting_started/create_new_project>`.
  41. You may notice that it contains some strangely named
  42. Game.init()/Game.update()/etc. functions, as well as other utility functions.
  43. Those functions are part of the GameBase framework, which is a tiny layer built
  44. on top of the basic init/shutdown/update/render hooks we discussed earlier.
  45. The GameBase layer allows for integration with the editors and adds support to
  46. common functionalities such as loading levels, creating a default camera etc.
  47. Expert users can avoid the GameBase althogheter by defining plain
  48. init/update/shutdown but we recommend to stick with it if you plan to use our
  49. tools.
  50. Hot reloading
  51. -------------
  52. Crown fully supports reloading of gameplay code while the game is running.
  53. Hot reloading is achieved by re-executing modified Lua files. This method works
  54. well in general, but needs some care in specific occasions. Consider the
  55. following script:
  56. .. code::
  57. Foo = {}
  58. Every time Crown reloads such file, a new table will be created and its
  59. reference will be stored in the variable Foo, making the previous state
  60. unreachable.
  61. To make it hot-reload safe you could check whether the Foo objects exists
  62. already, and skip its creation if that is the case:
  63. .. code::
  64. Foo = Foo or {}