Important Site Docs
Articles
Skyrim Papyrus Tutorial: How to use OnUpdate
- Details
- Category: The Elder Scrolls 5: Skyrim Modding Guides
- Last Updated on Wednesday, 21 March 2012 01:57
- Written by Giskard
- Hits: 2889
Skyrim Papyrus Tutorial: How to use OnUpdate
Introduction
OnUpdate is basically the new version of the old Gamemode function from the Oblivion days. Those of you who remember Gamemode from the older Elder Scrolls games will know everything after Gamemode got executed roughly once every 5 seconds over and over again. OnUpdate does the same thing except you need to declare how often you want it to run everything inside the OnUpdate event first and that requires you make a function and an event inside script and call the function from somewhere else.
That is what we will do in this tutorial.
To use OnUpdate you need to know how to call Functions so its best reading through our "Skyrim Papyrus Tutorial: Functions Explained" tutorial first found in this section of the website. I have also given an brief overview of functions and how to use them in this tutorial for those who just need a quick reminder of how they work.
Quest Scripts and Script Properties
Lets assume you have a quest and you want to add an event to it that will cause the script to run every X amount of seconds. First you would add a script, so you open your quest and go to the last tab which should be scripts and then click ADD and select New Script and click OK. Now we give our script a new name, lets call it myquestscript, the extends part should say quest already.
When your done press OK.
Now we need to make our questscript available to the rest of quest so when myquestscript appears under script name, select it and click properties. Now click add Property and change the type to Quest and give it a name like myquest for short. When the property appears, select it and then click Edit Value and then use the pick object section to select the quest that the script was added too. It should be the quest your editing right now.
From now on we can refer to this script as myquest anywhere in the scripts result boxes for this quest, which is handy because we need to use an End box to set the update period for the OnUpdate Event later.
Do not forget, this is explained in detail in our other tutorial "Skyrim Papyrus Tutorial: Functions Explained".
Editing your Quest script.
Now we need a function to set up the update timer, a function like this should do the trick.
Function regularchecks()
RegisterForUpdate(5)
endFunction
IF we just put RegisterForUpdate(5) in the script directly we would get an error so it has to be inside a function, but it will not run automatically inside a function so we need to call the regularchecks() function from else where in order to run the RegisterForUpdate(5) command. Also regularchecks() is a name I made up, you could have called it BigMooCows() and it would still work the same way. More on that later, first lets set up the event.
A simple test event would look like this.
Event OnUpdate()
Debug.MessageBox("Updating")
endEvent
Every 5 seconds a box would appear in the game that simply says "Updating". You can use this to test if your OnUpdate Event is actually working later.
When you know its working comment out the Debug line like this to stop the message popping up all the time.
Event OnUpdate()
;Debug.MessageBox("Updating")
endEvent
What our script looks like now.
This is our new script, with comments.
Scriptname myquestscriptextends Quest
; do not forget, anything after the ; is a comment and is ignored by the game.
Quest Property myquest Auto ; this is the property we set up.
RegisterForUpdate(5) ; this means check the OnUpdate Event in this script every 5 seconds.
; This next function is the function we need to call from else where to boot up out little OnUpdate Event
Function regularchecks()
RegisterForUpdate(5)
endFunction
; This is the event that will be run every 5 seconds, because we commented out the Debug line
; This event will do a whole lot of nothing every 5 seconds until you put something in it.
Event OnUpdate()
;Debug.MessageBox("Updating")
endEvent
Setting up a Topic from which we can run our RegisterForUpdate() request.
We must now find a way to call our regularchecks() so it kick starts our OnUpdate Even. You can do this from triggers, topics, by opening chests or topics in a quest, all of which are beyond the scope of this tutorial.
So I'll run through some examples of how to run this function.
If you wanted a death of an NPC to start it, you would add a script to the NPC that contained something like this.
Event OnDeath(Actor akKiller)
(myquest as myquestcript).regularchecks()
EndEvent
If you wanted to run it from a chest when it gets opened, you would add a script to the chest and add something like this to it.
Event OnActivate(ObjectReference akActionRef)
(myquest as myquestcript).regularchecks()
endEvent
If you wanted to run it when cell first loaded, you would add it to a script in that cell something like this.
Event OnLoad()
(myquest as myquestcript).regularchecks()
EndEvent
Basically what ever event you want to use, you just put this in the middle it of it somewhere.
(myquest as myquestcript).regularchecks()
Remember though, if you wanted to call this from a topic on the quest that held our regularchecks() function, you would add this next line to the result box instead.
(GetOwningQuest() as myquestcript).regularchecks()
The difference between how a function is used from a totally different script and how a quest uses its own functions is clearly explained in the "Skyrim Papyrus Tutorial: Functions Explained".
Final comments.
If you left the Debug command uncommented and triggered our little event, you would start to see "Updating" appearing in a box you had to OK every 5 seconds until you quit the game and commented out that line in your mod. This makes for a good test of your code. Once your happy the event is working, insert your own stuff in between the "Event OnUpdate" part of your script like this
Event OnUpdate()
;put your code here that you want to run every X amount of seconds.
endEvent
Also remember the 5 in RegisterForUpdate(5) means run our event every 5 seconds, you can set it to what ever you want, even 1 second but the game may not be that accurate when calling your event, so best not reply on total accuracy in the timing department.
Login Form
Forum and Website Logins
Please be aware that the forum and the main website are 2 completely different logins.
Skyrim Menu
Fallout New Vegas Menu
Fallout 3 Menu
Oblivion Menu
Game Guides
Misc Menu
Who is online
We have 45 guests and 3 members online
Members Online Today
Total Members Online Today: 8FatGiant Gwynfael sonicfast01 Valin777 42wolfe42 nikki191 oblivionmaster310 Vagn














