Modding, Mission Design, and Coding > The Scripting Workshop

Favorite Personal LUA Tricks?

(1/1)

wookieejedi:
As I am writing more and more in LUA I just wanted to know what favorite tips or tricks folks had regarding LUA in FSO? Or, what things did you learn about that you wished you knew much earlier?

Just to start off, one of my favorites is using or to set default values for variables (ie var= var or "foo").

Cyborg17:
Var = Var sets what as the default value?

wookieejedi:
Ah I should be more specific:

function test(input_var)

    input_var = input_var or 1 --So if the variable is nil then the "or" sets it to 1

   print(input_var)

end

test("bar") -> "bar"
test() -> 1

m!m:
I recently added built-in support for using the module system provided by Lua with the FSO filesystem. If you place a script file test.lua which is compatible with the Lua module system in data/scripts then you can load it into another script file like this:

--- Code: ---local test = require("test")

--- End code ---

Lua will take care that the file is only loaded once and reused whenever necessary.

wookieejedi:

--- Quote from: m!m on September 17, 2018, 09:21:19 am ---I recently added built-in support for using the module system provided by Lua with the FSO filesystem. If you place a script file test.lua which is compatible with the Lua module system in data/scripts then you can load it into another script file like this:

--- Code: ---local test = require("test")

--- End code ---

Lua will take care that the file is only loaded once and reused whenever necessary.

--- End quote ---

Oh that's cool. Sounds like something Axem would especially like  :D

Navigation

[0] Message Index

Go to full version