Jump to content

Photo

Persistence of Variables in Event Handlers

nvse

  • Please log in to reply
9 replies to this topic

#1
UnvalidUserName

UnvalidUserName

    Enthusiast

  • Members
  • PipPip
  • 246 posts

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?



#2
dubiousintent

dubiousintent

    Resident poster

  • Premium Member
  • 8,878 posts

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
UnvalidUserName

UnvalidUserName

    Enthusiast

  • Members
  • PipPip
  • 246 posts
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.

#4
dubiousintent

dubiousintent

    Resident poster

  • Premium Member
  • 8,878 posts

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
UnvalidUserName

UnvalidUserName

    Enthusiast

  • Members
  • PipPip
  • 246 posts

Thanks, I might just try printing the values to see if they change correctly. That should clear that out for me.



#6
FiftyTifty

FiftyTifty

    Resident poster

  • Members
  • PipPipPipPipPip
  • 2,794 posts

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
Mktavish

Mktavish

    Pleb

  • Premium Member
  • 2,962 posts

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.



#8
GamerRick

GamerRick

    Faithful poster

  • Supporter
  • PipPipPipPip
  • 1,086 posts

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

 



#9
Mktavish

Mktavish

    Pleb

  • Premium Member
  • 2,962 posts

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
dubiousintent

dubiousintent

    Resident poster

  • Premium Member
  • 8,878 posts

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

IPB skins by Skinbox
Page loaded in: 1.165 seconds