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…)
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… “io.open()”. 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 io.open() 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 io.open() 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 file_found=io.open(file_name, "r") if file_found==nil then file_found=file_name .. " ... Error - File Not Found" else file_found=file_name .. " ... File Found" end return file_found end print(file_check("C:\\Stonehearth\\Mods\\Test Mod.txt")) print(file_check("C:\\Stonehearth\\Mods\\TestMod.txt")) print(file_check("C:\\Stoneheart\\Mods\\Test Mod.txt"))