Jump to content

[LE] How functions work?


Recommended Posts

Function test1()

Do something

EndFunction

 

Would this execute or would it need a

test1() statement to trigger the function I seen code that used states

 

ScriptName StatesExample extends ObjectReference

 

Auto State Inactive

Function Toggle()

GoToState("Active")

EndFunction

EndState

 

State Active

Function Toggle()

GoToState("Inactive")

EndFunction

EndState

 

Function Toggle()

; Empty function definition to allow Toggle to be defined inside states. More on this soon...

EndFunction

 

Event OnActivate(ObjectReference akActionRef)

Toggle()

EndEvent

 

 

I thought a function had to be called to run..can someone explain how these functions are running without being called?

Link to comment
Share on other sites

Functions have to be called by something in order to run. What does the calling does not have to be inside the same script; this is most obvious when you write your own code and call the pre-built functions that shipped with the game. As a result it can sometimes be non-obvious what is calling which functions.

 

The game will automatically call a certain special class of function, called Events. They are identical to functions in nearly every respect as far as working with Papyrus goes; the key difference is that the Events in the base script objects will be called by the game engine itself -- not any particular Papyrus script -- when their triggering event happens. They are your entry points into getting the game to do things with scripts.

 

In the example you provided, the OnActivate() event is called, by the game, when you try to activate whatever object the script is attached to (it can also be called manually by some script). Could be an NPC, a chest, whatever. That is what then calls the Toggle() function. If the script is in the Inactive state, that call will use the version of Toggle() defined for that state, and if it's in Active, it will use the one from the Active state instead. All that changes between them is which state the function will send the script to -- it's acting as a switch. Or, more precisely, as a toggle :)

Link to comment
Share on other sites

It's the OnActivate event handler which is then calling Toggle. OnActivate is an event sent to the script via the game engine event model when the object to which the script is attached is activated by an actor.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...