Codera

Premium
  • Posts

    9
  • Joined

  • Last visited

  • Days Won

    1

Codera last won the day on September 28 2023

Codera had the most liked content!

2 Followers

Recent Profile Visitors

318 profile views

Codera's Achievements

1

Reputation

  1. 1. Introduction Hi. In this forum post, I would like to talk about the following Lua built-in function. require() With this function, you're able to use return values of other source files in your script, like that: -- Filename: util.lua local M = {} --- Pathfinds from one position to another, with a specific step between positions. -- @param from Table with the x, y, z fields set, the initial point, from which it will try to find a path to the `to` point. -- @param to Table with the x, y, z fields set, the final point, to which the path will be attempt to be built. -- @param step Step between positions, I.E. with from={x=100, y=100, z=100}; to={x=200,y=200,z=200} the path with step 50 would look something like this: { {x=100,y=100,z=100}, {x=150,y=150,z=150}, {x=200,y=200,z=200 -- @return The table with path needed to go to if found/possible, otherwise nil. M.pathfind = function(from, to, step) -- TODO: Fill this end return M -- Filename: main.lua local utils = require("util") utils.pathfind(...) -- Just an example of the usage of require function. 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: npm install -g luabundler 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: build: # You can omit this line, if you're using Batch or UNIX Shell. luabundler bundle src/main.lua -p "src/?.lua" -o bundled.lua # This line invokes the Lua bundler tool with the source code being in src/, with the entrypoint file (the one that should ideally either require other files or register the main module) called main.lua, the file resolve/search path being every .lua file in src folder, and the output being in the root folder, named bundled.lua. To configure it in a different way, take a look at the README of the bundler. 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.