UnvalidUserName Posted April 4, 2020 Share Posted April 4, 2020 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? Link to comment Share on other sites More sharing options...
dubiousintent Posted April 5, 2020 Share Posted April 5, 2020 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- Link to comment Share on other sites More sharing options...
UnvalidUserName Posted April 5, 2020 Author Share Posted April 5, 2020 I'm actually trying to ask if the local variables of the script persist, not the parameters. I'm unsure if the event handler is only instanced once and then reused every time or if it's reinstanced every time. Link to comment Share on other sites More sharing options...
dubiousintent Posted April 5, 2020 Share Posted April 5, 2020 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- Link to comment Share on other sites More sharing options...
UnvalidUserName Posted April 5, 2020 Author Share Posted April 5, 2020 Thanks, I might just try printing the values to see if they change correctly. That should clear that out for me. Link to comment Share on other sites More sharing options...
FiftyTifty Posted April 5, 2020 Share Posted April 5, 2020 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. Link to comment Share on other sites More sharing options...
Mktavish Posted April 5, 2020 Share Posted April 5, 2020 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. Link to comment Share on other sites More sharing options...
GamerRick Posted April 5, 2020 Share Posted April 5, 2020 Finally found an answer in a tutorial posted on an adult site LoversLab: Yes, local variables are reset every time you call a UDF again. It's a one-frame only script, and when it's done, it's done Link to comment Share on other sites More sharing options...
Mktavish Posted April 5, 2020 Share Posted April 5, 2020 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 ? Link to comment Share on other sites More sharing options...
dubiousintent Posted April 6, 2020 Share Posted April 6, 2020 (edited) 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 April 6, 2020 by dubiousintent Link to comment Share on other sites More sharing options...
Recommended Posts