Dynamically load/require lua-files/tables runtime

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

Aaaand I’m sorry.

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

– Dumstrut på

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

You need to use sys.load_resource:

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.

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

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.

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.

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
...
6 Likes

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

2 Likes

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

1 Like

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.

2 Likes

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

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 :thinking:

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.

1 Like

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.

2 Likes

Ok great!

Thank you @Pkeod and @gianmichele for your answers