# Implementing in-game Lua scripting

**URL:** <https://forum.defold.com/t/implementing-in-game-lua-scripting/68311>\
**Category:** Questions\
**Created:** [April 28, 2021, 1:00am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311 "2021-04-28T01:00:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FluxCapacitor](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/fluxcapacitor/32/23445_2.png) [@FluxCapacitor](https://forum.defold.com/u/FluxCapacitor)\
**Post date:** [April 28, 2021, 1:00am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/1 "2021-04-28T01:00:50Z")

</div>

I am wondering, how would you implement in-game scripting for a game made in Defold? Since Defold is in Lua already I assume it should be easy?

My first thought was that my Defold project could ‘require’ a .lua script file that the user would provide in some outside directory location. But, it seems a defold project can only take scripts from within the project directory structure right? And even if you could do that, would building the project to windows/mac/linux break this?

I am thinking I would have created functions in my player.script that would provide game state info (e.g. distance to target, etc). But what I want is to expose those functions to the user via some kind of api - so that the user can add functionality and tell the player when to do things like fire weapons via scripting.

Any suggestions on how to do this? It seems like it should be easy, but my rudimentary efforts have failed.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [April 28, 2021, 1:22am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/2 "2021-04-28T01:22:50Z")

</div>

You can dynamically run Lua code with [loadstring()](https://defold.com/ref/alpha/base/#loadstring:string-%5Bchunkname%5D) but it’s very dangerous unless you first process the scripts to remove nasty code.

---

<div class="post-metadata">

**Author:** ![FluxCapacitor](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/fluxcapacitor/32/23445_2.png) [@FluxCapacitor](https://forum.defold.com/u/FluxCapacitor)\
**Post date:** [April 28, 2021, 2:02am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/3 "2021-04-28T02:02:48Z")

</div>

hmm, with some more searching I found this: [GitHub - kikito/lua-sandbox: A lua sandbox for executing non-trusted code · GitHub](https://github.com/kikito/sandbox.lua)

(a lua library for running untrusted lua code) So maybe this library with loadstring() could work.

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [April 28, 2021, 2:34am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/4 "2021-04-28T02:34:47Z")

</div>

That does seem like a possible option for you. Please try it and let us know here how it works for you.

---

<div class="post-metadata">

**Author:** ![FluxCapacitor](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/fluxcapacitor/32/23445_2.png) [@FluxCapacitor](https://forum.defold.com/u/FluxCapacitor)\
**Post date:** [April 28, 2021, 3:47am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/5 "2021-04-28T03:47:01Z")

</div>

ok, so I did get loadstring() to work… I use assert(loadstring(‘./test.lua’)) to execute a command in my external file. (e.g. print(‘hello world’)).

But let’s say, I have a player.script on my player GO. And in my player.script, I have defined:  
local currently\_contacting\_ground = true

So let’s say in my test.lua script I want to have a command for my player to jump, if currently\_contacting\_ground is true.

But, the test.lua script has no access to the currently\_contacting\_ground… So I haven’t really achieved my goal - I haven’t been able to expose anything about the game state to the external script. Any ideas about that?

---

<div class="post-metadata">

**Author:** ![Pkeod](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/pkeod/32/606_2.png) [@Pkeod](https://forum.defold.com/u/Pkeod)\
**Post date:** [April 28, 2021, 3:56am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/6 "2021-04-28T03:56:09Z")

</div>

You can use messaging to send messages to your player script from the executed script somehow. As long as the script which is doing the loadstring() has a Lua module required in it the executed code should be able to use it too I think.

Broadcast is what I usually use for publish / subscribe so you don’t have to worry about exact script paths.

> <https://github.com/britzl/ludobits/blob/master/ludobits/m/broadcast.md>

---

<div class="post-metadata">

**Author:** ![TheKing0x9](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/theking0x9/32/13588_2.png) [@TheKing0x9](https://forum.defold.com/u/TheKing0x9)\
**Post date:** [April 28, 2021, 9:29am UTC](https://forum.defold.com/t/implementing-in-game-lua-scripting/68311/7 "2021-04-28T09:29:51Z")

</div>

You can also have a look at Defold Codepad.

[https://github.com/defold/codepad](https://github.com/defold/codepad)
