[mod] Formspec Tools [formspec_tools]

Post Reply
User avatar
MisterE
Member
Posts: 693
Joined: Sun Feb 16, 2020 21:06
GitHub: MisterE123
IRC: MisterE
In-game: MisterE

[mod] Formspec Tools [formspec_tools]

by MisterE » Post

get the boring stuff outa the way:
license: CC0
depends: none
screenshot: N/A
download: https://notabug.org/MisterE123/formspec ... master.zip
git: https://notabug.org/MisterE123/formspec_tools
issues: https://notabug.org/MisterE123/formspec_tools/issues

So this is a simple, small library to make writing formspecs easier. You can install it as a mod or just copy the code into a file and run it.

The basic premise is that its difficult to write formspecs because of having to concatenate all the input strings together. This mod concatenates everything together for you, so you just have to call functions.

The inputs for the functions are the same as the inputs for the formspec elements, as much as possible. When a formspec element recieves an open-ended list, this mod implements it as a table that is passed to the function.

the advantage of using functions is that the syntax is easily readable, and that you can easily do calculations or call variables for the inputs to the formspec. While you could just break a formspec string, concatenate in a variable or calculation, and then resume the string with concatenation, keeping track of the syntax becomes bothersome, at least to me, and it leaves the code in a hard-to-read format. Using functions on an object seems simpler and easier to use and to think about.

The mod adds a single object which you can use to build your formspec.

example:
instead of this:

Code: Select all

local size_x = 8
local size_y = 9
local fs_string = "size["..size_x..","..size_y.."]"..
"list[context;main;0,0;8,4;]"..
"list[current_player;main;0,5;8,4;]"
using formspec_tools, you would do:

Code: Select all

local size_x = 8
local size_y = 9
fsobj = formspec_tools.Fs:new()
fsobj:size(size_x,size_y)
fsobj:list("context","main",0,0,8,4)
fsobj:list("current_player","main",0,5,8,4)
fs_string = fsobj.string
As you can see, it makes it easier to have variables input to the formspec without a lot more typing, and without having to worry about opening and closing strings.

The script is WIP, and likely has bugs, but the code is easy to read, short, and if you find a bug it will be easy to fix. Please submit bug reports on notabug.

I will be using this myself so I will be working out bugs. But I am unlikely to use all its functions, so please submit issues for bugs, or better, PRs.

Edit: I confirmed that it works, after a few bugfixes

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests