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

Returning an error string if specific peripheral not found

Started by Cranium, 19 July 2014 - 06:56 AM
Cranium #1
Posted 19 July 2014 - 08:56 AM
I've got a program that automatically detects certain types of peripherals through Openperipheral, and displays things like fluid levels, energy levels, and storage amounts. Right now, it works okay, but I'm trying to detect if a certain type of peripheral isn't connected to the network, and return an error message. It's crashing the program when it doesn't find the fluids normally connected.
I'm not sure it's returning the string properly. I keep getting errors on line 71, index expected, got string.
I'm 90% certain I'm doing it wrong, but if anyone could help me improve it, I would appreciate it.

http://pastebin.com/iyPUG8XC
Lyqyd #2
Posted 19 July 2014 - 09:18 AM
You're looping through all connected peripherals. For each connected peripheral, you check if the wrapped variable exists, and if it has certain methods. If so, you add values to the table you declared. If not, you overwrite the table with a string and continue on. You're finding a peripheral that doesn't match what you want and then finding one that does, by which time your table is now a string.
Cranium #3
Posted 19 July 2014 - 04:13 PM
Hmm, yeah, I see your point. How would I implement what I want though? I wanna have it display the "error" string when nothing was found for that type of peipheral.
theoriginalbit #4
Posted 19 July 2014 - 04:20 PM
I'd say in your rendering code check to see if storage.capacity is 0 and if it is, then output the string as opposed to the percentage bar or however you wanted to lay out those stats.
Cranium #5
Posted 19 July 2014 - 04:42 PM
I've moved around where I check the info, and I get errors right now. Line 39 is getting me index expected, got string.
http://pastebin.com/iyPUG8XC

Edit: This is when no fluid tanks are connected.
Edited on 19 July 2014 - 02:42 PM
Bomb Bloke #6
Posted 19 July 2014 - 05:08 PM
It's saying that you're trying to treat a string like a table. The reason "liquid" is getting a string instead of a table is because of the way you built the "liquids" table on line 9.

Change that line to just:

local liquids = {}
Cranium #7
Posted 19 July 2014 - 06:47 PM
I think I got it. I took your suggestion, and after getting some more errors, I finally set it up to catch the errors.
http://pastebin.com/iyPUG8XC