Child Scripts Receive States?

Post » Tue Jan 19, 2016 9:48 am

I've written a small script that I've attached to a copy of the Bear Trap activator that I want to make disappear when activated in specific state and add a miscellaneous object to the player's inventory.


The script extends to the default "TrapBear" script and I want it to intercept the "OnActivate" function only when the parent script is in a specific state. However, I have been unable to find any information on whether child scripts can access the states of their parents, and I'm having problems getting it to recognize functions and variables that I know to be heritable.



The relevant parts of the TrapBear Script read...




scriptName TrapBear extends objectReference



...



state Open


Event OnBeginState()


...debug message


EndEvent



Event OnActivate(objectReference TriggerRef)


GoToState("Busy")


hitBase.goToState("CannotHit")


playAnimationAndWait("Trigger01", "Trans01")


goToState("Closed")


endEvent


endState



and the script I'm trying to extend to it reads...




scriptName aaaTrapBear extends TrapBear



miscObject Property aaaTrapBearMisc Auto; OBJECT I'M TRYING TO ADD ON ACTIVATION



Event OnActivate(objectReference TriggerRef)


Debug.Notification("Script Intercepted Activation"); THIS NEVER FIRES


endEvent



State Open


Event OnBeginState()


Debug.Notification("Inherited Open State");THIS FIRES WHEN OBJECT LOADS


endEvent



Event OnActivate(objectReference TriggerRef)


Debug.Notification("Intercepted Activation In Open State");THIS FIRES, BUT IN EVERY STATE


endEvent


endState



...but it experiences problems as outlined in the debug notifications, notably the child script seems to ignore functions outside the parent script's state, but doesn't actually respond to the state that is active at any given time.



I feel like I'm probably missing something, either how to declare states or functions or some call like "getParentScript()" that would be used to refer to the parent script. I've found online tutorials that deal with inheriting variables and global functions, but never specific functions or states like this. Does anyone have experience here? Thanks

User avatar
Jennie Skeletons
 
Posts: 3452
Joined: Wed Jun 21, 2006 8:21 am

Post » Tue Jan 19, 2016 10:07 am

From the wiki: http://www.creationkit.com/State_Reference





It should all be one state for child/parent



Edit: To add on... what are you trying to accomplish? Your script doesn't do anything except override a few key functions (i.e. your OnActivate will run, but the parent OnActivate won't). There is nothing in your child script which changes state. Also, the parent script can be called like so:




; Call the parent's version of DoStuff, ignoring our local definition
Parent.DoStuff()
User avatar
Phillip Brunyee
 
Posts: 3510
Joined: Tue Jul 31, 2007 7:43 pm

Post » Tue Jan 19, 2016 7:52 pm

Hi mojo, thanks for the quick reply, I didn't know you could call "parent" like that without declaring the containing script or object somehow, am I correct in assuming you can call it without declaring anything in the same way as calling "self"? Since this script extends another script I'm guessing this is so, but I know in the actual parent script calling "self" actually refers to the parent object to which the script extends, and I've never seen a "parent" call in any scripts that extend to non-script forms like ObjectReferences, Actors, etc. so I want to make sure I don't have that confused.


As for the purpose of the script, I'm trying to overwrite the parent ObjectReference script's OnActivate() event only when it's in the open state, which usually triggers placing the activator in a "busy" state coupled with an animation for snapping shut, before moving to a "closed" state in which activating it reopens the trap and returns to the "open"state. Instead, I would like to use it to delete the activator on which the script is running when it's activated while open and add the appropriate misc item to the player's inventory. If I had to guess what it would look like it would be...


Scriptname aaaBearTrap extends BearTrap; the original script which extends ObjectReference


MiscObject Property aaaBearTrapMisc auto; declared item to add


State Open

Event OnActivate(ObjectReference TriggerRef); when activating in the parent script's open state

Self.disable();disable the trap

TriggerRef.addItem(aaaBearTrapMisc, 1);add the misc equivalent to the actor

Utility.wait(some small amount of time)

Self.delete(); delete the disabled trap

EndEvent

EndState


However, when testing this structure without the functions just using debug notifications, the OnActivate() event in the "open" state seemed to fire when activating the object even when I knew the parent script to be in another state, "closed" or "busy". As such, I wanted to instead try something like what I think from your advice would be...


Script name, extension ...


MiscObject declared...


Event OnActivate(ObjectReference TriggerRef)

If(parent.getState() == "open");I didn't know how to reference the parent script here, is this right?

Self.disable()

TriggerRef.Additem()...

Utility.wait()...

Self.delete()

EndIf

EndEvent


I'll try this when I can get back to the Creation Kit to see if it works and report back. Unless you see anything wrong that's eluded me already...

Thanks again!
User avatar
jess hughes
 
Posts: 3382
Joined: Tue Oct 24, 2006 8:10 pm

Post » Tue Jan 19, 2016 4:13 pm

I suspect your problem is that you have both your new script and the original BearTrap script attached to the activator at the same time. You should never have a child script and its parent script attached to the same object. If you remove the original BearTrap script from your new activator the first version of your code will probably work.



Your best option is probably to copy the entire contents of the BearTrap script into a new script and attach it instead of the BearTrap script to your object. In this case it makes more sense to copy the original and make changes because there are multiple mods that alter that particular script and trying to inherit from the original while one of the modified versions is in someone's game is very likely to cause problems. And as a bonus you don't have to worry about the quirks of inheritance.

User avatar
Farrah Barry
 
Posts: 3523
Joined: Mon Dec 04, 2006 4:00 pm

Post » Tue Jan 19, 2016 3:44 pm

States and function overrides should work just fine.

However, it's not clear what scripts you've attached to the traps.

If you are attaching your script in addition to the base script to the object, then both scripts are independent and will not affect eachother.

You should instead replace the old script on the object with your new script. There is no need to copy the entire old script into your new on and, in so doing, you wouldn't be able to take advantage of other mods or patches adjusting the base script.

Also - something very important to remember - removing scripts from an object does not remove those scripts from the object in your save game. The save game will override the masterfile and re-attach the base script as a secondary script. So if you're replacing scripts like this you need to test from a completely new character (even a "clean" save won't work because you're trying to remove a base game script).
User avatar
Neliel Kudoh
 
Posts: 3348
Joined: Thu Oct 26, 2006 2:39 am


Return to V - Skyrim