Modding Help Is loadstring() available in the Lua API?

Discussion in 'Starbound Modding' started by OBAN, May 26, 2018.

  1. OBAN

    OBAN Scruffy Nerf-Herder

    I want to store a function in a table as part of a library function. I can of course not do this directly as the internal engine can only handle JSON sterilisable values in tables, so I am attempting to use string.dump() to store it (which works), and then later call it with loadstring(), however, this fails:

    [20:41:30.345] [Error] Exception while invoking lua message handler for message 'dpxRequest'. (LuaException) Error code 2, [string "/scripts/dpxObject.lua"]:55: attempt to call a nil value (global 'loadstring')

    Is this function not implemented, or am I doing something wrong? Is there a work around ( the lua manual does recommend this only as a last resort because of how taxing it is)?
     
    Last edited: May 26, 2018
  2. Errors4l

    Errors4l Spaceman Spiff

    There's a property in the starbound config "safeScripts" (or unsafeScripts, I forgot). This'll give you access to a bunch of functions deemed unsafe. You want to stay away from this as making users enable unsafe scripts opens them up to harmful mods messing with their machine (i.e. os.remove).
     
  3. bk3k

    bk3k Oxygen Tank

    I'm missing something on why you need this. You want to load Lua code from a JSON source? I can't see any benefit when you can just make more .lua files and use requires("/path/someStuff.lua"). Note that you don't NEED to do that right at the top as is customary. You can do this anytime from within your code such as after conditional statements.

    And your script can decide what it needs to load based off of data that you can store as JSON such as an object's properties. That could be done in any number of ways from simple boolean logic, integers representing different modes, storing function names, storing the path to your script as a string, etc. I can't think of any scenarios where loadstring() is necessary nor even better than other options.

    This is lua 5.3 and you can store functions in tables as that's very basic functionality, but I guess that's not what you mean? Perhaps you are referring to functions like object.setConfigParameter() which will only accept a JSON value. If so, that's because you're adding to the object's JSON data itself so only JSON-convertible data would be valid. Consider that JSON data entirely separate from the lua _ENV, although the engine can retrieve it for you via config.getParameter().
     
    Errors4l likes this.

Share This Page