This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
lkmnds's profile picture

Help about APIS

Started by lkmnds, 29 September 2015 - 10:36 PM
lkmnds #1
Posted 30 September 2015 - 12:36 AM
Hi!, this is my first topic here!

One question:

How can I get a non-localized variable from an API, but this API is in a "while true do" loop, and this variable is not in _G while its doing the loop

example API:


handler = {}
handler.test = 0

while true do
    print("hi world")
    os.sleep(1)
end


loader code for API:


os.loadAPI("example")
K = _G["example"] --this is running while the api is loading
print(K.handler) -- gives nil because of the while loop

KingofGamesYami #2
Posted 30 September 2015 - 01:15 AM
Um… your API shouldn't be executing a loop in the body. Maybe in a function, but not in the body. In your example, here's what would happen line by line:


os.loadAPI( "example" ) --#never move past this line because the API never loads... because it's executing a loop
K = _G["example"] --#wohoo, if the API ever stopped looping this might be executed
print( K.handler ) --#this would print a nice table pointer (table: #######) if it was ever executed, which it wasn't.
lkmnds #3
Posted 30 September 2015 - 01:34 AM
That's the problem, how can I do this, or some method of getting the non-localized variables of the API, but not loading the API itself, since doing that it will cause the problem described?

(Just for explanation: I just called this an API for simplicity, but I'm using os.loadAPI to load a program, which has some non-localized variables that I could get)
TYKUHN2 #4
Posted 30 September 2015 - 02:46 AM
API "printerHello"

function printHello()
    print("Hello!")
end

Program "printerUser"

os.loadAPI("printerHello")
printerHello.printHello()

Result: "Hello!"
Bomb Bloke #5
Posted 30 September 2015 - 02:49 AM
I guess what you're wanting to do is stick the loop in a function, then once the API is loaded, run that function in parallel with the rest of your code.

But that's a guess - the best way to do things hinges on what your overall goal here is. Provide some more details if you're still stuck/unsure.
lkmnds #6
Posted 30 September 2015 - 10:36 AM
Now I see, like a main() function in C?
Bomb Bloke #7
Posted 30 September 2015 - 02:17 PM
Nope.

The point is that most Lua functions run in order - one has to complete before another can begin. There are a few exceptions to the rule, but they're exceptions, and few and far between.

If you call os.loadAPI("suchAndSuch"), then the code in the "suchAndSuch" file is executed, and any global variables it produced are loaded into a _G.suchAndSuch table once it completes. Until it completes, os.loadAPI() does not return, meaning that any lines of code you write under that call won't be executed. I recommend reading this tutorial about APIs, if you haven't already.

You've written an API that performs an infinite loop when executed. As such, os.loadAPI() won't ever return, and so your script won't continue after calling it.

If you really need to have an infinite loop performed by your API, then sticking it within a function definition means that loading the API won't run the loop - it'll simply define the function that runs the loop. You can then use something like the parallel API to run the function containing the loop "alongside" whatever other code you want to run at the same time the loop is being processed.

Again, that's not the only way of handling the whole "multitasking" thing, and it may or may not be the best way to achieve your desired "end result". The "best way" would depend on what your desired "end result" is. If you have difficulty explaining it, just comment what you've got and dump it onto Pastebin, providing links here.
lkmnds #8
Posted 30 September 2015 - 08:05 PM
Thank you, now I understand! :)/>