-
Posts
9 -
Joined
-
Last visited
-
Days Won
1
Content Type
Profiles
Forums
Store
Calendar
Downloads
Posts posted by Codera
-
-
term trol
-
+rep had to check spam folder, other than that - no issues at all
term trol
+rep had to check spam folder, other than that - no issues at all
Using require() in scripts
in General discussion
Posted
1. Introduction
Hi. In this forum post, I would like to talk about the following Lua built-in function.
With this function, you're able to use return values of other source files in your script, like that:
2. Why?
Imagine having a script with some complex logic, for example, calling out to 3-4 different APIs, each with their own different return types (XML, JSON, Binary-serialized C struct). While I'm not 100% sure that this is possible, you can probably use the LuaRocks package manager with this setup. This also allows you to do something like a UI Component library, specifically for Astolfo (with the implementation possibly being abstract/separated from the Astolfo API, allowing you to use it on other clients with the Lua API, such as Moon). This also allows you to separate out the big functions, such as, again, a pathfinder.
3. The problem at hand
However, this is not possible with the Astolfo Lua runtime, as it is not fully compliant with Lua spec, regarding functions like loadstring(), require(), or loadfile(), you can't use those functions dynamically (with it being the keyword here). It is possible to statically link all of your code into a single, but big, Lua source file, which the client will easily be able to parse and load.
4. How?
Let's start with the prerequisites.
1. You must have a Node compatible runtime with TS support (for example, node itself) and NPM installed. The tool that will be shown here is written in TypeScript, and as such, this is one of the requirements.
2. A way to easily invoke the tool you need. For this, as I'm a Linux user, I will use GNU Make. On windows, you should probably just use a .bat file.
After making sure that the prerequisites are set up fine, you should run the following command as an administrator/root:
This will install the Lua bundler. With it, you can statically link all of your code to a single Lua source file, being a transcompiler/transpiler.
To use it, create a Makefile/Batch file, and put the following in it:
With this, running the build file/Make, it will statically (at build-time) resolve all the imported files and put them in your script. Now, you can run the output script, and it will load perfectly fine, just like any other script, except that the output file will be harder to modify.