Extrait du course Getting Started with Lua
Leadwerks Engine utlitizes the Lua scripting language. Lua is used in many games including Crysis, S.T.A.L.K.E.R.: Shadow of Chernobyl, and Garry’s Mod. More information about the Lua scripting language is available at www.lua.org.
Scripts in Leadwerks Engine can be run on both a global and per-model approach. The entire flow of a program can be written in script, or per-model scripts can be used with a game written in another programming language. In this lesson we will learn how to write a simple program using Lua script.
The Script Editor
The script editor can be used to write, edit, run, and compile Lua scripts. When we open the script editor it starts with a blank text area:
Enter this text into the text area and press F5 to run the script:
Print(« Hello »)
The printed output will be visible in the output log at the bottom of the script editor window:
What is happening when we run the script? The script editor saves the script and launches the engine interpreter. The interpreter is a standalone application that loads a script and runs it.
The interpreter can be used to make standalone games that are controlled entirely with script. The script editor saves all open scripts and launches the interpreter, passing the name of the selected script in the command line. If a script has not been saved yet and has no name, as our untitled script here does, the script editor will save the text in a temporary file with a random name. The temporary file will be deleted after the script program completes.
Debugging Scripts
We can also debug scripts. When we debug a script, it is run with the debugger instead of the interpreter. The debugger is an executable called engine.debug.exe. The debugger is the sameas the interpreter, but it is compiled with debugging options enabled. The debugger runs moreslowly than the interpreter, but it is better at catching errors.
Let’s make a script that creates an error. This code will cause an error to occur, because the Point() method is being used to point the created mesh at an entity that doesn’t exist:
–Register abstract path
RegisterAbstractPath(« »)
–Set graphics mode
if Graphics(1024,768)==0 then
Notify(« Failed to set graphics mode. »,1)
return
end
world=CreateWorld()
if world==nil then
Notify(« Failed to initialize engine. »,1)
return
end
mesh=CreateMesh()
mesh:Point(nil)
Compiling Scripts
Lua has the ability to compile scripts into precompiled byte code files. These precompiled files will load faster and can be used to protect your source code. Precompiled Lua files do not run any faster than uncompiled Lua files. To compile a script, press the F6 key. You will be prompted to save the script as a file if you have not already. The script editor will then run the luac.exe utility. This is the Lua script compiler. The Lua script compiler will load the Lua script…
Running a Game
The script interpreter can be used to run a game without the script editor. By default, the interpreter will load the Lua file « start.lua » if it exists. If name of the script file to be run can also be passed to the interpreter in the command line. If the command line the interpreter is launched with consists of only one parameter, the interpreter will assume that is the script file to load. This allows the user to simply drag a Lua file onto the interpreter to launch the script.
The script file to load can also be indicated using the +script switch, where +script is followed by the name of the Lua file to run. The diagram below illustrates how the script to run can be passed to the interpreter in the command line. As you can see, we can run either…
……….
Course Getting Started with Lua (1,28 Mo) (Cours PDF)