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

Global table in an API and a normal Program

Started by Luca_S, 16 November 2014 - 08:52 AM
Luca_S #1
Posted 16 November 2014 - 09:52 AM
Hi,

i have the following problem: I need a global table which is the same in an API and a normal Program(something like this:

API:

function addtotable(value)
  table[#table + 1] = value
end
Normal program:

table = {}
os.loadAPI("/API")
API.addtotable("test")
print(table[1])

The output of this is nothing, but should be "test". I hope someone knows a solution.
Lyqyd #2
Posted 16 November 2014 - 10:35 AM
Why? There are ways something like this could be done, but there's probably a better way to accomplish whatever you plan on using this for. What's the end goal?
Luca_S #3
Posted 16 November 2014 - 12:43 PM
I wanna save some coroutines in two tables: one for running coroutines, one for suspended, you should be able to use a function like multitasking.addtask(task), etc. but the loop, which resumes the coroutines is in another Program. Now the program with the loop and the multitasking API cant acces the same Table(The loop program cant find any coroutines running/suspended). Thats my Problem.
Edited on 16 November 2014 - 11:47 AM
MKlegoman357 #4
Posted 16 November 2014 - 01:13 PM
Then I would suggest making all of your multitasking system into one API/program.
Luca_S #5
Posted 16 November 2014 - 01:41 PM
I tried it, but if I put the while loop into the API no more code will be executed after os.loadAPI()
MKlegoman357 #6
Posted 16 November 2014 - 02:58 PM
Well, you can make a function in the API which would hold your main loop:


function main ()
  while true do
    --// code here
  end
end
Luca_S #7
Posted 16 November 2014 - 05:37 PM
That would be an idea, i will try this

EDIT: Tried it, but it only runs the coroutine ones…

Btw, why cant you use tables in an API and a program at once?

I have another idea too now, i will execute the API as an program. I will test this now.
Edited on 16 November 2014 - 04:51 PM
KingofGamesYami #8
Posted 16 November 2014 - 06:03 PM
You can define a table globally inside your api, and access it through api_name.table_name in your program.

However, what MKlegoman said should work, if you do it properly.
Luca_S #9
Posted 16 November 2014 - 08:07 PM
Thanks, i just tried to access the Table, now i know how to fix my Program :D/>. And thanks to anyone other who help!