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

hidden parallel tread inside api

Started by agowa338, 06 April 2015 - 09:27 PM
agowa338 #1
Posted 06 April 2015 - 11:27 PM
I'm writing an custom API for all my Computercraft devices, if I write shell.openTab("otherprogram") into the lua prompt it works fine.
But if I do the inside my API I'll receive "myapi:78: attempt to index ? (a nil value)"

What am I doing wrong or is there another way doing that in an API?
Edited on 06 April 2015 - 09:27 PM
Wojbie #2
Posted 06 April 2015 - 11:30 PM
APIs don't have access to shell and multishell functions (tables/APIs). Only API that does stuff in parallel with shell right now (rednet API) is started at startup by special case code added for it. What do you try to do exactly we could try to find other way do do that.
Edited on 06 April 2015 - 09:35 PM
ElvishJerricco #3
Posted 06 April 2015 - 11:32 PM
APIs don't have access to the shell API. You can't open tabs from APIs.

EDIT: ninja'd =P
Edited on 06 April 2015 - 09:33 PM
agowa338 #4
Posted 06 April 2015 - 11:39 PM
I'm trying to make a api that replaces the turtle.forward(), turtle.turn… functions with my custom ones, that traces the coordinates and starts a gps host with this commands and after each movement it stops and restarts the gps host with the new commands. Therefore i have a 2nd file:

function startgps()
local x, y, z = myapi.getCoords()
shell.run("gps host "..x.. " "..y.." "..z)
end
function getExitEvent()
while not (os.pullEvent() == "STOP_GPS_NOW") do end
end
do
parallel.waitForAny(startgps, getExitEvent)
end

With that i wanted to make a decentralized gps network.
Wojbie #5
Posted 06 April 2015 - 11:44 PM
You could make a custom gps host function inside your API that would always had correct coords due to use of myapi.getCoords() function. That way all end user would need to do is start it once in parrapel on their own and it would chug along in background always having correct gps cords.
agowa338 #6
Posted 06 April 2015 - 11:49 PM
Are there any issues by doing a custom gps function if there are also normal gps satellites in reach?
Wojbie #7
Posted 06 April 2015 - 11:51 PM
IF you base it on normal gps program code they will work with each-other beautifully. Stuff i am suggesting is actually what i made for my turtle overwrite API i am working on right now so it is doable.
agowa338 #8
Posted 06 April 2015 - 11:53 PM
Thanks, I'll check the gps file out to see how it is implemented and do a re-implementation.
agowa338 #9
Posted 07 April 2015 - 12:17 AM
Now I have implemented it in a slightly different way, I added a function to start a new shell in parallel, like that:

local function startShell()
if term.isColour() then
  os.run( {}, "rom/programs/advanced/multishell" )
else
  os.run( {}, "rom/programs/shell" )
end
os.run( {}, "rom/programs/shutdown" )
end
local function gpsServer()
-- Act as a GPS host
if pocket then
	 print( "GPS Hosts must be stationary" )
	 return
end
-- Find a modem
local sModemSide = nil
for n,sSide in ipairs( rs.getSides() ) do
  if peripheral.getType( sSide ) == "modem" and peripheral.call( sSide, "isWireless" ) then
   sModemSide = sSide
   break
  end
end
if sModemSide == nil then
  print( "No wireless modems found. 1 required." )
  return
end

-- Open a channel
local modem = peripheral.wrap( sModemSide )
    print( "Opening channel on modem "..sModemSide )
    modem.open( gps.CHANNEL_GPS )
-- Serve requests indefinitely
while true do
  local e, p1, p2, p3, p4, p5 = os.pullEvent( "modem_message" )
  if e == "modem_message" then
   -- We received a message from a modem
   local sSide, sChannel, sReplyChannel, sMessage, nDistance = p1, p2, p3, p4, p5
   if sSide == sModemSide and sChannel == gps.CHANNEL_GPS and sMessage == "PING" then
    -- We received a ping message on the GPS channel, send a response
    modem.transmit( sReplyChannel, gps.CHANNEL_GPS, { x, y, z } )
   end
  end
end
-- Close the channel
    print( "Closing channel" )
    modem.close( gps.CHANNEL_GPS )
end

do
    parallel.waitForAll(gpsServer, startShell)
end
Rougeminner #10
Posted 08 April 2015 - 05:28 AM
your os.run({},"/rom/programs/advanced/multishell") is not needed thanks to dan there is a multishell API just use multishell.launch({},"/agowa338/gpsTracker") this code will work as long as you replace /agowa338/gpsTracked with the proper location of course,

for example one could open a secondary shell by editing startup like this


multishell.launh({},"rom/programs/shell")
Bomb Bloke #11
Posted 08 April 2015 - 08:37 AM
I suspect the idea might be to conserve gold.