Zulfilham Posted March 30, 2022 Posted March 30, 2022 So, this is the case. I have file one.lua: loadfile("two.lua")(); But in file two.lua, I want to know which script is executing it. two.lua: -- how to get file name one.lua from here How?
MarioRossi93i Posted March 31, 2022 Posted March 31, 2022 file 1: local filename = 'foo1' loadfile('foo2')() file 2: print(filename)
Zulfilham Posted March 31, 2022 Author Posted March 31, 2022 6 hours ago, MarioRossi93i said: file 1: local filename = 'foo1' loadfile('foo2')() file 2: print(filename) That'll only work if we know the variable name in advance. But our script could also be run by other unknown scripts or maybe we just want dynamic value, that would be more useful than hard-coding it.
MarioRossi93i Posted March 31, 2022 Posted March 31, 2022 file1: loadfile('file2')() file2: print( debug.getinfo(3,'S').short_src )
Zulfilham Posted April 1, 2022 Author Posted April 1, 2022 4 hours ago, MarioRossi93i said: file1: loadfile('file2')() file2: print( debug.getinfo(3,'S').short_src ) Thanks, it works. But could you also explain how it works? Or might be references? Edit: I did my homework, I found a good reference explaining it.
darklinux Posted April 1, 2022 Posted April 1, 2022 @Zulfilham Could you share your reference? I'm not finding much on the stack levels, why does the value 3 work? Stack level 0 always corresponds to the debug.getinfo call Stack level 1 would be the function calling debug.getinfo Stack level 2 ? Stack level 3 would be the function that loaded the file? I guess I'll debug and find out
Zulfilham Posted April 1, 2022 Author Posted April 1, 2022 Just now, darklinux said: @Zulfilham Could you share your reference? I'm not finding much on the stack levels, why does the value 3 work? Stack level 0 always corresponds to the debug.getinfo call Stack level 1 would be the function calling debug.getinfo Stack level 2 ? Stack level 3 would be the function that loaded the file? I guess I'll debug and find out https://www.lua.org/pil/23.1.html
Question
Zulfilham
So, this is the case. I have file one.lua:
But in file two.lua, I want to know which script is executing it.
two.lua:
-- how to get file name one.lua from here
How?
7 answers to this question
Recommended Posts
Archived
This topic is now archived and is closed to further replies.