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

Find program thet used an API

Started by flaghacker, 22 April 2014 - 02:06 PM
flaghacker #1
Posted 22 April 2014 - 04:06 PM
Is there any way an API can know witch program called it? Because I want to make an API save data about different programs, like this:

Program:

os.loadAPI("test")
test.setId(5)

API:

idTable = {}
function setId(id)
  idTable[getCallProgram()] = id
end

With getCallProgram() being a function that returns the program that called that function.
Edited on 22 April 2014 - 02:07 PM
CometWolf #2
Posted 22 April 2014 - 04:14 PM
Normally you'd use shell.getRunningProgram, but chances are an API loaded with os.loadAPI won't have access to the shell. Meaning that you would either have to pass the result of shell.getRunningProgram to the API through a function, or load your api in a different way. eg loading it with loadfile into a specific environment which has access to shell, then store that table in global. This is essentially what os.loadAPI does, minus the shell part.
flaghacker #3
Posted 22 April 2014 - 04:34 PM
How do you do that?
(I'm not that advanced in lua/computercraft)

And why doesn't os.loadApi gives access to the shell?
Edited on 22 April 2014 - 02:36 PM
CometWolf #4
Posted 22 April 2014 - 05:33 PM
It's something to do with the ability to have multiple instances of the shell running at the same time, i forgot the exact reason :P/> The easiest thing in your case would probably be to just setup a function in your api

local runningProgram
function setRunningProgram(program)
  runningProgram = program
end

api.setRunningProgram(shell.getRunningProgram())
flaghacker #5
Posted 22 April 2014 - 09:14 PM
Can I put the "api.setRunningProgram(shell.getRunningProgram())" in a separate file then? And would it work when I run that file at the beginning of the API, so that when it's loaded it can detect what program did it?

O, wait. That wouldn't work work because then the program would detect itself… I guess?

I'll have to test it tomorrow…

Thanks!
CometWolf #6
Posted 22 April 2014 - 09:29 PM
I was thinking you'd put that function in your own API, then call it at the start of your program after loading your api.