Using Lua as Script Language in Games Coded in Java

Formation using Lua as Script Language in Games Coded in Java, tutoriel & guide de travaux pratiques en pdf.

THREE TIPS FOR GAME SCRIPTING US-ING LUAJAVA

LuaJava is a scripting tool for Java. The goal of this tool is to allow scripts written in Lua to manipulate components developed in Java [Cassino et al., 1999]. LuaJava allows integration between Lua and Java in both directions: manipulation of Java objects by Lua scripts and manipulation of Lua objects by Java programs [Cassino et al., 1999]. The access to Java components is made from Lua without any need for declarations or any kind of preprocessing.
In this paper we have choose to work using Java objects inside Lua scripts because in that way we can create generic objects and make scripts to change the value of its attributes producing a clean, and easy to understand, code.
The rst thing we need to do when we want to handle Java objects inside Lua is to create a LuaState. The LuaState will control access to the Lua environment. We will be able to create a LuaState by doing this:
LuaState luaState;
luaState = LuaStateFactory.newLuaState();
After creating a LuaState, we now need to open the LuaJava libraries. This will be done by the following instruction:
luaState.openLibs();
Now that we have built our environment we are ready to start writing Lua code. This should be done in a Lua le (.lua). Now that we have an environment and Lua le, we just need to put all together. It should be done calling the /tt LdoFile method. This method tells the Lua environment to read the Lua le passed as a parameter. The instruction should be:
luaState.LdoFile(<luafile location>);
Once we have seen all the methods to create a Lua environment and use it inside a Java project, let’s put all together in a classic Hello World example.
void Main() {
LuaState luaState;
luaState = LuaStateFactory.newLuaState();
luaState.openLibs();
luaState.LdoFile(« helloworld.lua »);
luaState.close();
}
helloworld.lua file:
print(« Hello World »)
LuaJava also o er many other features. We will take a closer look at two of these features which will be im-portant to our scripting schema guring in our second and third tips. The rst feature is how you call a Lua function to be executed inside the Java scope. The sec-ond one is how do you use a Java object inside a Lua le.
To use a Lua function in Java you need to get the Lua global variable that stores the function, that will be done by calling the method getGlobal passing the function’s name as a parameter:
luaState.getGlobal(<function name>);
After that we just use the LuaState call method. That method is particularly important because it rst parameter is the number of parameters passed to the function. But how do you pass those parameters? That will be explained by our third tip.
To pass a Java object to Lua we will use the method pushJavaObject and pass it as a parameter of a function. The instruction sentence is:
luaState.pushJavaObject(<object>);
So, with these two features we will be able to pass a Java object as a parameter to Lua function. Use it inside that function and call the function inside the Java scope. That will allow us to create a class that will handle scripts for us as we will see in the next section.

Building a LoadScript class

Now that we have seen how LuaJava works, let’s build a class that will handle all scripts in our game. That class will have only one attribute, our LuaState. The constructor of our class will receive only one parameter that will be the path for our script le. Inside our constructor, we will create the Lua environment with LuaStateFactory.newLuaState and openLibs meth-ods, and execute the Lua le received as a parameter by the constructor.
Our class will have two methods: closeScript and runScriptFunction.closeScript just call luaState.close to terminate the use of Lua environ-ment.
runScriptFunction will get a Lua function received as parameter and call it passing a Java object, also received as parameter, to that function.
We have built a class to handle all our scripts. In the next section we will see how we use that class.

LOADSCRIPT CLASS

import org.keplerproject.luajava.LuaState; import org.keplerproject.luajava.LuaStateFactory;
public class LoadScript {
LuaState luaState;
/**
* Constructor
* @param fileName File name with Lua script.
*/
LoadScript(final String fileName) { this.luaState = LuaStateFactory.newLuaState(); this.luaState.openLibs(); this.luaState.LdoFile(filename);
}
/**
* Ends the use of Lua environment.
*/
void closeScript() {
this.luaState.close();
}
/**
* Call a Lua function inside the Lua script to insert
* data into a Java object passed as parameter
* @param functionName Name of Lua function.
* @param obj A Java object.
*/
void runScriptFunction(String functionName, Object obj) { this.luaState.getGlobal(functionName); this.luaState.pushJavaObject(obj); this.luaState.call(1, 0);
}
}
}
Using Lua script les For a short example of everything that we have saw until now, let’s consider a game with a huge among of di erent monsters. It could be something like Blizzard’s Diablo or World of Warcraft. Our monsters will have four di erent attributes: race, life, attack and defense. Let’s suppose that the game has one hundred di erent kinds of monsters, it one with di erent attribute values.
What would you do to model those monsters? Build a monster class and one particular class for each kind of monster? That would be very bad.
You should consider build the monster class with our previous made script class.
The monster class will receive a new attribute called script. The class constructor will receive the new monster race as a parameter and load its script calling the LoadScript class. After that, we just call the method runScriptFunction calling \create ». Then each race will have a Lua script le that will load the monster instance with the race attributes.
Let’s take a look in the code:

MONSTER CLASS

public class Monster extends Creature { /* Info */
protected String race;
protected int defense;
protected int attack;
protected int life;
/* Script */
private LoadScript script;
public Monster(String race) {
/* Loads Lua script for this race.*/ this.script = new LoadScript(race+ ».lua »); /*Call Lua create function.*/ script.runScriptFunction(« create », this);
}
public String getRace() {
return race;
}
public int getDefense() {
return this.defense;
}
public void setDefense(int defense) { this.defense = defense;
}
public int getLife() {
return this.life;
}
public void setLife(int life) {
this.life = life;
}
public void setAttack(int attack) {
this.attack = attack;
}
public int getAttack() {
return this.attack;
}
}
Analyzing the code above us can see that the rst line is the monster class declaration. Then the next four lines declare the class attributes relative to the information about monsters. Next we have an attribute that is our brand new class LoadScript. After the attribute declarations we can nd the class constructor. As we saw above, the constructor receives a string with the monster’s race and in its rst line call the LoadScript constructor to store in the attribute script the Lua le that stores the values for the attributes of that monster race.

Cours gratuitTélécharger le cours complet

Télécharger aussi :

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *