Author Topic: [Solved]pcall metatable?  (Read 311 times)

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
[Solved]pcall metatable?
« on: May 05, 2017, 09:30:23 am »
How does a pcall or xpcall error message work with a metatable?
http://www.solarus-games.org/doc/latest/lua_api_main.html#lua_api_main_get_metatable
« Last Edit: May 05, 2017, 02:55:10 pm by Zefk »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1023
    • View Profile
Re: pcall metatable?
« Reply #1 on: May 05, 2017, 10:01:00 am »
What do you mean?

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: pcall metatable?
« Reply #2 on: May 05, 2017, 10:44:17 am »
If the function fails, then I'd like to give a custom message.

Code: Lua
  1. function myfunction()
  2.    n = n/nil
  3. end
  4.  
  5. if pcall(myfunction) then
  6.    print("Success")
  7. else
  8.    print("Failure")
  9. end

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1023
    • View Profile
Re: pcall metatable?
« Reply #3 on: May 05, 2017, 11:21:31 am »
Your code is a correct way to catch errors.
But you were talking about metatables in your first message, what do you want to know exactly?

I don't know if it answers your question, but sol.main.get_metatable() returns nil if the parameter is an unknown type name. It will only throw an error if the parameter is missing or is not a string.

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: pcall metatable?
« Reply #4 on: May 05, 2017, 12:35:26 pm »
Is it possible to pcall something like the following?

example.lua

Code: Lua
  1.  
  2. local metatable = sol.main.get_metatable("custom_entity")
  3.  
  4. function metatable:example()
  5.   --some code
  6. end


main.lua
Code: Lua
  1. require(scripts/example.lua)

custon entity:

Code: Lua
  1. entity:example()
  2.  
  3. if pcall (example) then
  4.  
  5.   print("success")
  6.  
  7. else
  8.  
  9.    print("fali")
  10.  
  11. end
« Last Edit: May 05, 2017, 12:39:06 pm by Zefk »

Diarandor

  • Hero Member
  • *****
  • Posts: 753
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: pcall metatable?
« Reply #5 on: May 05, 2017, 01:31:16 pm »
Note that your "example" variable, inside pcall, is nil. And that space between pcall and the parenthesis may give an error, but I am not sure.
« Last Edit: May 05, 2017, 01:33:16 pm by Diarandor »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1023
    • View Profile
Re: pcall metatable?
« Reply #6 on: May 05, 2017, 02:05:09 pm »
Ok, so your question is actually independent from metatables or Solarus, it is about the passing a value of type function vs calling a function.
Code: Lua
  1. function metatable:example()
  2.   --some code
  3. end

This defines a field "example" (of type function) on the object "metatable", and not an "example" global value.

Which means that you can call it like this, assuming you  have an entity called my_entity:
Code: Lua
  1. my_entity:example()
and not like this
Code: Lua
  1. example() -- Does not work: example is nil

If you want to make a value of type function instead of doing the call entity:example(), just wrap the call it in a function:
Code: Lua
  1. local function f()
  2.   my_entity:example()
  3. end
Then it is a value of type function (and not a function call anymore, this is the trick!) so you can pass it to anything that wants a value of type function, like pcall, sol.timer.start, etc.
Code: Lua
  1. local success = pcall(f)
  2. if success then
  3.   ...
  4. else
  5.   ...
  6. end
  7.  

You can even keep it anonymous if you prefer:
Code: Lua
  1. local success = pcall(function()
  2.   entity:example()
  3. end)
  4. if success then
  5.   ...
  6. else
  7.   ...
  8. end
  9.  

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Re: [Solved]pcall metatable?
« Reply #7 on: May 05, 2017, 03:10:23 pm »
@Christopho
That was a perfect explanation. Thank you Christopho!
« Last Edit: May 07, 2017, 05:30:09 am by Zefk »