Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
MaximilianVINCENT

Testing for number of slots available

Question

5 answers to this question

Recommended Posts

  • 0
  • 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.

Link to post
Share on other sites
  • 0

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!

Link to post
Share on other sites
  • 0

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)
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.