I have a doubt about variables declared on a function that is then used as an event handler. Do their content persist or is it reset every time the event handler is used?

#1
Posted 04 April 2020 - 11:52 PM

#2
Posted 05 April 2020 - 01:06 AM

This sounds like it is really a question about "variable scope". Doesn't matter if you are talking about a "function" or an "event handler". But I think you are asking more about the nature of a "parameter".
Speaking very generally (as there are always exceptions), a variable passed as a "parameter" to another function is a "value" rather than the variable itself. So the receiving function (your event handler) gets the "value" of the passed parameter variable which it "initializes" to it's own internally declared "parameter variable". The original variable is unchanged and merely a vehicle to convey the value. (Which is not to say it can't get assigned the "returned value" of the event handler if set up that way.)
Think of variables like "taxis": they carry a passenger (value) around from one location to another, but are not the what the location cares about. Only their contents, which can be (and likely are) different each trip. But, each taxi is somewhat specialized and is expected to have a "passenger" of a particular nature (e.g. "lawyer" = "long integer", "singer" = "string", etc.).
-Dubious-
#3
Posted 05 April 2020 - 01:32 AM

#4
Posted 05 April 2020 - 12:01 PM

Hmm. I'm of the impression that it depends on whether or not the variable is defined before or after the "Begin" block, but I can't find a reference off-hand so don't count on that without testing. If you want to ensure persistence confined to the Event Handler, I would suggest you look into "Auxiliary Variables".
But as for event handlers themselves, according to Docta Sax' tutorial:
Event handlers are UDFs that you register with NVSE to be called whenever [and before] their associated event happens. These handlers are then set for the rest of the game session; even if you load a different save, they are still valid until you actually restart the game.
-Dubious-
#5
Posted 05 April 2020 - 12:22 PM

Thanks, I might just try printing the values to see if they change correctly. That should clear that out for me.
#6
Posted 05 April 2020 - 01:24 PM

I'm curious about this as well. I'll do a test when I can be faffed, if you didn't get to it before me.
#7
Posted 05 April 2020 - 01:26 PM

Do you mean a variable as it relates to the "First" & "Second" of an eventhandler ?
So basically you capture a variable , then plug that variable into the eventhandler for 1 or 2 ?
Which honestly I didn't know that was possible if is the case.
Or is this a variable condition as a prefix trigger and/or suffix count of the trigger ? (something outside 1 & 2 )
As a mention to expand on what dubious said. Variable storage is possible without scripts or globals .
By using a container to put stuff in or take out. Then check it's item count , or even what is in them.
Something I learned from PixleHate a few years back ... thought it worth a mention as a means to keep a persistent variable where instances of scripts variables get reset from 1 to another instance of the same script.
#9
Posted 05 April 2020 - 09:13 PM

What's an example of a local variable ?
And question I am not clear on.
When an EventHandler fires ... it needs reset in order to trigger again ?
#10
Posted 06 April 2020 - 01:48 PM

What's an example of a local variable ?
Any variable defined in a script at the time it runs. See the link on "variable scope" in my initial reply. See also "Global variables" in the GECKWiki.
Where it gets murky is "inheritance", because a "child" script called from another script will "inherit" variables of the parent script. This is not the same as a "global" variable which is available to any script regardless of whose child it is.
And question I am not clear on.
When an EventHandler fires ... it needs reset in order to trigger again ?
Per the ref GamerRick found: a UDF is a "one frame" script; and an EventHandler is just a particular kind of UDF. So it automatically is reset each time it is called/triggered.
-Dubious-
Edited by dubiousintent, 06 April 2020 - 01:49 PM.
Also tagged with one or more of these keywords: nvse
