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

Calling variable from one API to another.

Started by helleputter, 22 May 2015 - 05:19 PM
helleputter #1
Posted 22 May 2015 - 07:19 PM
Hi,

I am trying to let 2 API's communicate (sort of). One variable gets made in one API and the other API uses it. Is it actually possible to get the variable? i know the code works (turned on redstone with it)

Here is the code from the first API called Com that creates the variable modem and sets it to true.

function ModemConnected()
  for T,side in ipairs(rs.getSides()) do
	if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
	  rednet.open(side)
	  Modem = 1
	  print(Modem)
	end
  end
end

the second API called Updater checks if Modem is 1 and prints the modem is connected (full program does other things but thats isn't the point now.

os.loadAPI("Com")
Modem = 0
function FileReceiver()
  Com.ModemConnected()
  if Modem == 1 then
	print("Modem connected")
  end
  print("test")
end
  

and yeah a program to start the Updater API.

os.loadAPI("Updater")
print("> Started")
Updater.FileReceiver()
print("> Done")
KingofGamesYami #2
Posted 22 May 2015 - 10:00 PM
When you declare a variable globally (ei without local) in an API, the variable is stored in yourAPIName.yourVariableName

It's definitely possible to share a variable, but I have no idea why you'd want to do so - just write a single API. Most APIs are self-contained, and don't rely on external functions.
helleputter #3
Posted 09 June 2015 - 06:24 PM
Thanks for the help!
Got it working now!