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

Stargate program crash when atempting to connect to busy gate

Started by Landstryder, 08 December 2013 - 11:12 AM
Landstryder #1
Posted 08 December 2013 - 12:12 PM
OK I got some of the bugs worked out of my dialing program but I'm not sure what to do about this problem.

When ever the program attempts to dial a gate that is busy (either connected to another gate or was chunk unloaded while connected)
it errors out the program at the console with the line number that the connect function is on and the message that the gate is busy.

I'm guessing I need to have some kind of event handler in there…

the code in question:

http://pastebin.com/RDHb3jC4


oh and how can I get the chevron encoded amount to format as a whole number instead of adding a decimal and zero?
theoriginalbit #2
Posted 08 December 2013 - 05:27 PM
ooooo, yay! someone finally using the new OpenPeripheral ;)/>

the error was an oversight on my part, and Mikeemoo not explaining how to implement multi-return values correctly :P/> as such when something cannot happen the code will error back to you. The way to get around this is to pcall each function that will error on you. you can also then check the error message to see why it won't happen. I'll supply a list in a minute to help you :)/> here is example code

local ok, err = pcall(gate.connect, "HGFIESF")
if not ok then --# the "ok" variable is whether or not the function call was successful, in this case we're checking if it was not successful
  print("Something went wrong!")
  print(err) --# this will print out what went wrong from the error message stored in "err"
end

A list of what can go wrong
connect
  • "Stargate damaged or incompelte" — (oops, just saw spelling mistake, fixed in the next version) occurs when the gate is missing blocks and is not a complete ring (can be mitigated by checking isGateComplete)
  • "Stargate addresses must be 7 letters" — occurs when the target address is not enough characters (can be mitigated by checking length yourself)
  • "No Stargate at address " + address — occurs when the stargate doesn't exist (can be mitigated by checking the address with isValidAddress)
  • "Stargate cannot connect to itself" — occurs when the target address is the same as the origin address (can be mitigated by making sure they're not trying to dial themselves)
  • "Stargate at address " + targetAddress + " is busy" — occurs when the target gate is currently connected to another gate
  • "Stargate has insufficient fuel" — occurs when the origin gate does not have enough fuel to make a connection (can be mitigated by checking fuel levels first!)
disconnect / getState / getLockedChevronCount / getFuelLevel / getMaxFuelLevel / isDHDConnected / isConnected / isInitiator / canTravelFromThisEnd / getDialledAddress
  • "Stargate damaged or incompelte" — occurs when the gate is missing blocks and is not a complete ring (can be mitigated by checking isGateComplete)

As you can see from the above list, most functions you can call on the stargate will error (there are a few that won't) but all of them except the target gate being busy has some way for you to avoid them. I suggest setting up helper/wrapper functions like so


local function connect(addr)
  if not gate.isGateComplete() then
	return false, "Gate is not yet built"
  end
  if #addr ~= 7 then
	return false, "Incomplete gate address"
  end
  if not isValidAddres(addr) then
	return false, "That stargate doesn't exist"
  end
  --# etc
  gate.connect(addr)
end

local function disconnect()
  if gate.isGateComplete() then
	gate.disconnect()
  end
end

--# etc

as for getting it to not have the .0 do this

m.write(string.format("%d", gate.getLockedChevronCount()))
having %d will remove it.
Edited on 08 December 2013 - 04:28 PM
Landstryder #3
Posted 08 December 2013 - 05:55 PM
thanks for the info! have to get to work on a new version now :D/>

ooooo, yay! someone finally using the new OpenPeripheral ;)/>
yea I've been toying with the Robot Warrior too, been thinking of making a gate site defender that could run as a coroutine to the gate dialer, but I've not yet even tried to understand coroutines :P/>