Author Topic: Debug.info with pcall question?  (Read 143 times)

Zefk

  • Sr. Member
  • ****
  • Posts: 492
  • Just helping Solarus
    • View Profile
    • Zelzec Business
Debug.info with pcall question?
« on: May 07, 2017, 06:31:01 am »
My problem is that I do not know how to pcall my function "Error()". Error() needs to be inside the entity:example() to work.

Lib.lua
Code: Lua
  1. local metatable_entity = sol.main.get_metatable("custom_entity")
  2.  
  3. function metatable_entity:example()
  4.   Error()
  5. end
  6.  
  7. function Error()
  8.     local level_1 = debug.getinfo(1) --Error() is level 1
  9.     local level_2 = debug.getinfo(2) --Name of function being called from is level 2
  10.     local level_3 = debug.getinfo(3) -- Return from where ever the function was declared is level 3
  11.     print ("-----------------------------------------------------------------------------------------------------------")
  12.     print(level_3.short_src.." => "..level_1.name.. " was called by function [".. level_2.name.. "] at line " .. level_3.currentline)
  13.     print ("-----------------------------------------------------------------------------------------------------------")
  14. end
  15.  

Entity script:

Code: Lua
  1. entity:example()

Output:
Code: [Select]
-----------------------------------------------------------------------------------------------------------
[string "entities/follower.lua"] => Error was called by function [example] at line 1
-----------------------------------------------------------------------------------------------------------
« Last Edit: May 07, 2017, 06:38:37 am by Zefk »