Jump to content




Help about APIS


  • You cannot reply to this topic
7 replies to this topic

#1 lkmnds

  • Members
  • 25 posts
  • LocationBrazil

Posted 29 September 2015 - 11:36 PM

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



#2 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 30 September 2015 - 12: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.


#3 lkmnds

  • Members
  • 25 posts
  • LocationBrazil

Posted 30 September 2015 - 12: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)

#4 TYKUHN2

  • Members
  • 210 posts
  • LocationSomewhere in this dimension... I think.

Posted 30 September 2015 - 01:46 AM

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

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

Result: "Hello!"

#5 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 September 2015 - 01: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.

#6 lkmnds

  • Members
  • 25 posts
  • LocationBrazil

Posted 30 September 2015 - 09:36 AM

Now I see, like a main() function in C?

#7 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 30 September 2015 - 01: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.

#8 lkmnds

  • Members
  • 25 posts
  • LocationBrazil

Posted 30 September 2015 - 07:05 PM

Thank you, now I understand! :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users