diff --git a/docs/game_data/spel2.lua b/docs/game_data/spel2.lua index f9f06777c..3eaaf6188 100644 --- a/docs/game_data/spel2.lua +++ b/docs/game_data/spel2.lua @@ -1157,9 +1157,13 @@ function get_local_state() end ---@return nil function get_local_players() end ---List files in directory relative to the script root. Returns table of file/directory names or nil if not found. ----@param dir string +---@param dir string? ---@return nil function list_dir(dir) end +---List files in directory relative to the mods data directory (Mods/Data/...). Returns table of file/directory names or nil if not found. +---@param dir string? +---@return nil +function list_data_dir(dir) end ---List all char.png files recursively from Mods/Packs. Returns table of file paths. ---@return nil function list_char_mods() end diff --git a/docs/index.html b/docs/index.html index 69f63ad49..257adfe7c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -324,6 +324,12 @@
If you use a text editor/IDE that has a Lua linter available you can download spel2.lua, place it in a folder of your choice and specify that folder as a "external function library". For example VSCode with the Lua Extension offers this feature. This will allow you to get auto-completion of API functions along with linting
The following Lua libraries and their functions are available. You can read more about them in the Lua documentation. We're using Lua 5.4 with the Sol C++ binding.
+-- Write a data file
+-- Data will be written to Mods/Data/[scriptname.lua or Mod Name]/timestamp.txt
+local f = io.open_data(tostring(os.time()) .. ".txt", "w")
+if f then
+ f:write("hello world at " .. os.date())
+ f:close()
+end
+
+-- List all files in data dir and read them out
+for _, v in pairs(list_data_dir()) do
+ local f = io.open_data(v)
+ if f then
+ print(v .. ": " .. f:read("a"))
+ end
+end
+
+
meta.unsafe
exposes all standard library functions and removes basedir restrictions from the custom functions.
In safe mode (default) the following standard and custom functions are available:
+ +io.type
io.open_data
: like io.open
but restricted to base directory Mods/Data/modname
io.open_mod
: like io.open
but restricted to the mod directorySafely opened files can be used normally through the file:
handle. Files and folders opened in write mode are automatically created.
Also see list_dir and list_data_dir.
+meta.unsafe
exposes all standard library functions and removes basedir restrictions from the custom functions.
In safe mode (default) the following standard and custom functions are available:
+ +os.clock
os.date
os.difftime
os.time
os.remove_data
: like os.remove
but restricted to base directory Mods/Data/modname
os.remove_mod
: like os.remove
but restricted to the mod directoryTo save data in your mod it makes a lot of sense to use json
to encode a table into a string and decode strings to table. For example this code that saves table and loads it back:
local some_mod_data_that_should_be_saved = {{
@@ -3662,7 +3714,7 @@ math
base
string
message(name)
end
Setting meta.unsafe = true
enables the rest of the standard Lua libraries like io
and os
, loading dlls with require and package.loadlib
. Using unsafe scripts requires users to enable the option in the overlunky.ini file which is found in the Spelunky 2 installation directory.
Setting meta.unsafe = true
enables the rest of the standard Lua libraries like unrestricted io
and os
, loading dlls with require and package.loadlib
. Using unsafe scripts requires users to enable the option in the overlunky.ini file which is found in the Spelunky 2 installation directory.
You can load modules with require "mymod"
or require "mydir.mymod"
, just put mymod.lua
in the same directory the script is, or in mydir/
to keep things organized.
List all char.png files recursively from Mods/Packs. Returns table of file paths.
+++Search script examples for list_data_dir
+
List files in directory relative to the mods data directory (Mods/Data/...). Returns table of file/directory names or nil if not found.
-Search script examples for list_dir
List files in directory relative to the script root. Returns table of file/directory names or nil if not found.
diff --git a/docs/light.html b/docs/light.html index a15112975..89a123b71 100644 --- a/docs/light.html +++ b/docs/light.html @@ -324,6 +324,12 @@- Lua libraries
+
- + io +
+- + os +
- math
@@ -788,6 +794,9 @@- list_char_mods
+- + list_data_dir +
- list_dir
@@ -3611,6 +3620,49 @@External Function Library
If you use a text editor/IDE that has a Lua linter available you can download spel2.lua, place it in a folder of your choice and specify that folder as a "external function library". For example VSCode with the Lua Extension offers this feature. This will allow you to get auto-completion of API functions along with linting
Lua libraries
The following Lua libraries and their functions are available. You can read more about them in the Lua documentation. We're using Lua 5.4 with the Sol C++ binding.
+io
+-- Write a data file +-- Data will be written to Mods/Data/[scriptname.lua or Mod Name]/timestamp.txt +local f = io.open_data(tostring(os.time()) .. ".txt", "w") +if f then + f:write("hello world at " .. os.date()) + f:close() +end + +-- List all files in data dir and read them out +for _, v in pairs(list_data_dir()) do + local f = io.open_data(v) + if f then + print(v .. ": " .. f:read("a")) + end +end + +
+ +
meta.unsafe
exposes all standard library functions and removes basedir restrictions from the custom functions.In safe mode (default) the following standard and custom functions are available:
+ ++
+ +- +
io.type
- +
io.open_data
: likeio.open
but restricted to base directoryMods/Data/modname
- +
io.open_mod
: likeio.open
but restricted to the mod directorySafely opened files can be used normally through the
+ +file:
handle. Files and folders opened in write mode are automatically created.Also see list_dir and list_data_dir.
+os
++ +
meta.unsafe
exposes all standard library functions and removes basedir restrictions from the custom functions.In safe mode (default) the following standard and custom functions are available:
+ ++
- +
os.clock
- +
os.date
- +
os.difftime
- +
os.time
- +
os.remove_data
: likeos.remove
but restricted to base directoryMods/Data/modname
- +
os.remove_mod
: likeos.remove
but restricted to the mod directorymath
base
string
table
coroutine
package
json
To save data in your mod it makes a lot of sense to use
json
to encode a table into a string and decode strings to table. For example this code that saves table and loads it back:local some_mod_data_that_should_be_saved = {{ @@ -3662,7 +3714,7 @@
math
base
string
message(name) end
Unsafe mode
-Setting
+meta.unsafe = true
enables the rest of the standard Lua libraries likeio
andos
, loading dlls with require andpackage.loadlib
. Using unsafe scripts requires users to enable the option in the overlunky.ini file which is found in the Spelunky 2 installation directory.Setting
meta.unsafe = true
enables the rest of the standard Lua libraries like unrestrictedio
andos
, loading dlls with require andpackage.loadlib
. Using unsafe scripts requires users to enable the option in the overlunky.ini file which is found in the Spelunky 2 installation directory.Modules
You can load modules with
@@ -4666,11 +4718,17 @@require "mymod"
orrequire "mydir.mymod"
, just putmymod.lua
in the same directory the script is, or inmydir/
to keep things organized.list_char_mods
List all char.png files recursively from Mods/Packs. Returns table of file paths.
+++Search script examples for list_data_dir
+
List files in directory relative to the mods data directory (Mods/Data/...). Returns table of file/directory names or nil if not found.
-Search script examples for list_dir
List files in directory relative to the script root. Returns table of file/directory names or nil if not found.