Author Topic: Questions about register_iuse in LUA  (Read 125 times)

Offline CodeSeg

  • Zombie Food
  • *
  • Posts: 12
    • View Profile
Questions about register_iuse in LUA
« on: September 27, 2017, 09:59:58 AM »
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?
Code: [Select]
"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!!

Offline Zhilkin

  • NPC
  • **
  • Posts: 210
    • View Profile
Re: Questions about register_iuse in LUA
« Reply #1 on: September 27, 2017, 01:13:26 PM »
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?
Code: [Select]
"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!!

1. First you need to have some lua function in `preload.lua`:

Code: [Select]
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

2. Second you need to register LUA iuse function in `preload.lua:

Code: [Select]
  game.register_iuse("IUSE_MY_LUA_ITEM_ACTION", iuse_my_lua_item_action)

3. Then you need to map LUA item action in `item_actions.json` (filename can be different).

Code: [Select]
[
  {
      "type" : "item_action",
      "id" : "IUSE_MY_LUA_ITEM_ACTION",
      "name" : "Try LUA item action"
  }
]

4. In the end you need to create new item with this item action:

Code: [Select]
[
  {
    "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"
  }
]

5. Go into game, create new world with your mod, add new item via debug menu and try to activate it.

***

You can check`dda-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
https://github.com/CleverRaven/Cataclysm-DDA/tree/master/doc/sample_mods/lua_manhack_iuse
http://smf.cataclysmdda.com/index.php?topic=14448.msg299239#msg299239
http://smf.cataclysmdda.com/index.php?topic=4893.msg78214#msg78214
« Last Edit: September 27, 2017, 01:25:01 PM by Zhilkin »

Offline CodeSeg

  • Zombie Food
  • *
  • Posts: 12
    • View Profile
Re: Questions about register_iuse in LUA
« Reply #2 on: September 27, 2017, 02:27:36 PM »
Wow, that helps a lot! Thank you very much @Zhilkin ! I'll have a try right away :)