# Lua modding prototype

**URL:** <https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102>\
**Category:** The Drawing Board\
**Created:** [July 4, 2013, 1:06pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102 "2013-07-04T13:06:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![CIB](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/cib/32/903_2.png) [@CIB](https://discourse.cataclysmdda.org/u/CIB)\
**Post date:** [July 4, 2013, 1:06pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/1 "2013-07-04T13:06:29Z")

</div>

I’ve completed a very barebone prototype that demonstrates how lua could be used to define iuse’s.

[https://github.com/CIB/Cataclysm-DDA/commit/5c2ff27dadf92d05403a1bcbff203c667319ae9c](https://github.com/CIB/Cataclysm-DDA/commit/5c2ff27dadf92d05403a1bcbff203c667319ae9c)

# Example:

data/luamods/core/main.lua

```auto
-- Main mod file that will be executed by the game
-- at initialization time.

function hiccup(player, item, active)
    game.add_msg("You hiccup.")
end

game.register_iuse("HICCUP", hiccup)
```

data/raw/items/tools.json

```auto
  { "id":"lighter",
    "type": "TOOL",
    "symbol": ",",
    "color": "blue",
    "name": "lighter",
    "description": "A lighter must be carried to use various drugs, like cigarettes, or to light things like molotov cocktails. You can also use a lighter to light nearby items on fire.",
    "rarity": 60,
    "price": 35,
    "material": ["plastic","iron"],
    "weight": 0,
    "volume": 0,
    "bashing": 0,
    "cutting": 0,
    "to_hit": 0,
    "max_charges": 100,
    "initial_charges": 100,
    "charges_per_use": 1,
    "turns_per_charge": 0,
    "ammo": "none",
    "revert_to": "null",
    "use_action": "HICCUP"
  },
```

Now the lighter prints a hiccup message.

* * *

This is, as I said, still very bare-bones. There’s no way to access anything but game.add\_msg(). Even the arguments to the iuse functions are currently nil, as I haven’t wrapped the player or item class in a lua object yet.

I’m not using any automatic binding generator. I’ve looked at tolua++, luabind and luna-gen, but all of them seemed too heavy weight to me or otherwise didn’t compile. All the bindings at the moment are hand-grafted, which doesn’t work too badly, though I suppose when it comes to wrapping accessors like [item.name](http://item.name) it will get annoying fast.

On the upside extra dependencies are thus minimal. On unix-like systems, lua 5.1 is easy to install. On other platforms, all that’s needed is downloading the lua source distribution and compiling it(lua is extremely portable, as it’s pure ANSI C).

Feedback welcome.

---

<div class="post-metadata">

**Author:** ![CIB](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/cib/32/903_2.png) [@CIB](https://discourse.cataclysmdda.org/u/CIB)\
**Post date:** [July 12, 2013, 11:17pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/2 "2013-07-12T23:17:20Z")

</div>

[http://www.youtube.com/watch?v=PiMRb4-\_Umw](http://www.youtube.com/watch?v=PiMRb4-_Umw) Some footage.

---

<div class="post-metadata">

**Author:** ![GlyphGryph](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/glyphgryph/32/847_2.png) [@GlyphGryph](https://discourse.cataclysmdda.org/u/GlyphGryph)\
**Post date:** [July 12, 2013, 11:43pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/3 "2013-07-12T23:43:01Z")

</div>

This is very, very cool.

It would allow us to expand the scope of the worldbuilder a bit and allow far more customization if people could define their own item actions, even if it’s only to a limited extent. I will be following this eagerly.

---

<div class="post-metadata">

**Author:** ![CIB](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/cib/32/903_2.png) [@CIB](https://discourse.cataclysmdda.org/u/CIB)\
**Post date:** [July 17, 2013, 3:31pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/4 "2013-07-17T15:31:19Z")

</div>

I’ve wrapped just a bit more player functionality and added a “tick” callback that will be called on every turn.

> local oxygen = 100 -- Will be called every game tick. function game.tick() -- Use up oxygen oxygen = oxygen - 3
> 
> ```
> -- Breathe
> if player.underwater then
> -- Do nothing, can't breathe
> elseif player:has_disease("smoke") then
> if game.rng(1, 10) < 3 then
> oxygen = oxygen + 3
> end
> else
> oxygen = oxygen + 4
> if oxygen > 100 then
> oxygen = 100
> end
> end
> 
> -- If we're low on oxygen alert the player.
> if oxygen < 10 then
> if game.rng(1, 10) == 1 then
> game.add_msg("You're choking to death!")
> end
> player.pain = player.pain + 5
> elseif oxygen < 50 then
> if game.rng(1, 10) == 1 then
> game.add_msg("There's no air, you're going to suffocate!")
> end
> elseif oxygen < 90 then
> if game.rng(1, 10) == 10 then
> game.add_msg("You can hardly breathe!")
> end
> end
> 
> ```
> 
> end

Just to demonstrate that you can do rather complex things with this. Of course, storing the oxygen in a local is bad style, as it won’t be saved. I plan to have a “catajson player::json\_data” variable, which will provide a way to easily add variables in lua to the player that will be saved automatically, while still remaining accessible to C++.

---

<div class="post-metadata">

**Author:** ![Zireael](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/zireael/32/956_2.png) [@Zireael](https://discourse.cataclysmdda.org/u/Zireael)\
**Post date:** [July 18, 2013, 7:35am UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/5 "2013-07-18T07:35:21Z")

</div>

Supporting this wholeheartedly.

---

<div class="post-metadata">

**Author:** ![TheGrifter](https://avatars.discourse-cdn.com/v4/letter/t/278dde/32.png) [@TheGrifter](https://discourse.cataclysmdda.org/u/TheGrifter)\
**Post date:** [July 19, 2013, 6:34pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/6 "2013-07-19T18:34:24Z")

</div>

Awesome. I’ve been waiting for something like this.

---

<div class="post-metadata">

**Author:** ![stone94](https://avatars.discourse-cdn.com/v4/letter/s/f9ae1b/32.png) [@stone94](https://discourse.cataclysmdda.org/u/stone94)\
**Post date:** [July 19, 2013, 6:49pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/7 "2013-07-19T18:49:35Z")

</div>

this is why i wanna learn programming languages C++, LUA, and Ruby are on the top of my list 😛 tho LUA and Ruby are easier to read xD

---

<div class="post-metadata">

**Author:** ![CIB](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/cib/32/903_2.png) [@CIB](https://discourse.cataclysmdda.org/u/CIB)\
**Post date:** [August 14, 2013, 3:44pm UTC](https://discourse.cataclysmdda.org/t/lua-modding-prototype/2102/8 "2013-08-14T15:44:26Z")

</div>

Some more progress lately, and (maybe) something actually useful:

Still all WIP, but definitely getting to the point now where I can add useful things with lua.
