# Questions about register\_iuse in LUA

**URL:** <https://discourse.cataclysmdda.org/t/questions-about-register-iuse-in-lua/14293>\
**Category:** The Toolbox\
**Created:** [September 27, 2017, 8:59am UTC](https://discourse.cataclysmdda.org/t/questions-about-register-iuse-in-lua/14293 "2017-09-27T08:59:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeSeg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/codeseg/32/7247_2.png) [@CodeSeg](https://discourse.cataclysmdda.org/u/CodeSeg)\
**Post date:** [September 27, 2017, 8:59am UTC](https://discourse.cataclysmdda.org/t/questions-about-register-iuse-in-lua/14293/1 "2017-09-27T08:59:58Z")

</div>

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?

```auto
"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!!

---

<div class="post-metadata">

**Author:** ![Zhilkin](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zhilkin/32/3749_2.png) [@Zhilkin](https://discourse.cataclysmdda.org/u/Zhilkin)\
**Post date:** [September 27, 2017, 12:13pm UTC](https://discourse.cataclysmdda.org/t/questions-about-register-iuse-in-lua/14293/2 "2017-09-27T12:13:26Z")

</div>

[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?

```auto
"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`:

```auto
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:

```auto
  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).

```auto
[
  {
      "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:

```auto
[
  {
    "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 check`dda-lua-items` mod as an example. You can find the mod here [http://smf.cataclysmdda.com/index.php?topic=14861.0](http://smf.cataclysmdda.com/index.php?topic=14861.0) or here [https://github.com/ZhilkinSerg/Cataclysm-DDA-LUA/tree/master/dda-lua-items](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=14231.msg300388#msg300388)

> **[CleverRaven/Cataclysm-DDA](https://github.com/CleverRaven/Cataclysm-DDA/tree/master/doc/sample_mods/lua_manhack_iuse)**
>
> Cataclysm-DDA - Cataclysm - Dark Days Ahead. A turn-based survival game set in a post-apocalyptic world.

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

---

<div class="post-metadata">

**Author:** ![CodeSeg](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/codeseg/32/7247_2.png) [@CodeSeg](https://discourse.cataclysmdda.org/u/CodeSeg)\
**Post date:** [September 27, 2017, 1:27pm UTC](https://discourse.cataclysmdda.org/t/questions-about-register-iuse-in-lua/14293/3 "2017-09-27T13:27:36Z")

</div>

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