STARTING LUA SCRIPTING FOR GAMEGURU
In GameGuru, Lua has been chosen as the entity-controlling script or language. This is an introduction for those who would like to try it. As a novice I find the official documentation fairly impenetrable and suggest a better approach is via experimentation with the scripts already provided in the GameGuru Files/Scriptbank and looking at the two documents in the GameGuru Documents and Files folders respectively: “Scripting With LUA.pdf” with rules and vocabulary for the Lua language and “global.lua” which gives a functions list for GameGuru use. (These are usefully identified by g_ . ) Both documents are being developed still.
First, Lua scripts can be written in a plain text program like Notepad although I prefer the free "LuaEdit 2010" (see the web).
Secondly, the scripts save in the GameGuru Files/Scriptbank folder. Take care not to save and overwrite existing scripts (by giving your scripts new names)!
1) To control a model or entity you need three basic things:
a) A model in .x format (along with any textures it needs) in the Entity folder.
b) A .fpe instruction file also in the Entity folder with same name as the model’s .x file.
This file includes, near the end, the frame numbers for the model’s animation as well as texture instructions, sound instructions and so on. Such files can be looked at and edited in Notepad. Take care not to save and overwrite existing files!
c) A Lua script (which is named in the .fpe instruction file.) This uses a combination of commands etc. from the basic language and functions found for example in the global.lua file specially set up for GameGuru.
2) The design of a script includes an introductory function and a main function, each rounded off with end:
e.g.
function NAME OF FUNCTION_init(e)
--This introductory function also includes the list of local variables etc.
end
PLUS
function NAME OF FUNCTION _main(e)
- -This is the active function
end
Here the letter (e) is used to indicate the entity or model being operated. Note that remarks or notes can be written after two hyphens or dashes as here (Remark blocks use --[[ and ]]--).
3) Essentially a script uses groups of
conditions (such as if … with each condition having an end to round it off) and
instructions (such as PlayAnimation(e,1) to animate the model,
e.g.
If g_InKey == “1” then
PlayAnimation(e,1)
End
Here g_InKey takes a keyboard input (as a string, hence the speech marks) and PlayAnimation(…) animates the relevant part of the model’s inbuilt actions, e indicating the model, and the number 1 gives the animation required, as listed in the model’s .fpe file (mentioned above). Note that the commands here have capital letters for each component word.
4) To test this set of model, .fpe file and script, place your model in GameGuru and run. It may come up with error messages (often quite helpful) or it may run successfully. If nothing happens, check that the model has got the Lua script correctly attributed to it by clicking on its properties in GameGuru and checking the lefthand panel details. You can correct that in the properties panel here (but the effect will only last until you shut GameGuru) or change it in the .fpe file.
5) Here is a simple script to open and close each door of a several section cupboard:
* * *
function cupboard_init(e) --records the Lua script below for this model or entity (e)
varnp = 0 --declare local variable to record keyboard input below
end
function cupboard_main(e)
PlayerDist = GetPlayerDistance(e) --(e) is the Entity
if PlayerDist < 200 and g_PlayerHealth > 0 then
GetEntityPlayerVisibility(e) -- when player comes close
if 1 then
PromptTextSize(3) --allows text size to be changed 1-5
Prompt("To open: use keys 1-6") --tell player what to do
varnp = g_InKey --varnp records the key chosen
if varnp > "0" then --take the key used to choose an animation
SetAnimation(varnp) -- the chosen animation equals the inkey
PlayAnimation(e) -- here this opens/shuts the relevant door
end -- closes the inkey variable subroutine
end --closes the prompt subroutine
end -- closes the player distance check subroutine
end --closes the whole MAIN routine
* * *
Note that this script could work with any model or entity with animations 1 to 9 that can be identified by the numbers on the keyboard. However be aware that numbers in this simple routine could clash with weapons set up etc. in a game so it is only a practice example really.
CRGSOS