Skip to content

How do I...

Ruin0x11 edited this page Jul 26, 2020 · 5 revisions

This page describes some common scenarios that may come up when writing mods.

...add a new event type?

Put the following in the mod's init.lua, or a file required by it.

data:add {
   _type = "base.event"
   _id = "my_event_type",

   params = {
       { name = "param_1", type = "int", desc = "Description of this parameter." }
   },
    returns = {
       { type = "int?" }
   }
}

This event will have an _id of <mod_id>.my_event_type.

...prompt the player to select an item with a custom filter?

Add a new entry of type elona_sys.inventory_proto with the filtering logic you want.

local inv_filter = {
   _type = "elona_sys.inventory_proto",
   _id = "my_inv_filter",

   sources = { "chara", "equipment", "ground" },
   icon = 17,
   show_money = false,
   query_amount = false,
   text = "ui.inventory_command.identify",

   filter = function(ctxt, item)
      return item.has_charge and (item.charges or 0) > 0
   end,

   on_select = function(ctxt, item, amount)
      return "player_turn_query"
   end
}
data:add(inv_filter)

Then call Input.query_item() to retrieve the item.

local result, canceled = Input.query_item(Chara.player(), "my_mod.my_inv_filter")
if canceled then
   return
end

local item = result.item
Log.info("Item: %s", item:build_name())

...draw a set of tiles or sprites?

First, use Draw.make_chip_batch() to generate a sprite batch. This lets you draw a sequence of sprites in an efficient manner.

local batch = Draw.make_chip_batch("chip")

Here are the different types of sprite batches you can create this way.

  • "tile": Map tiles.
  • "chip": Character, item, and feat sprites.
  • "portrait": Portraits.

Then, add the sprites you want to draw. For a "tile" batch, you pass in the ID of a base.map_tile entry. For "chip", pass in a base.chip ID, and for "portrait", pass in a base.portrait ID.

batch:add("elona.chara_putit", x, y, self.tile_width, self.tile_height)

Finally, during the rendering phase, call :draw().

batch:draw(x, y)

To clear the batch, call :clear(). Be sure to do this first if you want to update the tiles to draw.

batch:clear()