Modding: Difference between revisions

From APICO Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
Line 53: Line 53:


<h1>API Reference</h1>
<h1>API Reference</h1>

<h2>Get Methods</h2>

<h3>api_get_player_position()</h3>
<p>This returns the current player x,y position, relative to 0,0 in the top left of the world</p>

<div class="aw-code">{{Method|method=api_get_player_position}}{{Return|datatype=Table|desc=returns a Table containing an X and a Y value}}</div>

<p>Example LUA code to get the current position:</p>
<div class="aw-code"><span class="ca">position</span> = <span class="cf">api_get_player_position</span>()<br/><span class="ca">x_position</span> = tostring(<span class="ca">position</span>[<span class="cs">"x"</span>])<br/><span class="cf">api_create_log</span>(<span class="cs">"player_position"</span>, <span class="cs">"Player is at "</span> .. <span class="ca">x_position</span>)</div>


<h2>Set Methods</h2>
<h2>Set Methods</h2>

Revision as of 18:33, 9 August 2021

This is a WIP page that I'm using to document the game's modding API as I go

Modder application form: https://forms.gle/gaqa4S6jUcoxePrA6

Mods for APICO are written in LUA. All standard LUA libraries are available except io and os for security reasons. If there is something you specifically need let us know and we can look into a way of exposing it through the API! The LUA interface we use into GameMaker is Apollo.

When creating a mod you will need to decide on a unique mod ID, which is a lowercase name with no spaces (underscores are accepted) - this will be used as not only your mod directory folder within the game but also for identifying your game in various debugging (so we can see which one of you keeps breaking stuff :P).

In all the documentation examples, we use sample_mod which is also the name of the sample mod you can download to test out the various API methods.

Current Mods

sample_mod by @ellraiser

Glossary

Dictionary

In the game every single item, object, machine, NPC, or button has a definition in the Dictionary. If there is not a definition the game will not recognise what you are trying to reference.

You can add definitions through the api_define_*() methods.


Modding Console

The Modding Console is a debugging tool available by pressing ., and it shows all messages from both the Modding API itself as well as any logs or errors from individual mods


In-Game Console

When enabled, the In-Game Console is an inline command runner you can use while playing the game by typing / followed by a command.

For example to spawn in 5 logs you'd use /gimme log 5


Getting Started

To get started you just need your favourite IDE! You don't need to compile your LUA code in advance, the game does this when it loads your mod, so outside of any LUA extensions you might find useful you don't need any other resources

When modding is properly live, a player will be able to see all mods from the "Modding" section thats accessible from the home screen. They'll be able to pick the mods they want and download them, which will save the mod into the /mods directory, as well as update their settings.json file. For now you'll need to do this manually.

For example if your mod is called "sample_mod", you'll need to set the "downloaded_mods" key inside settings.json as ["sample_mod"], as well as add your mod in a folder called "sample_mod". Your directory will look like this:

<img/>

If done correctly, when you go to the "Modding" section you'll see your mod in the right-hand section, just inactive.Pressing the "tick" button will activate the mod, and now any time you start a game you'll see a "Loading mods..." screen


Mod Structure

For mods to work they need to follow a basic structure so that APICO can register and load mods correctly, as well as handle any errors that might occur when trying to compile your code.

Every mod needs to start with a mod.lua file at the root of your mod folder. This is the main file that initially gets loaded - a basic mod file would look something like this:

mod_id = "sample_mod"

function register()
  return mod_id
end

function init(working_directory)
  api_create_log("init", "Hello world!")
  return "Success"
end

function clock()
  api_create_log("clock", "1 second has passed!")
  return "Success"
end

When the game loads, it'll retrieve the player's mod settings to get a list of all active mods. It will then try and init these mods through the sc_init_mod() method that you'll see in the Modding Console.

This method will first try and call a register() method in your mod, which will check for duplicate names, and will then call an init() method.

Your init() method is a place for you to setup all mod code you need at the start, and needs to return either Success or an error message - this lets the game know if your mod loaded correctly.

If there are any errors while trying to define an item you'll see one of the following codes logged in the Modding Console

Error: Mod Failed To Init
This means your mods code could not be compiled correctly! You'll also see related LUA errors logged after this error message that should help narrow down the issue
Error: Duplicate Mod Registered
This means the mod id you have provided is already registered by the game, try a different mod id!
Error: Mod Failed To Load
This is called if your init() method does not return "Success", allowing you to debug your mods setup


General Notes

The game completely trusts the save file when it loads - because of this your players save file will load with any mod content in the save (custom items, objects, etc). If it can't then find these items in the dictionary you'll end up with a bunch of pink cubes - basically undefined instances.

API Reference

Get Methods

api_get_player_position()

This returns the current player x,y position, relative to 0,0 in the top left of the world

@method api_get_player_position()
@return {Table} - returns a Table containing an X and a Y value

Example LUA code to get the current position:

position = api_get_player_position()
x_position = tostring(position["x"])
api_create_log("player_position", "Player is at " .. x_position)

Set Methods

api_set_devmode()

This turns on Dev Mode, which allows you to run commands through the In-Game Console

@method api_set_devmode(devmode)
@parameter devmode {Boolean} - whether to set devmode to truefalse
@return {Nil}

Example LUA code to turn on dev mode:

api_enable_dev(true)

There are no associated errors with this method

Create Methods

api_create_log()

Allows you to log messages to the Modding Console

@method api_create_log(func, message)
@parameter func {String} - the name of the method calling this log, shown in the console
@parameter message {String} - the message to show in the console
@return {Nil}

Example LUA code to log "Hello World!" to the Modding Console:

api_log("my_method", "Hello World!")

There are no associated errors with this method

api_create_item()

Allows you to create an item at a specific point. The item needs to already exist in the Dictionary, so if you are trying to spawn in an item from your mod be sure to use api_define_item() first.

@method api_create_item(item_id, amount, x, y)
@parameter item_id {String} - the item_id of the item to spawn, must be in the Dictionary
@parameter amount {Integer} - the amount of the item to spawn
@parameter x {Integer} - the x position to spawn the item
@parameter y {Integer} - the y position to spawn the item
@return {Integer} - either returns the created item instance ID or Nil if it fails

Example LUA code to spawn 5 planks at a specific point:

api_create_item("planks1", 5, 200, 300)

If this method fails you'll see the following errors in the Modding Console:

api_create_object()

Allows you to create an object instance at a specific point. The object needs to already exist in the Dictionary, so if you are trying to spawn in an object from your mod be sure to use api_define_object() first.

@method api_create_object(object_id, x, y)
@parameter object_id {String} - the object_id/oid of the item to spawn, must be in the Dictionary
@parameter x {Integer} - the x position to spawn the item
@parameter y {Integer} - the y position to spawn the item
@return {Integer} - either returns the created object instance ID or Nil if it fails

Example LUA code to spawn a tree at a specific point:

api_create_object("tree", 50, 50)

If this method fails you'll see the following errors in the Modding Console:

Error: Failed To Create Object
This means the object could not be created, most likely an incorrect object_id, or badly defined object definition for the given object_id

Define Methods

api_define_item()

This allows you to define a new Item for the game and add it to the Dictionary

This is needed before you can spawn the item in, add it to recipes, or use it with machines.

The sprite you use needs to be a 60x16px image within your mods resources, made up of 4 frames (normal, highlighted, undiscovered, undiscovered + highlighted). The sprite will be loaded virtually and used automatically when drawing your item.

Once you've created an item with this method you'll be able to access your sprite reference with TODO if you need it during a custom draw call. You'll also be able to directly get the definition from the dictionary with TODO

@method api_define_item(definition, sprite_image)
@parameter mod_name {String} - the name of your mod, the same as your mod folder, i.e. "sample_mod"
@parameter definition {Table} - the item definition with the following keys:

id {String} - a unique identifier for the item. This will be prepended with "mod_name_"
name {String} - a name for the item that will be shown in tooltips
category {String} - a category for the item that will be shown in tooltips
tooltip {String} - a tooltip description for the item that will be shown in tooltips
shop_buy {Decimal} - a buying price for the item if this item was to be bought
shop_sell {Decimal} - a selling price for item if this item was to be sold
shop_key {Boolean} - whether this is a key item which means it can't be sold
machines {Array} - a list of machine this item can be used in i.e. ["workbench", "sawbench"]
durability {Integer} - if this item is a tool that gets used up you can specify a total durability here
singular {Boolean} - if true then this item will not be able to be stacked
placeable {Boolean} - whether this item can be placed - if specified you must also give an "obj" key
place_grass {Boolean} - if placeable, this specifies the item can only be placed on grass only
place_water {Boolean} - if placeable, this specifies the item can be placed on shallow water
place_deep {Boolean} - if placeable, this specifies the item can be placed on deep water
obj {String} - if placeable, this specifies the object that will be created when the item is placed. If this is not a different object id, you should be making an Object instead of an Item
honeycore {Boolean} - specifies whether this item will buy/sell for Honeycore instead of Rubees

@return {String} - either your new item's id (i.e. "sample_mod_item_name") or nil if there was an error

Example LUA code to define an axe item with an ID of "sample_mod_super_axe":

item = {}
item["id"] = "super_axe"
item["category"] = "Tools"
item["tooltip"] = "This is my first mod tool!"
item["shop_buy"] = 0.0
item["shop_sell"] = 0.0
item["shop_key"] = false
item["singular"] = true
item["durability"] = 9999
item["name"] = "Super Axe"
def_item = api_define_item("sample_mod", item, "sample_item_sprite.png")

If this method fails you'll see the following errors in the Modding Console:

Error: Missing Required Key (KEY)
This means one of the required keys above was missing from your definition parameter
Error: ID Already Defined
This means the id key provided is already in the Dictionary, either it's already used or you defined the item twice.
Error: Failed To Map Keys
This means one of your keys couldn't be mapped properly - possible a null value.
Error: Failed To Add Sprite
This means your sprite couldn't be added, either an incorrect format or file not found

Hook Methods

register()

This is a required hook that your main mod file needs to have in order for your mod to be loaded correctly. This hook is called when the game tries to first "register" your mod to avoid mod name clashes, and the hook needs to return the unique name of your mod (this will be the "approved" name from TNgineers) i.e. sample_mod

@method register()
@return {String} - the hook should return your unique mod name, i.e. "sample_mod"

Example LUA code that your main mod file needs to include:

function register()
  return "sample_mod"
end

If this method fails you'll see the following errors in the Modding Console:

Error: Mod Failed To Register
This means either your mod does not have a register() hook, or the code inside the register method failed to compile correctly
Error: Duplicate Mod Registered
This means somehow the game has managed to register your mod twice, you probably won't see this error ever.

init()

This is a required hook that your main mod file needs in order for your mod to be loaded correctly. This hook is what is called for the first time by the game to your mod, allowing you to setup all the things your mod needs to run! You should return a "Success" message to this mod if everything goes well so the game knows if things went A-OK from your end.

You can (and should) put all the setup code you need here - you can call out to methods in other functions but this is your one and only chance to define all the stuff you need at runtime to get everything the mod needs ready. After this you'll only be able to use the other hooks like clock() to directly call mod code.

@method init()
@return {String} - this should return either "Success" if things went as expected or an error message. Any error will be shown in the Modding Console

Example LUA code that your main mod file needs to include:

function init()
  check = 1 + 1
  if check != 2 return "Error: Maths is made up!"
  return "Success"
end

If this method fails you'll see the following errors in the Modding Console:

Error: Mod Failed To Init
this means either your mod does not have a init() hook, or the code inside the init method failed to compile correctly
Error: Mod Failed To Load
this is shown when you do not return "Success" to the init() hook, and is shown along with whatever error message you returned.