Getting a script to run when the game loads

Post » Mon Jun 18, 2012 2:38 pm

Is it possible to get a script to run once every time the game is loaded to check variables and quest states. (And act on them)

Or is it possible to attach a script to the player's onload event.
User avatar
james kite
 
Posts: 3460
Joined: Sun Jul 22, 2007 8:52 am

Post » Mon Jun 18, 2012 5:48 am

Is it possible to get a script to run once every time the game is loaded to check variables and quest states. (And act on them)

Or is it possible to attach a script to the player's onload event.

Make a new quest that starts every time you start the game -> tick the start game with option and choose type None so it dosnt get shown in your journal
Then attach your script to that quest and in the Oninit event register it for updates (RegisterForUpdate(time) ) and use Event OnUpdate for code use.
User avatar
Skivs
 
Posts: 3550
Joined: Sat Dec 01, 2007 10:06 pm

Post » Mon Jun 18, 2012 9:20 am

Thanks I thought the quests might be an easy way to do it but the options were a bit confusing. Does the quest OnInit Event fire every time you start the game?
User avatar
Rhysa Hughes
 
Posts: 3438
Joined: Thu Nov 23, 2006 3:00 pm

Post » Mon Jun 18, 2012 12:10 pm

Make a bGameLoadedGLOB global with its "Constant" flag ticked and value set to 1, then...
Spoiler
ScriptName YourQuestScript extends YourQuestScriptGlobalVariable Property bGameLoadedGLOB AutoEvent OnInit()	RegisterForUpdate(5)EndEvent;==========================Event OnUpdate()	If (bGameLoadedGLOB.GetValue() == 1)		bGameLoadedGLOB.SetValue(0) ; Will set itself back to 1 before loading ANY save		;Do stuff once when a save is loaded	EndIfEndEvent


"Constant" globals are written in stone
User avatar
Samantha Mitchell
 
Posts: 3459
Joined: Mon Nov 13, 2006 8:33 pm

Post » Mon Jun 18, 2012 12:41 am

No Oninit is a script Event that trigers when the script is executed. What you want to do is set the Quest itself as "start game with" so it triggers the script everytime you load the game back up.

OnInit() will only happen when the quest first starts or if it's reset, not upon save loads.

Yeah when the game starts = game back up :P
User avatar
Amber Hubbard
 
Posts: 3537
Joined: Tue Dec 05, 2006 6:59 pm

Post » Mon Jun 18, 2012 11:01 am

So oninit basically is this languages version of main(). So once the quest starts once on startup the script will always get it's oninit excuted.

I was wondering how to just get a script to do something right at the start and missed the oninit event. However wrote this scripting system really likes events. In .Net and C++ the startup code isn't in a event handler but I guess none of the scripts here are really running on their own as they are all being processed by the script engine so every script is just registering for events even for their startup.
User avatar
Lucky Boy
 
Posts: 3378
Joined: Wed Jun 06, 2007 6:26 pm

Post » Mon Jun 18, 2012 11:06 am

http://www.creationkit.com/OnInit != http://cs.elderscrolls.com/index.php/GetGameLoaded
User avatar
Nice one
 
Posts: 3473
Joined: Thu Jun 21, 2007 5:30 am

Post » Mon Jun 18, 2012 11:41 am

http://www.creationkit.com/OnInit != http://cs.elderscrolls.com/index.php/GetGameLoaded

Do those commands exist in skyrim those are from the script extender docs from oblivion. They don't show up on their wiki list of functions and stuff.

Edit: Those as in GetGameLoaded()
User avatar
megan gleeson
 
Posts: 3493
Joined: Wed Feb 07, 2007 2:01 pm

Post » Mon Jun 18, 2012 11:49 am

There is no GetGameLoaded() for Skyrim, but there hopefully will be via the SKSE Team. The above snippet will approximate the script extender function as the value of bGameLoadedGLOB will not be stored in a save.
User avatar
Claire Jackson
 
Posts: 3422
Joined: Thu Jul 20, 2006 11:38 pm

Post » Mon Jun 18, 2012 8:23 am

So does oninit() only happen once per instance of skyrim (Only the first loaded save gets the event?)(Because that would be a bit strange for quests to not get reloaded through loads, wouldnt the shared scripts have all their oninit events fire then, or is there magical caching or something?)

And the subscribe for events presists through load/save cycles to run the script once per load event due to the constant being reset?
User avatar
Marilú
 
Posts: 3449
Joined: Sat Oct 07, 2006 7:17 am

Post » Mon Jun 18, 2012 2:17 am

So does oninit() only happen once per instance of skyrim (Only the first loaded save gets the event?)
Yes, unless the quest/whatever is reset.

Say you have a quest with...
ScriptName OnInitScript extends QuestImport DebugEvent OnInit()	Debug.MessageBox("Initialized")EndEvent
...attached to it. When the quest first loads, you'll get a MessageBox. Save, load said save, and there will be no MessageBox 'cause the quest has already initialized. If you want your code to run every time a save is loaded, the constant global is the way 'til we, hopefully, get a GetGameLoaded() function SKSE style. That is what you mean by "run once every time the game is loaded", right?
User avatar
Lily Evans
 
Posts: 3401
Joined: Thu Aug 31, 2006 11:10 am

Post » Mon Jun 18, 2012 9:14 am

Does the oninit fire if the user saves quits and then loads the same save?
User avatar
Nicole Coucopoulos
 
Posts: 3484
Joined: Fri Feb 23, 2007 4:09 am

Post » Mon Jun 18, 2012 10:45 am

Does the oninit fire if the user saves quits and then loads the same save?
*User loads Save420
*MessageBox fires "Initialized"
*User saves Save421
*User quits game
*User loads Save421
*Nothing happens
User avatar
Nuno Castro
 
Posts: 3414
Joined: Sat Oct 13, 2007 1:40 am

Post » Sun Jun 17, 2012 11:52 pm

Yes, unless the quest/whatever is reset.

Say you have a quest with...
ScriptName OnInitScript extends QuestImport DebugEvent OnInit()	Debug.MessageBox("Initialized")EndEvent
...attached to it. When the quest first loads, you'll get a MessageBox. Save, load said save, and there will be no MessageBox 'cause the quest has already initialized. If you want your code to run every time a save is loaded, the constant global is the way 'til we, hopefully, get a GetGameLoaded() function SKSE style. That is what you mean by "run once every time the game is loaded", right?

Couldn't you just do "OnCellLoad"?

Sure it would run the check everytime the cell loads (even if you aren't loading a save) but by loading a save you are basically forcing a cell load to happen, which would start the quest script it is attached to.
User avatar
Natasha Biss
 
Posts: 3491
Joined: Mon Jul 10, 2006 8:47 am

Post » Mon Jun 18, 2012 6:15 am

Couldn't you just do "OnCellLoad"?

Sure it would run the check everytime the cell loads (even if you aren't loading a save) but by loading a save you are basically forcing a cell load to happen, which would start the quest script it is attached to.

Does a quest support the event OnCellLoad?
User avatar
sas
 
Posts: 3435
Joined: Thu Aug 03, 2006 8:40 am

Post » Mon Jun 18, 2012 6:15 am

No it doesnt.
It has the events of the Quest script and the Form script.

Only the objectreference script has it.

And OnCellLoad doesn't trigger on a game load - tested this by attaching a script to the Player actor.
User avatar
Miss K
 
Posts: 3458
Joined: Sat Jan 20, 2007 2:33 pm

Post » Mon Jun 18, 2012 1:12 am

There is no GetGameLoaded() for Skyrim, but there hopefully will be via the SKSE Team. The above snippet will approximate the script extender function as the value of bGameLoadedGLOB will not be stored in a save.

Just wanted to note for the benefit of future readers who may find this thread while doing a search--as I just did--that I don't think this method actually works. Unless I am doing something wrong, you cannot set the value of a constant global; the game throws an error message ("error: Cannot set the value of a constant GlobalVariable stack"), and so the code in the OnUpdate block gets run every few seconds rather than just once.
User avatar
Red Bevinz
 
Posts: 3318
Joined: Thu Sep 20, 2007 7:25 am

Post » Mon Jun 18, 2012 9:17 am

Here's what I do:
http://www.gamesas.com/topic/1349590-init-at-game-load-and-init-at-game-restart
User avatar
OJY
 
Posts: 3462
Joined: Wed May 30, 2007 3:11 pm

Post » Mon Jun 18, 2012 11:32 am

Just wanted to note for the benefit of future readers who may find this thread while doing a search--as I just did--that I don't think this method actually works. Unless I am doing something wrong, you cannot set the value of a constant global; the game throws an error message ("error: Cannot set the value of a constant GlobalVariable stack"), and so the code in the OnUpdate block gets run every few seconds rather than just once.
Yeah, "Constant" Globals can't be set with any native Papyrus functions. The below, however, does work.

ScriptName FunctionTestScript extends Quest{Demonstrates GetGameLoaded() function}Form Property ObscureItem AutoLeveledItem Property EmptyLVLI AutoObjectReference Propety SomeContainerREF AutoEvent OnInit()	RegisterForSingleUpdate(3)EndEvent;;;;;;;;;;;;;;;;;;;;;;;;;;;;Event OnUpdate()	RegisterForSingleUpdate(3)	If GetGameLoaded(EmptyLVLI, ObscureItem, SomeContainerREF)		Debug.Notification("GetGameLoaded()") ; Shows once per save load	Else		Debug.Notification("!GetGameLoaded()") ; Shows repeatedly	EndIfEndEvent;;;;;;;;;;;;;;;;;;;;;;;;;;;;Bool Function GetGameLoaded(LeveledItem akLeveledItem = None, Form apItem = None, ObjectReference akContainer = None)	akContainer.AddItem(akLeveledItem, 1, True)	If akContainer.GetItemCount(apItem)		akContainer.RemoveItem(apItem, akContainer.GetItemCount(apItem), True)		Return False	Else		akLeveledItem.AddForm(apItem, 1, 1)		Return True	EndIfEndFunction 
User avatar
hannah sillery
 
Posts: 3354
Joined: Sun Nov 26, 2006 3:13 pm

Post » Sun Jun 17, 2012 11:32 pm

Nice find Justin.

If I'm reading that right, on the http://www.creationkit.com/AddForm_-_LeveledItem page, we should have a warning that changes are not saved in savegames?

Edit: Jaysus had already put a note was already on the discussion page. It may have changed in a patch because he mentioned it caused CTD's.
User avatar
ANaIs GRelot
 
Posts: 3401
Joined: Tue Dec 12, 2006 6:19 pm

Post » Mon Jun 18, 2012 3:19 am

Nice find Justin.
Thanks, but it was Cipscis' idea which I'd tried and given up on awhile ago as I'd mistakenly tested with the FormList version which does persist.
If I'm reading that right, on the http://www.creationkit.com/AddForm_-_LeveledItem page, we should have a warning that changes are not saved in savegames?
Done.
Edit: Jaysus had already put a note was already on the discussion page. It may have changed in a patch because he mentioned it caused CTD's.
Didn't crash while testing the GetGameLoaded() function. If ever AddForm() for LVLIs sticks, I'll pull the note from the Wiki.
User avatar
louise tagg
 
Posts: 3394
Joined: Sun Aug 06, 2006 8:32 am

Post » Mon Jun 18, 2012 11:48 am

This one's faster and is less likely to ever stop working after a patch given BrainCondition is "Obsolete" and, along with http://www.creationkit.com/Actor_Value#Notes, is not saved.
...	Debug.Notification("GetGameLoaded() == " + GetGameLoaded(MaiqTheLiarREF))...Bool Function GetGameLoaded(Actor akActor = None)	If akActor.GetActorValue("BrainCondition")		akActor.SetActorValue("BrainCondition", 0)		Return True	Else		Return False	EndIfEndFunction 
User avatar
Jesus Sanchez
 
Posts: 3455
Joined: Sun Oct 21, 2007 11:15 am

Post » Mon Jun 18, 2012 9:41 am

This one's faster and is less likely to ever stop working after a patch given BrainCondition is "Obsolete" and, along with http://www.creationkit.com/Actor_Value#Notes, is not saved.
...	Debug.Notification("GetGameLoaded() == " + GetGameLoaded(MaiqTheLiarREF))...Bool Function GetGameLoaded(Actor akActor = None)	If akActor.GetActorValue("BrainCondition")		akActor.SetActorValue("BrainCondition", 0)		Return True	Else		Return False	EndIfEndFunction 
Cool!
User avatar
matt oneil
 
Posts: 3383
Joined: Tue Oct 09, 2007 12:54 am

Post » Mon Jun 18, 2012 11:33 am

Better than the AddForm() hack and probably permanent. Should work from any number of scripts so long as akActor and/or the AV differs. Still trying to think of a way to do it w/o akActor, hopefully not using Game.GetPlayer(), and still ensure the function will return 'True' when appropriate for each script using it.
User avatar
Charleigh Anderson
 
Posts: 3398
Joined: Fri Feb 02, 2007 5:17 am

Post » Mon Jun 18, 2012 2:18 pm

Ahhh yeah. All mods that use this method will be accessing the same AVs. :(
User avatar
The Time Car
 
Posts: 3435
Joined: Sat Oct 27, 2007 7:13 pm

Next

Return to V - Skyrim