Lua Script: Check if file exists (easy)

This following script allows to check if a defined file exists. This could be useful if you want to check if e.g. a certain Mod is installed.

To receive the expected results this needs a bit of preparation. If you want to give it a try, please create a Folder “C:\Stonehearth\Mods” and save a .txt-File in this folder which you call “Test Mod.txt”. We will check the existance of this file during the example.

Update: This code might only work out under Windows.

(Translation into German is also in place…)

The code:


Line 3: The logic of this script should be again included in a function, to make it re-usable. Here the function is called “file_check” and expects a parameter which should contain the path to the file we want to check. The parameter will be stored in the variable “file_name” for further use in the function.

Line 4: Here we use a new pre-defined function… “”. This function belongs to the I/O-Library (Input and Output) and is pre-defined by Lua. The function tries to open a file of which the path is handed over to the function as the first parameter (we simply call the function with the “file_name” varaible as we expect that this will include the path). The second parameter is optional and defines how the file will be opened. “r” stands for “read”, so the file would be readable only.

As many functions also this one will return a value. This value should be filled in the variable “file_found”.

Lines 6 to 10: The return value of is now already stored in the variable “file_found”. There are two general information which can be stored, based on the fact if the file was found / opened or not. If not, the return value of is equal to “nil”. We can check therefore if the value of our variable is “nil”. If yes, the file was not found and does not exist. We check this with the if-command in line 6 and manipulate the content of “file_found”. We simply overwrite it with the text we want to return from our function “file_check”.

Line 9 will be only called if the file was opened, so we can overwrite with a text highligting that it worked out.

Line 11: We return the value of “file_found” which is either the message that the file was found, or not.

Lines 14 to 16: Here we call the function “file_check” with 3 different paths. If the preparation was done correctly, we should receive twice the error-message as a return value (lines 15 and 16) and only once the success-message (line 14).

As with all the other examples… the way is the goal. I do expect that Stonehearth will provide another way to find out if a Mod is installed or not.

Here the “copy & paste”-version of the code:

-- Check availability of file

function file_check(file_name)
  local, "r")      
  if file_found==nil then
    file_found=file_name .. " ... Error - File Not Found"
    file_found=file_name .. " ... File Found"
  return file_found

print(file_check("C:\\Stonehearth\\Mods\\Test Mod.txt"))
print(file_check("C:\\Stoneheart\\Mods\\Test Mod.txt"))

Key thing that that doesn’t support, multi-platform. UNIX uses / not \ for path separators along with not having a drive at the start, I googled around and it appears that there is no nice way to do this with core lua. Anyway I’d personally suggest we get a standard API for fs stuff if we even need it in stonehearth, preferably just block it completely however and provide support things like checking for mods and other stuff. I’d say more but I’m starting to go into tangents about desired modding API features now and should probably stop. Anyway code looks suspicious to me what with no errors from loading a file that doesn’t exist, that kind of thing should be a serious error.

Unfortunately Lua is here a bit rudimental related to filesysems, so you have to work with modules to make it more solid… but I am not there yet ;-). To make this clear, I will update the initial post… good point.

Regarding “…what with no errors from loading a file that doesn’t exist…”, if a file does not exist returns “nil” which I am checking for. There should be only the two conditions which I am handling in the script… to my understanding.

Yeah I mean like how python gives an IO Error that stops the program unless prepared for, and lua is limited by what ANSI C can do so you need libraries to get proper functionality for them. Either way it’s something worth learning but for most modding in games they shouldn’t let you have direct interaction with the os or fs for security issues, and there can’t be exceptions for standard stuff in case they’re exploited by other mods so I’d say hardcoded API here.

@Ponder (Tony) replied to one of my questions that the Load/Save system in game is part of the Core in C++ and therefore not modable.
see Here

Although this Lua algorithm is interesting it might not be useful in Stonehearth.