# Is this possible: Lua and User Input

**URL:** <https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130>\
**Category:** The Lab\
**Created:** [March 13, 2015, 6:28pm UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130 "2015-03-13T18:28:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![deadmoose](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@deadmoose](https://discourse.cataclysmdda.org/u/deadmoose)\
**Post date:** [March 13, 2015, 6:28pm UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130/1 "2015-03-13T18:28:45Z")

</div>

I was wondering if it’s possible to use Lua to control my character in the same I would as a player.

I want to be able to do a simple script where my character would train in mechanics by prying open a door, then closing it if it opens. The script would stop when my character is hungry, thirsty, or tired.

---

<div class="post-metadata">

**Author:** ![Coolthulhu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/coolthulhu/32/2446_2.png) [@Coolthulhu](https://discourse.cataclysmdda.org/u/Coolthulhu)\
**Post date:** [March 13, 2015, 6:39pm UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130/2 "2015-03-13T18:39:37Z")

</div>

At the moment no.  
Lua support is currently limited to a rather narrow set of functions.

---

<div class="post-metadata">

**Author:** ![deadmoose](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@deadmoose](https://discourse.cataclysmdda.org/u/deadmoose)\
**Post date:** [March 13, 2015, 9:45pm UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130/3 "2015-03-13T21:45:57Z")

</div>

Would it be difficult for me to add?

I’m not all that familiar with the codebase, but know some C++. I wouldn’t know where to even look, though. I just need to simulate user input.

---

<div class="post-metadata">

**Author:** ![Coolthulhu](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/coolthulhu/32/2446_2.png) [@Coolthulhu](https://discourse.cataclysmdda.org/u/Coolthulhu)\
**Post date:** [March 13, 2015, 11:51pm UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130/4 "2015-03-13T23:51:17Z")

</div>

[quote=“deadmoose, post:3, topic:9130”]Would it be difficult for me to add?

I’m not all that familiar with the codebase, but know some C++. I wouldn’t know where to even look, though. I just need to simulate user input.[/quote]

May be doable.

Input is handled in a big loop in game.cpp in function game::handle\_action(). At the top of the function (the if-else block), you could add own function that would check if Lua “wants” to force some input.  
You would probably need to create some object that would hold the state of this Lua-driven input. This could possibly be just a single bool variable, say “lua\_input” in game class.  
If this check returns true, then instead of asking player for input, you’d run a script to generate it instead.

Since actions are strings, it should be rather easy to pass them from Lua to C++ - just have the function run a Lua script that returns a string, then return that string back to handle\_action().

Most (all?) Lua code is in catalua.cpp. Probably the best example is the use\_function::call function there, because it is invoked from C++, calls Lua scripts and gets a return value from them.

The biggest problem here is understanding how C++ and Lua connect, but catalua.cpp has some good examples. It has loading lua script from file, setting up environment for the script and finally calling it.

You may need to define some extra wrapper functions, such as furn\_at(x,y) (to open/close doors). Those are in src/lua/catalua.cpp. They have rather simple (copy+paste) structure.

tl;dr You want to make game::handle\_action() get a string from a script and the script to be able to “see” furniture.

---

<div class="post-metadata">

**Author:** ![deadmoose](https://avatars.discourse-cdn.com/v4/letter/d/5f8ce5/32.png) [@deadmoose](https://discourse.cataclysmdda.org/u/deadmoose)\
**Post date:** [March 14, 2015, 2:05am UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130/5 "2015-03-14T02:05:10Z")

</div>

Thank you for the in-depth reply. I have my work cut out for me, but this is as much as I could of asked for to get started.

---

<div class="post-metadata">

**Author:** ![kevin.granade](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.cataclysmdda.org/kevin.granade/32/1316_2.png) [@kevin.granade](https://discourse.cataclysmdda.org/u/kevin.granade)\
**Post date:** [March 15, 2015, 7:28am UTC](https://discourse.cataclysmdda.org/t/is-this-possible-lua-and-user-input/9130/6 "2015-03-15T07:28:57Z")

</div>

Just FYI, for basic things that only require global keybinding inputs like walking around and melee combat, this would do it for you, but for any menu interaction you’re going to have trouble (by which I mean you’d have to add hooks into the code all over the place).  
Depending on what you’re doing a much bigger problem is getting output from the game, any kind of AI is going to require extensive work to make game state visible to your code. If you’re looking at plugging in a macro system or something similar though, that wouldn’t be a problem.
