Allow Game Modding With LuaJ

Lua Script Code

Sharing is caring!

Luaj is a lua interpreter based on the 5.2.x version of lua which allows you to execute lua from inside java, compile lua to lua bytecode and compile lua to java bytecode. It is used in many games to allows players to create and add mods to a game. A list of some games can be found here.

 

What’s the point in adding modding to a game? well I’ll tell you. The amount of people out there who make mods is huge and they all have ideas and skills which you may not have. Allowing modding means your game can be improved by the players so they get to add the things they want in the game and all you need to do is allow them access. Modding will allow your game to be more entertaining for a longer period as mods can always be added even when development of the game is finished.

 

So how would you allow modding? The first thing you should do is decide on what you will use to allow your users to mod the game. I will be using Lua with LuaJ but you could allow them to use anything from JavaScript to XML. You could even create your own scripting language designed to work with your game.

 

To start using LuaJ with your java project you need to include it in your project. I am using gradle but you could add it manually if you’re not using gradle. Below is an excerpt from the gradle.build file I use.

With LuaJ in the build file I refreshed dependencies and built the project. Now I have LuaJ in my project I can start using it. I wanted a system that would allow me to use more than one scripting language later on so I made a script interface for the scripts, a class for Lua Scripts, an execute script for registering functions and a script manager.

 

Below in the Script interface. This sets the methods should be defined in a script.

The LuaScript class defines all the methods for executing Lua functions and storing the return values. LibGDX code is commented out here but can replace the current code to use within a LibGDX project.

The Execute Script which allows Lua scripts to call other Lua functions.

Finally the ScriptManager which is used to call Lua functions in the scripts. Again here commented out code is the LibGDX version.

How to use these scripts.

  1. Create a Source folder for your modding scripts. I called mine Mods 🙂
  2. Add your lua scripts to your Mods folder
  3. Use example code below to create your own Lua function calls.

 

 

 

 

 

 

 

 

Further Reading:

Getting Started in LuaJ

Adapted From Code by: Riho Peterson 2014

Sharing is caring!

One Reply to “Allow Game Modding With LuaJ”

  1. then how to make the game when we script the game?

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.