# Dynamically load/require lua-files/tables runtime

**URL:** <https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620>\
**Category:** Questions\
**Created:** [December 13, 2015, 2:52pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620 "2015-12-13T14:52:10Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andreas\_Jirenius](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas_jirenius/32/114_2.png) [@Andreas\_Jirenius](https://forum.defold.com/u/Andreas_Jirenius)\
**Post date:** [December 13, 2015, 2:52pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/1 "2015-12-13T14:52:10Z")

</div>

Using Tiled as a game editor you can export to many different formats including **json** and **lua**.  
Now I would like to use the lua formats (obviously) as it would be nice to just require these files in to the gameproject.  
But I cannot find a way to use require (or load resources for that matter). Is there any way to do this?  
This is what Ive done so far:  
exporting lua tables (level-definitions coming from Tiled) into a folder  
Adding that folder to the game project using _custom\_resources_  
Tried using **require** in different ways but cannot retrieve those lua files.  
Tried using sys.load\_resource(dynamic\_path) and that will give me a string back.. not a table

Instead if using JSON I can create that string mentioned above and use json.decode(str) and then get the table. But hey.. shouldnt it work with lua directly somehow??

best regards

Andreas

---

<div class="post-metadata">

**Author:** ![Andreas\_Jirenius](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas_jirenius/32/114_2.png) [@Andreas\_Jirenius](https://forum.defold.com/u/Andreas_Jirenius)\
**Post date:** [December 13, 2015, 2:54pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/2 "2015-12-13T14:54:37Z")

</div>

Aaaand I’m sorry.

3 minutes after sending I found the sys.load function.

– Dumstrut på

---

<div class="post-metadata">

**Author:** ![Andreas\_Jirenius](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas_jirenius/32/114_2.png) [@Andreas\_Jirenius](https://forum.defold.com/u/Andreas_Jirenius)\
**Post date:** [December 14, 2015, 7:02am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/3 "2015-12-14T07:02:35Z")

</div>

Aand I’m NOT sorry.

Seems like sys.load only works on files created by sys.save()

So problem still remains, how can I load in a resource bundled from start but with dynamic content (so I can retrieve it runtime). Is it possible at the moment?  
Best usercase would obviously be the previous mentioned level-data one.

/Andreas

---

<div class="post-metadata">

**Author:** ![Johan](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan/32/127_2.png) [@Johan](https://forum.defold.com/u/Johan)\
**Post date:** [December 14, 2015, 8:20am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/4 "2015-12-14T08:20:11Z")

</div>

You need to use sys.load\_resource:

> **[API reference (System)](https://defold.com/ref/sys/)**
>
> Defold is a free and open game engine used for development of console, desktop, mobile and web games.

---

<div class="post-metadata">

**Author:** ![Andreas\_Jirenius](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas_jirenius/32/114_2.png) [@Andreas\_Jirenius](https://forum.defold.com/u/Andreas_Jirenius)\
**Post date:** [December 14, 2015, 9:20am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/5 "2015-12-14T09:20:37Z")

</div>

That I did, but got stuck where it only gives me a string and not an actual lua table.  
Parsing a lua table string to get the tabe in the end feels very weird.. then I could go with json.

---

<div class="post-metadata">

**Author:** ![Johan](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/johan/32/127_2.png) [@Johan](https://forum.defold.com/u/Johan)\
**Post date:** [December 14, 2015, 9:21am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/6 "2015-12-14T09:21:47Z")

</div>

Ah sorry, I see. I didn’t read your question properly it seems. I blame monday mornings…

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [December 14, 2015, 8:55pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/7 "2015-12-14T20:55:40Z")

</div>

If you write the lua code into the project structure you should be able to treat it as any other source file and require it. For example:

main/main.script:

```
function init(self)
    require 'main/test'
    pprint(test)	
end

```

and main/test.lua (created directly on filesystem):

```
test = "some_data"

```

Works fine.

---

<div class="post-metadata">

**Author:** ![Andreas\_Jirenius](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/andreas_jirenius/32/114_2.png) [@Andreas\_Jirenius](https://forum.defold.com/u/Andreas_Jirenius)\
**Post date:** [December 15, 2015, 12:40pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/8 "2015-12-15T12:40:27Z")

</div>

Yes but the problem is that I don’t know the require string before hand.  
Case:  
I have a table full of level filenames that I want to read in one by one as a lua table.  
Havent found any way to do that with a luafile. Only json-files.

---

<div class="post-metadata">

**Author:** ![sicher](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/sicher/32/8_2.png) [@sicher](https://forum.defold.com/u/sicher)\
**Post date:** [December 16, 2015, 10:38am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/9 "2015-12-16T10:38:29Z")

</div>

Ok, I understand now. Requires are a bit specially treated in Defold to statically include them in the build/bundling process.

You have to use sys.load\_resource() to get custom content into the game. You _can_ use lua’s loadfile() function, but that will only work locally as long as the loaded file exist in the local filesystem. As soon as you bundle, that changes and everything is packed in archives. sys.load\_resource() is required to load files off the bundled archive. loadstring() returns a function that evaluates the code in the string when the function is called.

```
local code_str = sys.load_resource("/main/test.lua")
local f = assert(loadstring(code_str))
pprint(f()) -- Note that we call f()!

```

main/test.lua:

```
test = "4711"
return test

```

Also remember to add the lua file to custom resources in “game.project”:

```
[project]
title = My project
version = 0.1
custom_resources = main/test.lua
...

```

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [December 16, 2015, 12:59pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/10 "2015-12-16T12:59:56Z")

</div>

Nice! I’ve never thought of using sys.load\_resource() and loadstring() combined. It’s good to know that it works.

---

<div class="post-metadata">

**Author:** ![gianmichele](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gianmichele/32/24028_2.png) [@gianmichele](https://forum.defold.com/u/gianmichele)\
**Post date:** [January 15, 2022, 10:21am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/11 "2022-01-15T10:21:16Z")

</div>

This is super interesting!

Anyone knows if the use of loadstring() is accepted on any platform? I’m not sure why but I seem to recall that on iOS it might not be possible because of app store restrictions/policy

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [January 15, 2022, 11:33am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/12 "2022-01-15T11:33:22Z")

</div>

It’s definitely not something Apple and many others approve of. But where to draw the line between a snippet of code, configuration data, JSON level data etc?

The main concern is where this data is coming from (the developer or use created) and if it can be used to turn the game approved by Apple into something completely different that is against the rules of the store.

---

<div class="post-metadata">

**Author:** ![gianmichele](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gianmichele/32/24028_2.png) [@gianmichele](https://forum.defold.com/u/gianmichele)\
**Post date:** [January 15, 2022, 3:23pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/13 "2022-01-15T15:23:56Z")

</div>

True. Is not used for any modding feature, just to work around the fact that requires cannot be dynamic.

---

<div class="post-metadata">

**Author:** ![Ragetto](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ragetto/32/32785_2.png) [@Ragetto](https://forum.defold.com/u/Ragetto)\
**Post date:** [February 4, 2022, 9:03pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/14 "2022-02-04T21:03:54Z")

</div>

Hi @britzl !

What do you mean by “It’s definitely not something Apple and many others approve of”?

Is is possible that the game would get rejected because of this? (no modding feature or user-generated content in my case, just regular levels)

This workaround/method exactly fits one of my needs (and after a few tests it seems to work fine)… but I’d prefer to avoid bad surprises later 🤔

---

<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:** [February 4, 2022, 9:32pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/15 "2022-02-04T21:32:21Z")

</div>

If you are submitting the game in the way that Apple will be reviewing and approving it then it’s probably not going to be an issue.

Roblox gets around this all… somehow. Obviously all of its “experiences” have code written by young developers all over the world that never was submitted on Roblox app updates.

---

<div class="post-metadata">

**Author:** ![gianmichele](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/gianmichele/32/24028_2.png) [@gianmichele](https://forum.defold.com/u/gianmichele)\
**Post date:** [February 5, 2022, 10:58am UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/16 "2022-02-05T10:58:10Z")

</div>

I believe the loadstring method should be fine with Apple because you’re not loading code resources afterward or from an external source. All your lua code is packaged into the defold binary so it’ll be ok.  
Modding on the other side is not.

---

<div class="post-metadata">

**Author:** ![Ragetto](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/ragetto/32/32785_2.png) [@Ragetto](https://forum.defold.com/u/Ragetto)\
**Post date:** [February 5, 2022, 6:25pm UTC](https://forum.defold.com/t/dynamically-load-require-lua-files-tables-runtime/620/17 "2022-02-05T18:25:04Z")

</div>

Ok great!

Thank you @Pkeod and @gianmichele for your answers
