Page 1 of 1

Getting the script own name

Posted: Tuesday 15 August 2017 10:04
by jeanclic
Hello everyone,

I am currently beginning to move all of my "standard" lua scripts to dzVents 2.2.0, and I am stuck on one thing : How get my script name...

For the story, I use a "personal" logging function, that I will use in dzVents too because I designed it to handle the "debug" mode for only one script, and not all of them as it is when you use the dzVents log level.

So I am using the helpers as explained in the wiki, in the "global_data.lua" file.

I would like to know if I can get the name of the script triggering this function, so I could write it on the log.

For now, I write in each script the following lines :

Code: Select all

local path = debug.getinfo(1,'S').short_src
while string.find(path, '/') do path = string.sub(path, string.find(path, '/')+1) end
local File = tostring(string.sub(path, 1, string.len(path)-4))
and then I use "File" as an argument when calling my function...

I have seen in "EventHelpers.lua" that the name of the module currently executed is coming from "eventHandler.name". Is there a way to use this directly inside of my own function ?

Re: Getting the script own name

Posted: Tuesday 15 August 2017 11:06
by dannybloe
There's nothing for this currently but why not pass a name to your helper function instead?

Re: Getting the script own name

Posted: Tuesday 15 August 2017 11:57
by jeanclic
Hello Danny,
Thanks for answering so quickly !
I did not explained well, but I am actually passing the name (through my "File" variable) to my helper function. My concern is to remove those 3 lines that I put in each one of my scripts (so I do not care of miswriting the script name, or forgot to give a proper name each time I create a script).
I will keep it like that for now.

I have another question, maybe not in the good thread : is there a "middle" level of the scope of a variable ? -> when I used "standard" lua scripts, if I used a Global variable in 1 script, the next script did not keep the variable value. But, due to the total integration of the dzVents scripts in a single one, if I declare a Global in the first script, and re-declare it with another value in the last script, the first script will be executed with the var value set by the last one, and so the result is not the one expected in my code.
Knowing that, is there a way to declare a Global "Local" to only one script ?

Re: Getting the script own name

Posted: Tuesday 15 August 2017 12:27
by dannybloe
and how about setting a custom logging on the script itself. Isn't that enough? I'd say it does just that.

Re: Getting the script own name

Posted: Tuesday 15 August 2017 12:57
by dannybloe
jeanclic wrote: Tuesday 15 August 2017 11:57 I have another question, maybe not in the good thread : is there a "middle" level of the scope of a variable ? -> when I used "standard" lua scripts, if I used a Global variable in 1 script, the next script did not keep the variable value. But, due to the total integration of the dzVents scripts in a single one, if I declare a Global in the first script, and re-declare it with another value in the last script, the first script will be executed with the var value set by the last one, and so the result is not the one expected in my code.
Knowing that, is there a way to declare a Global "Local" to only one script ?
That's exactly why you should never use globals in the first place because you never know if it causes undesired side-effects. It is considered as a very bad practice in programming. Always use local when defining variables in your script. Always.. unless you have a very very good reason to use globals.

My two cents...

Re: Getting the script own name

Posted: Tuesday 15 August 2017 16:51
by jeanclic
dannybloe wrote: Tuesday 15 August 2017 12:27 and how about setting a custom logging on the script itself. Isn't that enough? I'd say it does just that.
Yes ! you are obviously right ! I'd see that but I wasn't gone far enough in my readings ! I will definitely change my habits fort this logging option !

OK for the scope of the variables : that is exactly how I see "best practice", but you know, between words and acts ... ;)