The source itself is just partially my work. The original code written by Brandon Ehle in 1999. I started to update the code, but the greater part of the code written by Mildred. Many thanks for his efforts.

+

The source itself is just partially my work. The original code written by Brandon Ehle in 1999. I started to update the code, but the greater part of the code written by Mildred. Many thanks for her efforts.

-

+

== Setup your enviroment==

== Setup your enviroment==

-

The very first step is download, complie and install LUA. You can get it from the official site:

+

The very first step is download, compile and install LUA. You can get it from the official site:

[http://www.lua.org/ LUA homepage]

[http://www.lua.org/ LUA homepage]

Line 20:

Line 19:

This tutorial uses the LUA 5.1 version.

This tutorial uses the LUA 5.1 version.

-

The next step is setting up an external CS project within your enviroment. Don't forget to set up your project as a dynamic link library, and the neccessary CS and LUA libs and include director, . The concrete steps depending from your IDE and operating system. ( This is a so called "professional" tutorial. Professional tutorials are not just for C++ masters , but I assume, you can set up this project without any external help.)

+

The next step is setting up an external CS project within your enviroment. Don't forget to set up your project as a dynamic link library, and the neccessary CS and LUA libs and include directories . The concrete steps depending from your IDE and operating system. ( This is a so called ''professional'' tutorial. Professional tutorials are not just for C++ masters , but I assume, you can set up this project without any external help.)

Line 48:

Line 47:

macro.

macro.

-

Tha csLua scf class will encapsulate the lua related stuff.

+

Tha ''csLua'' scf class will encapsulate the lua related stuff.

<pre>

<pre>

Line 64:

Line 63:

</pre>

</pre>

-

We define the constructor and the virtual destructor first. The shared_instance is a static csLua instance, this object will be used by Crystal Space. We need a know about the ObjectRegistry, object_reg will store a pointer to this. The lua_State* is a specia statment indicator, when you knows Lua, yo knoe, what is it.

+

First we define the constructor and the virtual destructor. The shared_instance is a static ''csLua'' instance, this object will be used by ''Crystal Space''. We need a know about the ObjectRegistry, object_reg will store a pointer to this. The ''lua_State*'' is a special statment indicator, when you knows Lua, yo know, what is it.

The public filed continues with functions:

The public filed continues with functions:

Line 73:

Line 72:

</pre>

</pre>

-

The Initialize function will warm up the plugin. The HandleEvent function will deal with the plugin-related events (these are spcial events, like the command line help event.

+

The ''Initialize'' function will warm up the plugin. The ''HandleEvent'' function will deal with the plugin-related events (these are spcial events, like the command line help event.

-

Here comes the iScript interface functions:

+

Here comes the ''iScript'' interface functions:

-

The iScript allows us to run text form a char arrray (RunTexT, or from file(LOadModule):

+

The ''iScript'' allows us to run text form a char arrray (''RunTex'' or from file(LoadModule):

<pre>

<pre>

Line 99:

Line 98:

</pre>

</pre>

-

The foloowing functions read the stored varibles value by name:

+

The following functions read the stored varibles value by name:

<pre>

<pre>

Line 114:

Line 113:

</pre>

</pre>

-

Next iScript ensures us many Call() to call stored functions:

+

Next ''iScript'' ensures us many ''Call()'' to call stored functions:

<pre>

<pre>

virtual bool Call(const char *name, const char *fmt, ...)

virtual bool Call(const char *name, const char *fmt, ...)

Line 140:

Line 139:

</pre>

</pre>

-

The plugin needs an event handler. We hide this handler inside the csLua class:

+

The plugin needs an event handler. We hide this handler inside the ''csLua'' class:

<pre>

<pre>

Line 162:

Line 161:

</pre>

</pre>

-

The event handler simply delegates the events to csLua class. So csLua need a reference to the handler:

+

The event handler simply delegates the events to ''csLua'' class. So ''csLua'' needs a reference to the handler:

<pre>

<pre>

Line 168:

Line 167:

</pre>

</pre>

-

The header is ready, let's go to create the impelematation.

+

The header is ready, let's go to create the implementation.

-

==Impelementation ==

+

==Implementation ==

-

The cslua.cpp will start some LUA heder:

+

The ''cslua.cpp'' will start some Lua header:

<pre>

<pre>

Line 202:

Line 201:

</pre>

</pre>

+

+

Nowe some SWIG-related stuff:

+

+

<pre>

+

extern "C" {

+

int SWIG_luaopen_cspace(lua_State* L);

+

}

+

</pre>

+

+

The cspace is the great SWIG interface file. Swig will generate from the CS API the bindings after this file. You can find it in your ''CS/include/bindings/'' directory. We need to modify it later.

Current revision

Contents

Creating a LUA plugin

Short indtroduction

In this tutorial we will create a plugin for integrating Lua. That means, you can reach the the whole CS API with LUA scripting. Note, you can any SWIG supported language integrate easily into Crystal Space. In that case, when SWIG is new for you, please have a look at the project here:

A note about the history of this plugin.
The source itself is just partially my work. The original code written by Brandon Ehle in 1999. I started to update the code, but the greater part of the code written by Mildred. Many thanks for her efforts.

Setup your enviroment

The very first step is download, compile and install LUA. You can get it from the official site:

You can find here also some precompiled version, you can use them, when you want.
This tutorial uses the LUA 5.1 version.

The next step is setting up an external CS project within your enviroment. Don't forget to set up your project as a dynamic link library, and the neccessary CS and LUA libs and include directories . The concrete steps depending from your IDE and operating system. ( This is a so called professional tutorial. Professional tutorials are not just for C++ masters , but I assume, you can set up this project without any external help.)

Start coding

Crystal Space uses the iScript interface to communicate with scripting languages, so we have to implement this interface to integrate LUA. So we include some CS headers:

First we define the constructor and the virtual destructor. The shared_instance is a static csLua instance, this object will be used by Crystal Space. We need a know about the ObjectRegistry, object_reg will store a pointer to this. The lua_State* is a special statment indicator, when you knows Lua, yo know, what is it.