Hard Light Productions Forums

Modding, Mission Design, and Coding => The Scripting Workshop => Topic started by: wookieejedi on September 17, 2018, 02:42:29 am

Title: Favorite Personal LUA Tricks?
Post by: wookieejedi on September 17, 2018, 02:42:29 am
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").
Title: Re: Favorite Personal LUA Tricks?
Post by: Cyborg17 on September 17, 2018, 03:27:18 am
Var = Var sets what as the default value?
Title: Re: Favorite Personal LUA Tricks?
Post by: wookieejedi on September 17, 2018, 03:30:37 am
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

Title: Re: Favorite Personal LUA Tricks?
Post by: 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: [Select]
local test = require("test")

Lua will take care that the file is only loaded once and reused whenever necessary.
Title: Re: Favorite Personal LUA Tricks?
Post by: wookieejedi on September 17, 2018, 01:35:54 pm
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: [Select]
local test = require("test")

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

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