Author Topic: Run From File Script (31 char limit workaround for script-eval) - Fixed  (Read 3899 times)

0 Members and 1 Guest are viewing this topic.

Offline FelixJim

  • 28
  • User 11092
Run From File Script (31 char limit workaround for script-eval) - Fixed
At the moment, you can only pass 31 characters through script-eval in FRED. Although I believe something to improve this situation is in the talks stage, I wanted something right now, even if it was quick, dirty, and a bit (very) hackish. And I thought I'd share it! Because I'm nice like that. It's not exactly the most complex of scripts anyway.


Anyway, this basically works by storing the string you wanted to pass through script-eval in a separate file (or script), and then from FRED you merely call the index of this string in the function and send it straight to the lua interpreter. This means that your original lua snippet bypasses the SEXP system entirely; and so a horrendous long function name and multiple lengthy arguments can be referenced by a short function where i is a single number, the index. The only limitation is that the function name is less than 32 characters (but it would have to be anyway). There are usage notes within the script itself as comments for the specifics, and example files included in the download which should make everything clear.

The lua snippets can be stored either in a text file in data\scripts (cannot be packed into a VP), or in the script itself (can be packed into a VP).

Important to note is that this script essentially executes user-defined lua code, and although there are a couple of built-in checks to filter out some nonsense code, it would be prohibitively difficult to check for all possible issues. If you pass bad data to the script you can crash FSO.

Here's a quick look to save anyone interested the trouble of downloading:

Code: [Select]
#Conditional Hooks

$Application: FS2_Open
$On Game Init:

-- FelixJim's Run From File script

-- This script is a workaround for the (current) limit of 31 characters when calling script-eval from FRED. Instead of
-- calling the lua code you want to directly, you place the snippet in a text file (filename specified below) in
-- data/scripts (SHOULD NOT BE PACKED INTO A VP!). One piece of code (usually a function call) should be placed
-- on each line. Then in FRED you call script-eval with the argument " number)" (without quotation marks),
-- where 'line number' is the line number of the text file where the code you want to run resides. For example, if you
-- wish to call a function "thisfunctionisaverylongfunction()", then write this on line 1 of the text file (without
-- quotation marks) and then call script-eval in FRED with argument "".
-- Note that function names must still be 31 characters or less, but function arguments can be of any size.
-- This script should be compatible with any other out there, assuming they do not use a variable called "rff".
-- The script will catch invalid lua syntax and attempts to call lines which don't exist and throw debug errors but
-- other problems, such as invalid function names, will not be caught and may cause FSO to crash.
-- Instead of using the text file, you can edit this script between the START and END notes to set up the code snippets,
-- with the advantage that it can be packed into a VP; and the disadvantage that you have to be more careful of syntax.
-- You should only be editing within the START and END notes

rff = {} --Table storing all variables and functions for this script
rff.commands = {} --Table used to store the file, one line per table element

-- Each entry should be between "rff.aux = function()" and "end", be on its own line and be in the format:
-- rff.commands[line number] = "lua code snippet"

--For example (these would have to have the "--" removed from the beginning; this implies a lua comment):
--rff.commands[1] = "thisfunctionisaverylongfunction()"
--rff.commands[2] = "thatfunctionisaverylongfunction('ThisShipHasAVeryLongNameIndeed')"
--rff.commands[3] = "myfunctionisaverylongfunction('This string is a very long string and has 56 characters.')"

rff.filename = "runfromfile.txt" --The file commands are loaded from, one command per line; this should be in data/scripts

if cf.fileExists(rff.filename,"data/scripts",true) then --Check that the file exists
local file = cf.openFile(rff.filename,"r","data/scripts") --Open the file
local i = 1
while true do
local entry = file:read("*l") --Read a line of the file
if entry == nil then break end --If there is no line to be read, the end of the file has been reached and so finish reading the file
rff.commands[i] = entry --Load the entry into the table
file:close() --Close the file
end = function(line) --Run a lua command from a line loaded from the file
local t = rff.commands[line] --Load the tabulated string
if t then --Check that a string has been loaded into the table entry
local f = loadstring(t) --Load the string into a function
if f then --Check that the function is valid
f() --Run the string
ba.warning("'"..t.."' loaded from line "..line.." of "..t.." and called from is not valid.") --Give a debug warning if string loaded isn't valid
ba.warning(" has just attempted to load line "..line.." from "..rff.filename.." and failed. Most likely this line or the file does not exist.") --Give a debug warning if the table entry is empty


« Last Edit: June 17, 2013, 11:18:25 am by FelixJim »
In-Mission Techroom Script v0.4 - See information from the techroom in game
Simple Animation Script v0.2 - Make your own on-screen animations for FSO
Visible Waypoints Script - Makes waypoints visible in-game
Run From File Script - Get around the pesky 31 character limit for script-eval