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

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
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) [Select]
local metatable_entity = sol.main.get_metatable("custom_entity")

function metatable_entity:example()
  Error()
end

function Error()
    local level_1 = debug.getinfo(1) --Error() is level 1
    local level_2 = debug.getinfo(2) --Name of function being called from is level 2
    local level_3 = debug.getinfo(3) -- Return from where ever the function was declared is level 3
    print ("-----------------------------------------------------------------------------------------------------------")
    print(level_3.short_src.." => "..level_1.name.. " was called by function [".. level_2.name.. "] at line " .. level_3.currentline)
    print ("-----------------------------------------------------------------------------------------------------------")
end

Entity script:

Code: ( lua) [Select]
entity:example()
Output:
Code: [Select]
-----------------------------------------------------------------------------------------------------------
[string "entities/follower.lua"] => Error was called by function [example] at line 1
-----------------------------------------------------------------------------------------------------------