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

0 Members and 1 Guest are viewing this topic.

Zefk

  • Hero Member
  • *****
  • Posts: 535
  • Just helping Solarus
    • View Profile
    • Zefk Design
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
-----------------------------------------------------------------------------------------------------------