Hard Light Productions Forums
FreeSpace Releases => Scripting Releases => Topic started by: m!m on November 24, 2014, 03:23:38 pm
-
I put together a small lua package that contains a few things that could be useful for a lua coder: https://github.com/asarium/FSOLuaUtils
Contents:
require package loader
The package contains a loader implementation so you can use require (http://www.lua.org/manual/5.1/manual.html#pdf-require) with modules that exist within the file system of FSO (that also includes VP files).
There is not much more to know about it except that when the loader has been initialized the variable cfileRequireInitialized is set to true. You can use that to delay the usage of require until the loaders have been initialized.
Coroutine manager
You can now use coroutines within FSO to execute code distributed over multiple frames (if you don't know what coroutines are you can read this tutorial (http://www.lua.org/pil/9.1.html)).
To use coroutines you first have to require the 'fsoCoroutine' module (fsoCoroutine = require("fsoCoroutine")).
The currently available function in that module are:
- startCoroutine(function): Calling this with a function argument will create a new coroutine and return it. From that point on the coroutine will be resumed every frame until it is done.
- waitUntil(function): This function will yield the coroutine until the given function returns true. The argument can be any function but this package contains some functions that handle most use-cases.
- secondsPassed(timeDiff): Returns a function that returns false until the given amount of seconds have passed based on the mission time of FSO. This function should be used with waitUntil() (see the example for a usage example).
Example:
local fc = require("fsoCoroutine")
fc.startCoroutine(function()
doSomeStartingStuff()
fc.waitUntil(fc.secondsPassed(10))
doSomeStuff()
fc.waitUntil(fc.secondsPassed(20))
doSomeOtherStuff()
end)
-
Sounds pretty cool! :yes:
-
this is very useful. now you can write stand alone scripts and let everyone require them in their own code.
i guess it would be good practice to put binary module files for all platforms into your file structure/vp files. it should in theory work the same on linux as windows after that.
-
I created a Github repository with the code for easier version management.
I changed the loaded to also try loading .lc files if the modder distributed a precompiled version of the module.