Jump to content

How do I make a script run when you load a savegame?


chaospearl

Recommended Posts

I'm running into another issue with my apple trees. Yeah, again.

 

I am using the vanilla beehive script to keep the apples on the tree. It's a simple script that enables Havok when the object is hit.

 

The problem is, when I start a new game and go check out my trees, it works beautifully. But when I make an update to add new trees, and go to test it, the script doesn't work and my new apples all start out Havok-enabled and fall right off the trees. Apparently, the script runs once when you start a new game and disables Havok on everything with the script attached, but then it doesn't run again when you load a savegame. How do I make it do that?

 

This is the script:

 

 

 

Scriptname BeeHiveHavokOnHit extends ObjectReference

{Stops the beehive from havoking until hit.}

 

 

Event OnLoad()

; Debug.Trace("This object is loaded, playing animations should work now")

self.SetMotionType(Motion_Keyframed, true)

endEvent

 

Event OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)

; Debug.Trace("I just got hit by something")

self.SetMotionType(Motion_Dynamic, true)

endEvent

 

 

 

I tried Googling this, but I don't know much about Papyrus so I'm in over my head. From what I can tell it appears that the OnLoad function may be a bit buggy? Some people have suggested replacing it with OnCellAttach. Does that apply to this situation?

Link to comment
Share on other sites

I tried Googling this, but I don't know much about Papyrus so I'm in over my head. From what I can tell it appears that the OnLoad function may be a bit buggy? Some people have suggested replacing it with OnCellAttach. Does that apply to this situation?

 

From the creation kit wiki:

 

For interiors, it often looks like this event fires whenever the player enters the cell. It doesn't. If you leave a cell, the cell may or may not have unloaded by the time you return, which means this event may or may not fire again. If you need a reliable event every time the player enters a cell, try the OnCellAttach Event instead.

 

Why not just give it a whirl?

Link to comment
Share on other sites

I tried Googling this, but I don't know much about Papyrus so I'm in over my head. From what I can tell it appears that the OnLoad function may be a bit buggy? Some people have suggested replacing it with OnCellAttach. Does that apply to this situation?

 

From the creation kit wiki:

 

For interiors, it often looks like this event fires whenever the player enters the cell. It doesn't. If you leave a cell, the cell may or may not have unloaded by the time you return, which means this event may or may not fire again. If you need a reliable event every time the player enters a cell, try the OnCellAttach Event instead.

 

Why not just give it a whirl?

 

Okay, gave it a shot. It doesn't work.

 

If the cell has been loaded already, the script does not work. Doesn't matter if it's a clean save that has never included that mod before. Anytime I put an apple in a cell that the player's been to already, the script doesn't fire, using either OnLoad or OnCellAttach. :( I mean... I'm assuming the script itself runs, but it doesn't do anything under the OnLoad or OnCellAttach lines. It acts as if the cell's not loading, when it clearly is.

Link to comment
Share on other sites

You mean you don't see any debug messages in your papyrus logs at all? For easier troubleshooting, you could temporarily replace the debug.trace with debug.notification so you get an onscreen notification if it fires.That way you can see if it's firing as intended at least.

 

(and remove the ; comment marker)

Edited by acidzebra
Link to comment
Share on other sites

Okay. I've figured out that it isn't working because the cell's not loading -- it's already loaded. OnLoad and OnCellAttach both do their thing when a cell first loads. If that cell is already loaded, neither one will work. If I go a disstance away and purge the cell buffer to force the cell to reload, THEN it works.

 

Apparently, the script runs once when you first enter a cell. It will not run again until the cell is unloaded and reloaded, and evidently that doesn't happen when you start up a savegame. Go into a cell, save, exit the game, then restart the game and luse that save. Your cell doesn't reload. It treats the cell as being already loaded, so even if you've placed totally new objects in there with that script on them, the script won't function because it wants the cell to reload first.

 

Basically, anytime I place an apple with the script in a cell the player's already visited, that script will not work. Doesn't matter if it's a brand new object, doesn't matter if it's a clean save without ever having had that mod running. No cell load = no script worky.

 

How do I make the script do what it needs to do, when the cell may already be loaded? OnLoad and OnCellAttach both assume the cell will be loading, but if it's already loaded... no go. There must be a way to make this work? And how is a cell possibly already loaded when you're just starting up the game now? If I start up a savegame, evidently that does not count as loading the cell, because the script's not working. Somehow the savegame remembers which cells were already loaded.

 

This mod is making me cry. :( Every single time I go through hell and finally "fix" the apples to stay on the trees, next time I work on it I find that HAHAHAHAHA that didn't fix it after all, serves you right for thinking you could mod with the big boys! What an idiot.

 

Edit the 2nd: Well, it does seem to work (tentatively -- seems like every time I say that, it turns out only to work under very specific circumstances that I somehow stumbled upon and will never see occur again) if I change OnLoad to OnInit. Of course I have no idea what I'm doing here... is OnInit a safe, viable way to do this? No resource hit, no unintended complications?

Edited by chaospearl
Link to comment
Share on other sites

@chaospearl: everything is working exactly the way it is supposed to. OnLoad() will fire if you travel sufficiently far away from the cell (usually about >15,000 units distance or so) and then return to it. Or try fast travelling to a different worldspace and back. If the game is saved with the cell loaded, the cell will be in this same state when the savegame is reloaded, it's simple logic. it would also prevent cheating by hitting the apple tree, saving, reloading, hitting the tree again, saving, reloading...

 

OnInit() will only fire ONCE when the script is first loaded and then never again until the cell RESETS. It is mainly used to initialize variables or kick off functions that only need setting up or running once.

 

Edit: to update your mod with more trees, you will need to travel to an interior in another worldspace, save, install the new mod, travel back to the location with the trees. You will also have to watch out for savegame bloat with all these new apples being spawned all over the place.

 

Edit2: try having both OnLoad() AND an identical OnInit() block. That way OnInit() should fire for any NEW trees that you add, but not on the existing trees.

Edited by steve40
Link to comment
Share on other sites

Steve, thanks... again. I'm sorry for all the time you seem to be spending following me around the forum fixing my various messes. :( Modding's a learn as you go endeavor, I guess.

 

Using OnInit works nicely, since it seems to do exactly what I need it to do -- run the script (well, the block) once and then go to sleep until the cell resets.

 

The way you explain cell loading\reloading in savegames makes perfect sense NOW that you put it that way. I was thinking that when you exit the game, everything would unload itself, because... well, no game, nothing can be loaded if there's nothing. So I thought that when you booted up your savegame, the cell would have to load again, isn't that what "loading" a savegame does, loads the game? I just didn't understand how the process actually works. I do now, thanks to you.

 

Whenever I update the mod to add new trees, the new ones are typically a good distance away from the old ones. I don't want trees littering every other cell, it would make them less neat to find. As of the last update, there are two trees in Riverwood and a few more behind the Honningbrew Meadery. I don't want to burn out on modding, so I normally spend my morning actually playing the game and wandering about the world. Keeping an eye out for good places to plant an apple tree, also looking for vanilla plants that would make good harvestables.

 

What would the difference be between using the OnInit block as I am now, and changing it to use two blocks, one with an OnLoad, as you said? I mean, I understand the difference in how it works, but could you explain why the two blocks option would be a better idea?

 

I really appreciate all the help you've given me.

Edited by chaospearl
Link to comment
Share on other sites

I simply assumed that you wanted the apples to reset every time the cell loaded, because you used OnLoad in your original script.

 

Ah, okay. No, the OnLoad was there because I didn't write my own script at first. I used a vanilla script, the one that keeps beehives on trees until you shoot them down. I'd never written a script before, didn't even know where to begin. I didn't see a need to do so if there was already a vanilla script that did exactly what I needed it to do. Unfortunately as it turned out, it didn't do EXACTLY what I needed because of that OnLoad so I had to make my own anyway. Took me a bit to figure out where the scripts are kept, the difference between psc and pex, how to compile a script. It's not hard, obviously, just one more thing I'd never done before. I'm not stupid but I am very ignorant when it comes to this.

 

As it turns out I ended up doing exactly what you said anyway. When I was using OnInit instead of OnLoad, it worked perfectly every time UNLESS you started a brand new game. In a new game, the OnInit wasn't firing, I don't know enough to understand why not. It's ironic because with the OnLoad, the only time it DID work was with a new game.

 

So I added another block to the script as you suggested and now it appears to work no matter what complicated combination of loaded cells and new or saved games I throw at it. Finally, huzzah.

Link to comment
Share on other sites

Ah, I know why. You can only change the motion type after the 3D has loaded. So with a new game you need to use OnLoad to be sure that the 3D has already loaded before you set the motion type. But if you're adding new trees to a loaded area then you can assume everything is loaded and use OnInit for the new stuff. Ideally you need to add something like this in OnInit to check that 3D is loaded first, otherwise it might spam the error log:

 

If Is3DLoaded() && GetParentCell() != None

SetMotionType(Motion_Keyframed, true)

EndIf

Link to comment
Share on other sites

  • Recently Browsing   0 members

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