Updated: 2008-06-18Step 1 - Create a Harvest mod description fileFrom Harvest version 1.10 and later, there's a new "mods" folder in your Harvest client data folder. This is located in ~/Library/Application Support/Harvest on Mac, and in the installation folder on Windows. The description file should be placed in this "mods" folder and have the extension ".hmd".
The description file currently has one block called "mod" and two values, the name of the mod (displayed in the game) and the mod's folder. For example:
Code:
mod
{
name = jeb's Awesome Mod;
folder = jebawesome;
author = jeb;
}
Please note that this is not lua code, so the semi-colons are required.
Step 2 - Create your main.lua fileWhen a player selects to load your mod in the game, the game will attempt to open a file called "main.lua" in the folder you have named in your description file (a sub-folder to "mods", such as "harvestClientData/sandbox/jebawesome"). This is a simple text document containing your lua code. It will be executed as soon as the game starts, either from the menu or through loading a saved game that has been saved with your mod activated.
For example, if you want to start the game with lots and lots of credits, you simply create a main.lua with the following line:
Code:
harvest.addCredits(100000)
Step 3 - Creating an update functionMost of the time you want your mod to be updated every frame loop. You do this by adding a "hook" on the game. Hooks are identified by a hook name, in this case "frameUpdate", and are added by calling hook.add(hookName, function). Here's a really simple example:
Code:
local function myTestFunction(updateTime)
end
hook.add("frameUpdate", myTestFunction)
Let's say you want the player to receive credits every 10 seconds. You will then have to keep a counter in your mod, which you place "outside" of the functions, like this:
Code:
local myCounter = 0
local function myTestFunction(updateTime)
myCounter = myCounter + updateTime
if myCounter >= 10 then
myCounter = myCounter - 10
harvest.addCredits(5)
end
end
hook.add("frameUpdate", myTestFunction)
Check the other threads for a list of possible hooks and functions that you can call.
Step 4 - Adding support for save and loadA proper mod will need to store its game variables when the game is saved, and then restore them again when it's loaded. This is managed through hooking "gameSave" and "gameLoad". Here's an example that shows how to save the 'myCounter' variable:
Code:
local function mySaveFunction(variables)
-- insert my variables with some kind of unique name
variables.jebawesome = { c = myCounter }
end
hook.add("gameSave", mySaveFunction)
local function myLoadFunction(variables)
if variables.jebawesome then
myCounter = variables.jebawesome.c
end
end
hook.add("gameLoad", myLoadFunction)
However, this way of saving is quite simplistic and only supports simple arrays of variables. If you have longer arrays or matrices, I recommend you serialize your tables first. Harvest includes chillcode's table serialization on startup, which you can use like this:
Code:
local function mySaveFunction(variables)
variables.jebawesome = table.save(myTable)
end
hook.add("gameSave", mySaveFunction)
local function myLoadFunction(variables)
if variables.jebawesome then
myTable = table.load(variables.jebawesome)
end
end
hook.add("gameLoad", myLoadFunction)
Step 5 - Publishing your modWhen you're pleased with your mod you should also create a mod icon for it. This is optional, of course, but will make your mod look a lot cooler in the mod list!
The mod icon is a 32x32 TGA file with alpha channel (32 bits) that should be placed in your mod folder. The filename must be "favicon.tga" (case sensitive). If the game finds it you will see it in the mod list when you start a Creative Mode game.
The last step is to
zip your mod so that others may use it. Simply use your favourite zip tool to create a package with your .hmd file and the mod's folder. To verify that your mod was zipped correctly, leave the zip file in your mods folder and check the mods list ingame. Your mod should now show up twice (first your .hmd file and then the same version in the zip).