Error with Variadic Parameters in API

Started by Toytl_King, Aug 08, 2024, 06:02 PM

Previous topic - Next topic

Toytl_King

Hello,
I recently was trying to build a system which would make it easy for a person to host a collection of files with links to each other and have other users connect to the server so that they could access the contents of the files. I wrote a few APIs in order to clear up my code a bit. However, upon testing, I've come to the realization that variadic parameters don't seem to work across the API. I've tried both require() and os.getAPI() but they both run into the same issue. The error I get is:
/api.lua:2: bad argument (table expected, got nil)Throughout my testing, I've come to realize that putting the ellipsis only how I would expect it to when working with an inline function. For example, if I take the function out of the API and put it the the script I want to run it at, my variadic parameters work fine. They only break when it's in an API.
I believe this is because it thinks I'm trying to take script arguments. If I run the API script by itself (and also add a function call to the end) it prints the name of the script plus everything I typed after that.

Is there anyway I can use variadic templates with my APIs?

INFO:
I am running CC on Minecraft 1.20.1
I am running on Forge
I have one addon installed, Digital Items 3 (https://www.curseforge.com/minecraft/mc-mods/digital-items-3)

I have attached the scripts I am using in my testing environment.

SquidDev

Neither require or os.loadAPI accept extra arguments directly. Instead, you can return a function from the external library, and then use that function elsewhere.

So, in api.lua
local function do_something(...)
  print("Doing something with", ...)
end

return { do_something = do_something }

Then, in the file which uses it:
local api = require("api")
api.do_something(1, 2, 3)

GitHub | CC:Tweaked: A ComputerCraft fork | Plethora: A peripheral mod