Questions about register_iuse in LUA

I’m very optimistic about LUA scripts in mods. So I’m trying to use LUA to create a new mod. I’m starting with “register_iuse”, but I have some problem figuring out the details.
If I defined a iuse function in Lua, then what will the key/value be in json data? For example, we can implement an iuse function in Lua, which has the exact same effect with “transform” use action, right?

"use_action": {
         "menu_text": "Expand",
         "type": "transform",
         "target": "baton-extended",
         "msg": "You snap open your baton."
},

I couldn’t find any existing code using this method. And I have tried to figure it out on my own, but could you please show some guidance about it? A little hint about which codes I should read is also appreciated. Thanks a lot!!

[quote=“CodeSeg, post:1, topic:14293”]I’m very optimistic about LUA scripts in mods. So I’m trying to use LUA to create a new mod. I’m starting with “register_iuse”, but I have some problem figuring out the details.
If I defined a iuse function in Lua, then what will the key/value be in json data? For example, we can implement an iuse function in Lua, which has the exact same effect with “transform” use action, right?

"use_action": {
         "menu_text": "Expand",
         "type": "transform",
         "target": "baton-extended",
         "msg": "You snap open your baton."
},

I couldn’t find any existing code using this method. And I have tried to figure it out on my own, but could you please show some guidance about it? A little hint about which codes I should read is also appreciated. Thanks a lot!![/quote]

  1. First you need to have some lua function in preload.lua:
function iuse_my_lua_item_action(item, active)

  local item_name = tostring(item:display_name())

  game.add_msg("You have used item: ".item_name)
  
end
  1. Second you need to register LUA iuse function in `preload.lua:
  game.register_iuse("IUSE_MY_LUA_ITEM_ACTION", iuse_my_lua_item_action)
  1. Then you need to map LUA item action in item_actions.json (filename can be different).
[
  {
      "type" : "item_action",
      "id" : "IUSE_MY_LUA_ITEM_ACTION",
      "name" : "Try LUA item action"
  }
]
  1. In the end you need to create new item with this item action:
[
  {
    "id": "item_with_lua_action",
    "type": "TOOL",
    "name": "item with lua action",
    "description": "Item with custom <color_ltgreen>`LUA!`</color> item action.",
    "weight": 1,
    "volume": 1,
    "price": 10000,
    "bashing": 1,
    "material": [ "plastic" ],
    "symbol": "!",
    "color": "red",
    "use_action": "IUSE_MY_LUA_ITEM_ACTION"
  }
]
  1. Go into game, create new world with your mod, add new item via debug menu and try to activate it.

You can checkdda-lua-items mod as an example. You can find the mod here http://smf.cataclysmdda.com/index.php?topic=14861.0 or here https://github.com/ZhilkinSerg/Cataclysm-DDA-LUA/tree/master/dda-lua-items.

Even more examples:

http://smf.cataclysmdda.com/index.php?topic=14231.msg300388#msg300388


http://smf.cataclysmdda.com/index.php?topic=14448.msg299239#msg299239
http://smf.cataclysmdda.com/index.php?topic=4893.msg78214#msg78214

Wow, that helps a lot! Thank you very much @Zhilkin ! I’ll have a try right away :slight_smile: