Load lua code at runtime

I am trying to find out how can I enable loading Lua scripts in a sandbox environment that I would create in my App.
I have seen some previous discussions saying that this would not be allowed by Android and iOS. I don’t know how some of the apps like:
Lua-IDE and
Luadroid

make that possible?
Can we make a plugin for solar2d to add that functionality since solar2d api does not have load()?

It’s possible, I thought about at one point making a host for this but I have been super busy :slight_smile:

I made a plugin for this

1 Like

Thanks a lot for sharing this! I see you use loadstring, I did not know that this function is available. It is not documented in the API.

This is great, thanks Scott. When you say ‘making a host’ what do you mean? It would be amazing to have the equivalent of Live Builds for production with the lua code and images etc stored on a server and synced to each client when it changes. Reading the latest guidelines, it looks to me like Apple allow this now, provided you don’t use it to make your app do something that goes against the published description.

Would something like “Patcher” work for you?

Ah thanks for replying @bgmadclown - will check that out. That’s a separate approach to the one @Scott_Harrison posted?

Thanks for mentioning it. Yes that will work. I did not know if loadstring works. Seems like it does. If it works then I think that will solve my purpose.

I haven’t used any of them but I believe @Scott_Harrison 's solution is mostly for running code in runtime and Patcher is aimed at updating the application, skipping the review process. I don’t want to mess it up for you so I suggest you review both libraries.

I’m late replying, but I believe patcher still works. I tested it on Windows and it works fine. You’ll want to test it on OS X, Android, and iOS yourself.

Here is a link to an example app using it. Read main.lua to puzzle out what is happening in the example:

https://github.com/roaminggamer/RG_FreeStuff/raw/refs/heads/master/myPluginSamples/patcher/patcher-example.zip

Also, be aware you don’t have to use it as a plugin. You can get the source for patcher.lua from my RG_FreeStuff repository on github.com here: https://github.com/roaminggamer/RG_FreeStuff/raw/refs/heads/master/Products/patcher/patcher.lua

Just remove the plugin stuff from build.settings:

	plugins =
    {
        ["plugin.patcher"] = { publisherId = "com.roaminggamer" },
    },  

, then include the patcher.lua file in your project and require it directly.

Note: You will want to use patcher’s version of require all the time. Again, see main.lua of the example to puzzle out how it works.

As a starter try this sequence of interactions when you run the example app.

  1. Click the top 3 sliders (leave caching off).
  2. Click the ‘Load Module’ button.
  3. Click the ‘Test Module’ button. This will use the original (local) copy of the module and its functions.
  4. Click ‘Download Patch File v3’
  5. Click ‘Test Module’ button again. Notice it still runs the original copy of the script file.
  6. Click ‘Load Module’ then click ‘Test Module’. Notice it now uses the v3 version of the myModule file.

I think you can puzzle out the rest by reading the example and playing with the app.

RG_FreeStuff++:

6 Likes

Thanks so much for replies! Will experiment!

Tom