How to include Scripts into a Level

From X-Moto
Jump to: navigation, search

Introduction

Scripting allows a level to become dynamic. You can change physic settings, move objects, modify game logic, etc. A script is written in the Lua language and can be included in a level file.

Documentation about Lua can be found here :
http://www.lua.org/manual/5.1/

To write a script, you needn't know a lot about the level files. Just use Inksmoto, where you can easily add your own script files. For doing this, you need to create a simple text file, where the functions you wanna use are put. In Inksmoto Level Editor, just give the path to this text file, in the field 'lua script' (you can find it in the level properties tab). A simple example for such a text file looks like this:

zone1 = {}

function OnLoad()
  Game.SetCameraRotationSpeed(0.05);
  Game.CameraRotate(math.pi);
  return true;
end

function zone1.OnEnter()
  Game.PlayAudio("ring");
end


Since 0.3.0, xmoto supports multiplayers ; in the script, a player is defined by an index. The first player has the number 0, the second the number 1, ...

The script examples on this page will use this basic level file:

<?xml version="1.0" encoding="utf-8"?>
<level id="tutscript">
<info>
<name>tutscript</name>
<description></description>
<author></author>
<date></date>
<sky>sky1</sky>
</info>
<script>
</script>
<limits left="0" right="50" top="30" bottom="0"/>
<block id="Block0">
<position x="0" y="0"/>
<usetexture id="default"/>
<vertex x="5" y="5"/>
<vertex x="5" y="10"/>
<vertex x="10" y="10"/>
<vertex x="10" y="5"/>
</block>
<entity id="MyPlayerStart0" typeid="PlayerStart">
<size r="0.4"/>
<position x="7.5" y="10"/>
</entity>
<entity id="Strawberry0" typeid="Strawberry">
<size r="0.4"/>
<position x="20" y="0.5"/>
</entity>
<entity id="SnowMan0" typeid="Sprite">
<param name="name" value="SnowMan"/>
<position x="19" y="0.2"/>
<param name="z" value="-1"/>
</entity>
<zone id="Zone0">
<box left="40" right="50" top="5" bottom="0"/>
</zone>
</level>

This level can be made easily with the Inksmoto level editor. It includes a block called Block0 which is a square, a strawberry called Strawberry0 and a zone called Zone0. A zone is an invisible part of the level that can execute actions when the player is inside.

Some functions require a minimum X-Moto version to work. If you use one of these functions, please put this information in the level. For example, to make your level require X-Moto version 0.2.0, add this line to your level:

<level id="tutscript" rversion="0.2.0">

Scripted level.jpg

All the code of the script is written in Lua. You can find the syntax here. However, if you choose to include the script in an xml file, < and > symbol must be replaced by &lt; and &gt;. The code of the script can be included between <script> and </script> in the level file.