Author Topic: Favorite Personal LUA Tricks?  (Read 13786 times)

0 Members and 1 Guest are viewing this topic.

Offline wookieejedi

  • 29
  • Intensify Forward Firepower
Favorite Personal LUA Tricks?
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").

 

Offline Cyborg17

  • 29
  • Life? Don't talk to me about life....
Re: Favorite Personal LUA Tricks?
Var = Var sets what as the default value?

 

Offline wookieejedi

  • 29
  • Intensify Forward Firepower
Re: Favorite Personal LUA Tricks?
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


 

Offline m!m

  • 211
Re: Favorite Personal LUA Tricks?
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.

  

Offline wookieejedi

  • 29
  • Intensify Forward Firepower
Re: Favorite Personal LUA Tricks?
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