Scripting

Sub pages
Scripting a System

Scripting a Class

Creating the ActionList System

Global Utility Library

Easing

The plan is to do most game logic through scripts. At the moment, the scripting will be done in Lua.

So far, all of the scripting will be done based off of events that occur. You can register for events in the Initialize function. If you want more information on events, go to the Events wiki page.

The function "Initialize" MUST be present in the script in order to be attached as a component, even if it is empty.

Script Template
Here is a template you can use for scripting. This will setup a basic component with an update loop. function Initialize owner.Space:RegisterEvent("LogicUpdate"); end function LogicUpdateEvent(event) end

Lua 101
Obviously not going to explain all of the basics for Lua, just the specifics for the engine. For a better overview of all of Lua look here.

First off, when scripting a component you have access to two global variables to represent the current component and current object that the script is operating on. These variables are "this" and "owner".

For example, the following code prints out the current component's address and the current object's id. function Initialize print("Address of this component!" .. this) print("Id of the current game object!" .. owner:GetId) end

This leads into calling functions on objects. The colon operator signals Lua to pass the object into the function it is calling as an implicit "self" variable. You must use a colon operator to call all functions through Lua.

If you forget and use the following code you might get an error such as this one: print("Id of the current game object!" .. owner . GetId) Lua Error: ..\Data\LuaScripts\TestScript.lua:6: bad argument #1 to 'GetId' (GameObject expected, got table) The error "X expected, got table" means that an object was expected to be passed along with the function, but it was not.

Everything in Lua is passed by reference by default. This means that you can get unexpected behavior when you thing you are saving the value of a variable.

Example: function AddNumbers(variable) local badVariable = variable badVariable = badVariable + 10 badVariable = badVariable * 2 badVariable = badVariable - 5 return badVariable end AddNumbers(owner.Transform.Position.x) In this example, owner.Transform.Position.x is being passed by reference into the function. The function "saves" the reference into a local, and the modifies the local. Since these are all references, the position of the object will change.

In order to get a value from a reference you need to use the Value function.

Example: function AddNumbers(variable) local savingVariable = Value(variable) savingVariable = savingVariable + 10 savingVariable = savingVariable * 2 savingVariable = savingVariable - 5 return savingVariable end AddNumbers(owner.Transform.Position.x) This will correctly save the current value of the owner's x Position, modify the copy of the data and return it from the function. The owner's position will not be affected.