Jump to content




Getting caller function


  • You cannot reply to this topic
11 replies to this topic

#1 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 07 November 2015 - 12:03 PM

How can you get the place where your function was called? (Like in error messages)
I know I could pcall(error) but for that I need to know how many callers the function has.

#2 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 07 November 2015 - 12:29 PM

error(msg,2) This allows you to blame the function that called the function that errored.

#3 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 07 November 2015 - 12:34 PM

View PostCreator, on 07 November 2015 - 12:29 PM, said:

error(msg,2) This allows you to blame the function that called the function that errored.
Thanks for replying, but I said in the original post that pcall("test",2) is just the caller. But I need the caller's caller, the caller's caller's caller etc.

#4 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 07 November 2015 - 01:16 PM

You can blame whatever layer you want.

error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...

I assume the same applies to the pcall thing. I wouldn't know.

#5 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 07 November 2015 - 01:21 PM

View PostKingofGamesYami, on 07 November 2015 - 01:16 PM, said:

You can blame whatever layer you want.

error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...

I assume the same applies to the pcall thing. I wouldn't know.

Wouldn't the code be aborted before the other errors are called?

#6 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 07 November 2015 - 01:35 PM

View PostCreator, on 07 November 2015 - 01:21 PM, said:

View PostKingofGamesYami, on 07 November 2015 - 01:16 PM, said:

You can blame whatever layer you want.

error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...

I assume the same applies to the pcall thing. I wouldn't know.

Wouldn't the code be aborted before the other errors are called?

That's why you have to use pcall.

#7 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 07 November 2015 - 01:37 PM

View PostH4X0RZ, on 07 November 2015 - 01:35 PM, said:

--snip--
That's why you have to use pcall.

This makes sense! It gives you already two levels.

#8 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 07 November 2015 - 02:42 PM

So you can do any number?
(Thanks, but what happens if I try to pcall error with a very big level, like 500 or math.huge? Or negative?)
I will try it
Thanks for helping!

Edited by Konlab, 07 November 2015 - 02:43 PM.


#9 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 07 November 2015 - 03:02 PM

If you get above the number of layers, I believe it acts like 0 does - erring without a line number or file name.

#10 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 November 2015 - 10:05 PM

... which means you still need to know how many callers there were.

If you want every error to be able to produce a full trace then you'll need to pcall every single function, have those functions pcall every function they want to call, etc.

Lua has a debug library that (among other things) would deal with this for you, but that's not available within distributable copies of ComputerCraft. Still, it's entirely possible to set up your own Lua environment with it enabled.

#11 Creator

    Mad Dash Victor

  • Members
  • 2,168 posts
  • LocationYou will never find me, muhahahahahaha

Posted 07 November 2015 - 10:11 PM

You can use CCTweaks for the debug library.

#12 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 07 November 2015 - 10:11 PM

There is a program that does error tracing. http://www.computerc...__fromsearch__1





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users