Skip to content

Lua Script API: Scripting & File Functions

Meme1079 edited this page Jul 24, 2023 · 12 revisions

Script Functions

addLuaScript(path:String, ?ignoreAlreadyRunning:Bool = false)

Adds a Lua script into the game.

  • path - The location of the Lua script file to be added.
  • ignoreAlreadyRunning - An optional parameter, It will execute while ignoring the Lua script that it's running from.

removeLuaScript(path:String)

Removes a Lua script into the game, if not used anymore.

  • path - The location of the Lua script file to be removed.

setOnLuas(varName:String, arg:Dynamic, ?ignoreSelf:Bool = false, ?exclusions:Array<String> = null)

Sets the variable globally across all currently active Lua scripts.

  • varName - The variable name to be referenced.
  • arg - The value for the variable to use.
  • ignoreSelf - (Still researching)
  • exclusions - (Still researching)

callOnLuas(funcName:String, ?args:Array<Dynamic> = null, ?ignoreStops = false, ?ignoreSelf:Bool = true, ?excludeScripts:Array<String> = null, ?excludeValues:Array<Dynamic> = null)

Sets the function globally across all currently active Lua scripts.

  • funcName - The function name to be referenced.
  • args - An optional parameter, The arguement(s) to be passed on the function.
  • ignoreStops - (Still researching)
  • ignoreSelf - (Still researching)
  • excludeScripts - (Still researching)
  • excludeValues - (Still researching)

isRunning(luaFile:String)

Checks if the Lua script is currently running; Returns a boolean.

  • luaFile - The location of the Lua script file to be used.

getRunningScripts()

Checks if multiple Lua scripts are current running; Returns a table.


Warning: These functions are weirdly buggy, it somehow works and other times it doesn't?????.

setGlobalFromScript(luaFile:String, global:String, val:Dynamic)

Calls a global variable from another Lua script and sets the current value with a new one.

  • luaFile - The location of Lua script to be referenced.
  • global - The global variable from the Lua script to get.
  • val - The new value to be set for the variable.

getGlobalFromScript(luaFile:String, global:String)

Calls a global variable from another script and gets the current value.

  • luaFile - The location of Lua script to be referenced.
  • global - The global variable from the Lua script to get.

callScript(luaFile:String, funcName:String, ?args:Array<Dynamic>)

Calls a global function from another Lua script.

  • luaFile - The location of Lua script to be referenced.
  • funcName - The function name from the Lua script to get.
  • args - An optional parameter, The arguement(s) to be passed to the function.

File Functions

getTextFromFile(path:String, ?ignoreMods:Bool = false)

Gets the text content from a txt file.

  • path - The location of the txt file to be used
  • ignoreMods - An optional parameter, It will be ignore by the mod folder directory, causing it to be outside the mod folder.

directoryFileList(folder:String)

Gets the contents of the folder; Returns a table.

  • folder - The location of the folder to be used; Starts outside the mod folder by defualt.

saveFile(path:String, content:String, ?absolute:Bool = false)

Changes the specific file contents and saves it.

  • path - The location of the file to be change
  • content - The file contents to be changed or modified, use double brackets [[]].
  • absolute - An optional parameter, It will be ignore by the mod folder directory, causing it to be outside the mod folder.

deleteFile(path:String)

Deletes the specific file literally.

  • path - The location of the file to be deleted.

checkFileExists(filename:String, ?absolute:Bool = false)

Checks if the file exist or not.

  • filename - The location of the Lua script file to be used.
  • absolute - An optional parameter, It will be ignore by the mod folder directory, causing it to be outside the mod folder.

Lua Exists Functions

luaSpriteExists(tag:String)

Checks if the sprite object exist inside the stage or script; Returns a boolean.

  • tag - The sprite object tag name to be used.

luaTextExists(tag:String)

Checks if the text object exist inside the stage or script; Returns a boolean.

  • tag - The text object tag name to be used.

luaSoundExists(tag:String)

Checks if the sound object exist inside the stage or script; Returns a boolean.

  • tag - The sound object tag name to be used.

Saving Data Functions

initSaveData(name:String, ?folder:String = 'psychenginemods')

Initializes/Creates the save data making it able to be called anywhere. This function is recommended to be place above the save data functions for obvious reasons.

  • name - The name of the save data to be used.
  • folder - An optional parameter, The folder to be loaded or be created.

flushSaveData(name:String)

Deletes the save data, if not used anymore; Returns a nil value.

  • name - The name of the save data to be used.

setDataFromSave(name:String, field:String, value:Dynamic)

Sets the current data field with a new value. Or creates a data field with a specify value of the data field.

  • name - The name of the save data to be used.
  • field - The name of the data field to be used.
  • value - The new value to be set or the specific value to be used, Like an Array, String, Number, etc.

getDataFromSave(name:String, field:String)

Gets the current data field current value; Returns the data field value.

  • name - The name of the save data to be used.
  • field - The name of the data field to be used.

String Tool Functions

stringStartsWith(str:String, start:String)

Checks the specific starting section of the string; Returns a boolean.

  • str - The string to be used.
  • start - The starting section to be called.
Lua Equivalent:

function string.startsWith(str, pattern)
     return str:match('^'..pattern) and true or false
end

stringEndsWith(str:String, end:String)

Checks the specific ending section of the string; Returns a boolean.

  • str - The string to be used.
  • end - The ending section to be called.
Lua Equivalent:

function string.endWith(str, pattern)
     return str:match(pattern..'$') and true or false
end

stringSplit(str:String, split:String)

Splits the string into multiple strings; Returns a table.

  • str - The string to be split.
  • split - The pattern of the string to split.
Lua Equivalent:

function string.split(str, sep)
     local words = {}
     for w in string.gmatch(str, "([^"..sep.."]+)") do
          table.insert(words, w)
     end
     return words
end

Example: stringSplit('A, B, C, D', ', ')[1], it will return A.

stringTrim(str:String)

Removes any whitspace characters from the string. Or you could just use ('string'):gsub('%s*', '') which functions the same. So this function is confirmed useless, so don't even bother using it lmao.

  • str - The string to be trimmed.

Randomization Functions

getRandomInt(min:Int, max:Int = FlxMath.MAX_VALUE_INT, exclude:String = '')

Randomizes the Int number from min to max values.

  • min - The minimum value.
  • max - The maximum value.
  • exclude - An optional parameter, The interger numbers to exclude when randomizing; Example: 1, 2, 4.

getRandomFloat(min:Float, max:Float = 1, exclude:String = '')

Randomizes the Float number from min to max values.

  • min - The minimum value.
  • max - The maximum value: Defualt value: 1.
  • exclude - An optional parameter, The floating-point numbers to exclude when randomizing; Example: 0.1, 2.5, 6.9.

getRandomBool(chance:Float = 50)

Randomizes the chances of returning a true value.

  • chance - The percent of it being true; Goes from 0 to 100; Default value: 50.

Global Variable Functions

setVar(varName:String, value:Dynamic)

Sets the current global variable with a new value. Or initializes the creation of a global variable if there is no global variable. This allows to access variable from other scripts, but only if the script is currently executed.

  • varName - The variable to be referenced.
  • value - The specified type of value for the variable to use or to over-write.

getVar(varName:String)

Gets the current global variable current value.

  • varName - The variable to be referenced.

Clone this wiki locally