Lua and configuration files
written by Barnski 2006.03.10. (barnski__hat__swissonline.ch)


This tutorial teaches you how to use my Lua Plugin to parse configuration files, and then to access the information in Dark Basic Professional.


What is LUA
You can find out more about LUA on the official website: http://www.lua.org
Note: I am NOT the author of Lua! I just wrote a wrapper in c++ that helps using Lua within DarkBasicPro applications.


A configuration file
Configuration files are useful since they can be used to configure the behaviour of an application, without having to recompile the program. This especially comes in handy when making games. Usually in games you have lots of objects that may vary in their attributes. It makes sense to define these attributes in a file, so that you can change them.
With Lua you can easily parse the content of such a file, and access the variables that are defined. You can even define fucntions in your script file, but lets make an example that treats only variables now.

An example for a configuration file could be key bindings. Lets say you have made a first person shooter. Now you want to give the player the freedom to define his keys as he likes it. So here a Lua script file comes in very handy.
You simply define variables for all the key bindings you want to be changeable in the file:

move_forward = KEY_UP
move_backward = KEY_DOWN
strafe_left = KEY_LEFT
strafe_right = KEY_RIGHT
shoot = MOUSE_LEFT

The first part of the statement is the variable name that will represent the value within the Lua interface.
The second part may be either defined variables in your program that you set global in the Lua interface before loading the script file, or you could include them in the Lua file.


How to use the file in DBPro
First you will need to enable the Lua interface:

LUA MAKE

To set global variables you can use the command:

LUA SET INT "A_KEY", 12

To load the script file, you do:

LUA LOAD "keys.cfg"

Note that you can name your script file whatever you like... for a configuration file it is typical to name it .ini or .cfg
Now the variables defined in the file are available for you. You can get them with the following function:

move_forward_key = LUA INT("move_forward")

That's already it! If you don't use the Lua interface anymore, then you can close it with:

LUA TERMINATE


Conclusion
Lua and configuration files allow an easy way to make your application flexible, customizable and personalized.
As we have seen this matters especially for games!


In the next Tutorial I will address more complex scripting functionalities!