[Solved]pcall metatable?

Started by Zefk, May 05, 2017, 09:30:23 AM

Previous topic - Next topic


If the function fails, then I'd like to give a custom message.

Code ( lua) Select
function myfunction()
   n = n/nil
end

if pcall(myfunction) then
   print("Success")
else
   print("Failure")
end

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.

May 05, 2017, 12:35:26 PM #4 Last Edit: May 05, 2017, 12:39:06 PM by Zefk
Is it possible to pcall something like the following?

example.lua

Code ( lua) Select


local metatable = sol.main.get_metatable("custom_entity")

function metatable:example()
  --some code
end



main.lua
Code ( lua) Select
require(scripts/example.lua)

custon entity:

Code ( lua) Select
entity:example()

if pcall (example) then

  print("success")

else

   print("fali")

end


May 05, 2017, 01:31:16 PM #5 Last Edit: May 05, 2017, 01:33:16 PM by Diarandor
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.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

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) Select

function metatable:example()
  --some code
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) Select
my_entity:example()
and not like this
Code (lua) Select
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) Select

local function f()
  my_entity:example()
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) Select

local success = pcall(f)
if success then
  ...
else
  ...
end


You can even keep it anonymous if you prefer:
Code (lua) Select

local success = pcall(function()
  entity:example()
end)
if success then
  ...
else
  ...
end

May 05, 2017, 03:10:23 PM #7 Last Edit: May 07, 2017, 05:30:09 AM by Zefk
@Christopho
That was a perfect explanation. Thank you Christopho!