Main Menu

Custom Api on server

Started by Beaver, Nov 03, 2020, 09:44 PM

Previous topic - Next topic

Beaver

Hi guys, i'm new to cc programming and lua in general, and i have a little problem. I want to use a custom API from internet, but I dont know how to do that. The method changed from cc version to cc version, so i dont know what tuto reading. Also i am playing on a server, so probably the process is different. I am using cc version 1.75 on mc version 1.7.10.
Thank you in advance, and if someone already answered that in another post, please let me know.

LDDestroier

In older versions of ComputerCraft, you would load APIs with the os.loadAPI() function. However, that's now considered poopy practice (it infects the global scope -- I'm not going to get into why exactly that's bad), so instead, you should use require.
For instance, you want to load in BigFont:

bigfont = require "bigfont"
bigfont.bigPrint("Hello, world!")
...


That line of code will load bigfont.lua and let you use it with the "bigfont" table. Don't add the ".lua" part of the filename into the argument for require, because require interprets them like slashes, like for going down directories.
(If you're wondering about the lack of parentheses, they're optional when you're only passing a single argument through it, and only if it's a string.)

As for downloading the APIs themselves, you can still just use the pastebin command, or on later versions, the wget command. Most APIs these days are usable with require, so don't worry about maybe sometimes having to use os.loadAPI(). I hope that covers what you're asking about.

Lupus590

There are two ways to use Lua APIs for CC, require is the prefered way but doesn't work in older CC versions. Given that you are using an old version of CC, you will either have to download another implementation of require or use the built-in os.loadapi function. There's also things like dofile and loadfile, but require and os.loadapi are better than those in nearly every way.

Here's some documentation on the os.loadapi function:
http://www.computercraft.info/forums2/index.php?/topic/29007-loading-an-api-the-best-way/
http://computercraft.info/wiki/Os.loadAPI
https://wiki.computercraft.cc/Os.loadAPI

Quote from: LDDestroier on Nov 04, 2020, 07:19 AM-snip-
Require is newer than CC 1.75 I belive, so they are going to have to use os.luadAPI or a third party implementation of require.

Beaver

Ok, got it, but what about downloading on a server? I need to place the .lua inside the server folder or the pastebin command does it automatically?

Lupus590

Quote from: Beaver on Nov 04, 2020, 11:24 AMwhat about downloading on a server?

if you are using a CC program to download the API then it will be available for the CC computer that downloaded it. If you want it available for every computer on the server then a server admin will need to make a resource pack with the program and place that resource pack on the server (clients don't need the Lua code, only the server does).