Jump to content

[Script Tutorial] Script initialization on game load


grey79

Recommended Posts

Erm...that doesn't look right.... :facepalm:

 

In step 2: in the Quest Data tab you should tick "Start Game Enabled" and "Run Once".

 

In step 3: in the Scripts tab, click the 'Add" button and then click "[New Script]". Type in the name "MyModsInstallScript" and extends "Quest". Then you should be able to pick "MyModsInstallScript" from the list. Right-click on the script and choose Edit Source. The script editor will open with a 1-line script (only the Scriptname in it). Delete the "Scriptname" line and then paste in the entire script code that grey79 posted earlier (including Scriptname), then click File->Save in the menu. It should compile without errors. Close the Script window.

 

That is it! You should save your plugin file, load it up in Skyrim and check that it works.

 

Note that Steps 4, 5, 6 and 7 are totally unnecessary!!! :wallbash:

 

Edit: oops, using Grey79's script won't work with the above method. You need to use the OnInit event rather than define an Init function. Use this code instead:

 

Scriptname MyModsInstallScript extends Quest

;===========================================

Event OnInit()
       ; My code runs here ...
       Debug.Notification("[MyModsInstallScript] has been installed")
EndEvent

;===========================================

Edited by steve40
Link to comment
Share on other sites

None of those work for me. There must be a way, there are so many mods out there which's scripts run in the background all the time. So, what's the solution to have a script running all the time, scanning for whatever.

 

Well, there are many ways to skin a cat. What is it that you are trying to do?

 

Below is an example of a script that will print a debug message on screen every 5 seconds. If you install it as per my earlier instructions it should run as soon as you start the game. Note that constantly running lots of update events or loops will degrade the game performance, just like running lots of background tasks in Windows is bad.

 

Scriptname MyModsInstallScript extends Quest

;===========================================

Event OnInit()
       ; My code runs here ...
RegisterForUpdate(5)
Debug.Notification("[MyModsInstallScript] has been installed")
EndEvent

;===========================================

Event OnUpdate()
; This block gets called by the game every 5 seconds...
; Do my cool stuff here.
      Debug.Notification("[MyModsInstallScript] The OnUpdate event has been called.")
EndEvent

;===========================================

Link to comment
Share on other sites

I got my script to work yesterday (finally, after many days). I didn't put it on the quest but on the quest alias (the player). One thing it does is checking for onitemadded and onitemremoved events. Does it take much performance to have a script running all the time checking for this events?
Link to comment
Share on other sites

I got my script to work yesterday (finally, after many days). I didn't put it on the quest but on the quest alias (the player). One thing it does is checking for onitemadded and onitemremoved events. Does it take much performance to have a script running all the time checking for this events?

 

No. Strictly speaking, scripts don't "run all the time" as they are not executable files. The game engine monitors specific scripts for specific event blocks when certain events happen on an object in-game. For example, when an actor picks up an item, the game automatically looks at the actor (Actor extends ObjectReference) to see if it has a script attached with an OnItemAdded block. It would have also checked the item for an OnActivate block as well, because you have to activate the item to pick it up. This happens whether you have added a script or not, afaik. If the game finds such a block, it runs it. Making a script literally run all the time would mean forcing it to run in a permanent loop, which would be a waste of CPU cycles. However, one way to do this might be to use a "while" statement, which would run in a loop while its condition is true. Info about while statement here: http://www.creationkit.com/Statement_Reference.

 

On the other hand, when you register for an update, for example, you are forcing the game to keep looking at your script periodically in addition to all the "regular" script monitoring that the game does, so this can also have an impact on the processing overhead. Similarly, if you apply lots of conditions on, for example, a Magic Effect or script by using the Target Conditions box of the CK, it can also degrade performance. However, the Target Conditions is another good way to constantly monitor for something and then activate an Ability, spell or script etc. For example, you can create a Magic Effect of the "Constant Effect" type with the Effect Archetype "Script" and attach a script (extending ActiveMagicEffect) to it. The script would have an OnEffectStart block (and possibly an OnEffectFinish block). Then make a new constant effect Ability that uses this Magic Effect. Then if you double-click the Magic Effect in the Ability form, the Conditions box will appear. You could add, for example, a condition to check if the actor is attacking (IsAttacking == 1, Run on subject). Then add this new ability to your actor (eg. adding it to the SpellList on the actor's form is one way to do this). Now what will happen is that the "ability/spell" (script) will activate only when the actor attacks something, and then it will deactivate when the actor stops attacking. So you could, for example, have some code in your OnEffectStart block of the script to play a funny sound. The sound would only play when the actor tries to hit something. The reason why that had to be done in such a complicated way is that there is no "OnAttack" event in Papyrus! On the other hand, let's say you wanted your script to run every time your actor is hit by another actor. In this case, there is such a thing as an "OnHit" event, so all you need to do is to simply attach a script (extending actor) to your actor with an OnHit block. In this case there's no need for all the fancy stuff like in the last example.

 

So getting your script code to run is really a matter of figuring out which type of script you need to extend, and which type of form/object you need to attach the script to, which all depends on what sort of event or functions you are needing to run, and when you want them to run. I hope this all makes sense.

 

Cheers

Edited by steve40
Link to comment
Share on other sites

Edit: Actually, no, it's not working. I mis-understood your problem.

 

Yeah, it remains greyed out for me too. How do we fix that?

 

And if you click OK, without the Papyrus fragment thingy, the CK crashes on me.

 

If it's greyed out, you simply need to select the log entry with your mouse.

Link to comment
Share on other sites

Aye, yer right. Had to create a new quest, click OK, then re-open it and then do your steps :3

 

Edit: I get these errors when I click compile :

 

C:\Program Files\The Elder Scrolls V Skyrim\Data\Scripts\Source\temp\QF_AAAHungerQ_010012D1.psc(8,0): variable kmyQuest is undefined
C:\Program Files\The Elder Scrolls V Skyrim\Data\Scripts\Source\temp\QF_AAAHungerQ_010012D1.psc(8,9): none is not a known user-defined type
No output generated for QF_AAAHungerQ_010012D1, compilation failed.

 

What should I do? :P

 

Ignore the error and close the form. If you look in your Scripts folder, you'll find that the script fragment has actually compiled.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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