MaximilianVINCENT 8 Posted March 19, 2015 Share Posted March 19, 2015 I would like to make sure at least two Inventory Upgrades are available. trying access a slot the robot does not have causes an error /bin/components.lua inventory returns nothing, so component.isAvailable() will not work. I was hoping something like robot.chestCount() existed, but not yet. Any ideas out there? Quote Link to post Share on other sites
0 dgelessus 26 Posted March 19, 2015 Share Posted March 19, 2015 trying access a slot the robot does not have causes an error Does "error" mean a normal Lua error (where you get a traceback) or a blue screen/OS crash? In the first case you can use the standard Lua `pcall` function to check whether the function ran successfully and catch any potential errors. Quote Link to post Share on other sites
0 Molinko 43 Posted March 19, 2015 Share Posted March 19, 2015 Should be the first method on the component wiki page. http://ocdoc.cil.li/component:inventory_controller component.inventory_controller.getInventorySize(#side)-- inv side number. This is sides.back for a robots own internal inventory I think Quote Link to post Share on other sites
0 MaximilianVINCENT 8 Posted March 19, 2015 Author Share Posted March 19, 2015 Thanks to sangar, I learned something new! I posted the robot.chestCount() as a suggestion on the bug tracker and he came back with robot.inventorySize(). Testing showed that component.inventory_controller.getInventorySize(sides.back) is an unsupported side and error. I actually expected that to work, but I am glad I do not need yet another Upgrade (Those Tier 3 Robots are expensive). dgelessus, it was printing a reason for the error and a stack traceback. I would call the blue screen a crash. Procedure calls are something I have no experience in yet, but do need to eventually learn. That and signals and events are still mysterious to me. The only thing to do now is to create a login for the wiki and add this little beauty. Thank you for the help, guys! Quote Link to post Share on other sites
0 dgelessus 26 Posted March 19, 2015 Share Posted March 19, 2015 pcall is really straightforward. Say you have this function: function errorOnDemand(param) if param then error("You asked for it.") else return "some stuff", "you'll see this later" end end (if you pass it true, it errors out.) To run the function and catch errors, do this: success, returned1, returned2 = pcall(errorOnDemand, true) print(success, returned1, returned2) -- false you asked for it success, returned1, returned2 = pcall(errorOnDemand, false) print(success, returned1, returned2) -- true some stuff you'll see this later The first argument to pcall is the function you want to call, any further arguments are passed to that function. The first return value of pcall is true or false depending on whether it succeeded. If so, the remaining return values are those of the called function, otherwise the error message. Or in code form: -- Normal call ret1, ret2, ret3, etc = func(arg1, arg2, arg3, etc) -- Protected call success, ret1_or_error_message, ret2, ret3, etc = pcall(func, arg1, arg2, arg3, etc) Quote Link to post Share on other sites
0 Molinko 43 Posted March 19, 2015 Share Posted March 19, 2015 its been added to the wiki Quote Link to post Share on other sites
I would like to make sure at least two Inventory Upgrades are available.
I was hoping something like robot.chestCount() existed, but not yet.
Any ideas out there?
Link to post
Share on other sites