Problems with debug.getinfo()

Place to get help with not working mods / modding interface.
Pi-C
Smart Inserter
Smart Inserter
Posts: 1759
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Problems with debug.getinfo()

Post by Pi-C »

Suppose I have a table of functions indexed by name:

Code: Select all

get_name = function() 
	print("Entered function "..debug.getinfo(2).name) 
end	

a = {
  test_a = function(x) get_name() print(x) end,
  test_b = function(x) get_name() print(2*x) end,	
}
It seems that debug.getinfo will have different results depending on how it is called:

Code: Select all

-- As expected:
> a.test_a(123)
Entered function test_a
123

-- As expected:
> a["test_a"](123)
Entered function test_a
123

-- Unexpected:
> f = "test_a"
> a[f](123)
Entered function ?
123
This prevents meaningful output when looping over all functions:

Code: Select all

> for f_name, f in pairs(a) do a[f_name](123) end
Entered function ?
123
Entered function ?
246
Same thing with getting the function name by concatenating strings:

Code: Select all

> x = "a"
> a["test_"..x](123)
Entered function ?
123
Is there any way to get at the correct function name in those cases as well?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!
Post Reply

Return to “Modding help”