qwertypol012 Posted June 19, 2019 Posted June 19, 2019 First of all, i'm an ultra newbie in Papyrus Scripting, hence i'm asking. I recently found some mods which utilize some event headers as functions (or how should i put it correctly? I don't actually know what's the correct terms anyway). For example:Function OnInit() ;function's stuff EndFunction Function OnUpdate() ;function's stuff EndFunction Function OnAnimationEvent(objectreference akSource, String asEventName) ;function's stuff EndFunctionSo, this works? and what are the uses of this form of coding?
ReDragon2013 Posted June 19, 2019 Posted June 19, 2019 Afaik there is no different between both variantsFunction OnInit() ;function's stuff EndFunction Function OnUpdate() ;function's stuff EndFunction Function OnAnimationEvent(Objectreference akSource, String asEventName) ;function's stuff EndFunctionEVENT OnInit() ENDEVENT EVENT OnUpdate() ENDEVENT EVENT OnAnimationEvent(Objectreference akSource, String asEventName) ENDEVENTbecause all of these are native methods, hardcoded to papyrus engine. The different comes true if you want to use your own function as event. For example: I made a new function to "critter.psc" and called this by event syntax.EVENT OnCritterStop(Int i) ; stuff here ENDEVENTAfter some time of reasearching I found out it would be better to use the normal way as function syntax.FUNCTION OnCritterStop(Int i) ; stuff here ENDFUNCTIONWhy? Function do not clash each other, but events are able to run at the same time. This could lead to mistakes.
npdogg Posted June 21, 2019 Posted June 21, 2019 The use of this form of coding is to make it harder to understand and more prone to programming mistakes. Even if the compiler will recognize either keyword, you should use "Event" for events and "Function" for user-defined functions. It makes it easier to understand what your script is supposed to do.
qwertypol012 Posted June 22, 2019 Author Posted June 22, 2019 (edited) Reveal hidden contents On 6/19/2019 at 7:21 PM, ReDragon2013 said: Afaik there is no different between both variantsFunction OnInit() ;function's stuff EndFunction Function OnUpdate() ;function's stuff EndFunction Function OnAnimationEvent(Objectreference akSource, String asEventName) ;function's stuff EndFunctionEVENT OnInit() ENDEVENT EVENT OnUpdate() ENDEVENT EVENT OnAnimationEvent(Objectreference akSource, String asEventName) ENDEVENTbecause all of these are native methods, hardcoded to papyrus engine. The different comes true if you want to use your own function as event. For example: I made a new function to "critter.psc" and called this by event syntax.EVENT OnCritterStop(Int i) ; stuff here ENDEVENTAfter some time of reasearching I found out it would be better to use the normal way as function syntax.FUNCTION OnCritterStop(Int i) ; stuff here ENDFUNCTIONWhy? Function do not clash each other, but events are able to run at the same time. This could lead to mistakes. This actually makes sense. Though i'm interested if anyone has more informations about it. @ npdoggThe fact is that (some also based on my strong assumption), such method is used by many mods (some are quite popular), such as Enhanced Blood Textures, Engarde (recently new combat mod), and probably chesko's mods (because the author of a mod i found using this method said that he used a method from chesko's Frostfall in his script). Edited June 22, 2019 by qwertypol012
npdogg Posted June 22, 2019 Posted June 22, 2019 On 6/22/2019 at 1:18 PM, qwertypol012 said: The fact is that (some also based on my strong assumption), such method is used by many mods (some are quite popular), such as Enhanced Blood Textures, Engarde (recently new combat mod), and probably chesko's mods (because the author of a mod i found using this method said that he used a method from chesko's Frostfall in his script). Well there's a difference between Chesko doing it and someone else who looked at Chesko's scripts doing it. I can't think of any useful reason to use the Event keyword for a function. Only one thread of execution can be in a script at a time, I don't see how changing the keyword from Function to Event would change that.
cdcooley Posted June 22, 2019 Posted June 22, 2019 There is absolutely no difference between Function and Event once the script is compiled so technically you can use Function everywhere without any problems. The use of Event is purely a cosmetic feature to make the bits of code called by the game engine itself stand out. The Champollion script decompiler is used by many people to get source code for mods that only included the compiled scripts. That program generates scripts that use Function everywhere instead of Event. My guess is that this trend started when people used that tool and copied its results into their own mods.
Rizalgar Posted June 23, 2019 Posted June 23, 2019 (edited) TL;DR comments. The differences between using Events and Functions of the same nature is that you can choose when to call said functions. AFAIK, an Event OnItemAdded will fire every time an item is added, however the Function OnItemAdded would be fired when the script calls it to fire. In example:Function InitiateAdd(references here) ;A player created function to determine when OnItemAdded is fired. OnItemAdded() ;Calls the function, starting it EndFunction Function OnItemAdded() ;The function is now initiated EndFunctionI may be incorrect, as a matter of fact I'm certain I am, but that is the way it seems to me. I've done enough coding to know the different between an event and function, but I'm still a derp. Edit - I mostly work with my own functions that call to other functions, I've never actually tried calling an OnItemAdded function on itself alone, does it fire every time an item is added? Feel free to chime in and correct me on my comment. Edited June 23, 2019 by Rizalgar
RichWebster Posted June 24, 2019 Posted June 24, 2019 I use the Event header for things that I use as events. For example when a quest starts and I want it to run some logic I'll created a custom Event like Startup() in my quest script, and just call it like a function from my stage fragment. Like so: ;Fragment kmyQuest.Startup() ;Quest script Event Startup() ;Do some cool stuff EndEvent Just remember not to use event names that already exist in the script you're extending, for example don't make your own OnActivate() event in a script that extends ObjectReference.
npdogg Posted June 24, 2019 Posted June 24, 2019 On 6/24/2019 at 5:07 PM, RichWebster said: I use the Event header for things that I use as events. For example when a quest starts and I want it to run some logic I'll created a custom Event like Startup() in my quest script, and just call it like a function from my stage fragment. Like so: ;Fragment kmyQuest.Startup() ;Quest script Event Startup() ;Do some cool stuff EndEventJust remember not to use event names that already exist in the script you're extending, for example don't make your own OnActivate() event in a script that extends ObjectReference. Yeah, this is the kind of thing that is not great to see. At first glance it makes it look like your script is listening for the "Startup" event, when in fact there is no such thing. More confusion, less readability. Ultimately the fault lies with the language design in being too permissive of bad syntax.
RichWebster Posted June 24, 2019 Posted June 24, 2019 On 6/24/2019 at 5:28 PM, npdogg said: On 6/24/2019 at 5:07 PM, RichWebster said: I use the Event header for things that I use as events. For example when a quest starts and I want it to run some logic I'll created a custom Event like Startup() in my quest script, and just call it like a function from my stage fragment. Like so: ;Fragment kmyQuest.Startup() ;Quest script Event Startup() ;Do some cool stuff EndEventJust remember not to use event names that already exist in the script you're extending, for example don't make your own OnActivate() event in a script that extends ObjectReference.  Yeah, this is the kind of thing that is not great to see. At first glance it makes it look like your script is listening for the "Startup" event, when in fact there is no such thing. More confusion, less readability. Ultimately the fault lies with the language design in being too permissive of bad syntax.It is listening for a startup event, and it's called from the fragment. It's probably personal preference, but I like to organise my code in how it behaves and is used.
Recommended Posts