Scripted objects in inventory

Post » Wed Jun 20, 2012 12:30 pm

I came across some issues with moving scripted objects in and out of your inventory. I'll try and explain the problem(s).

I put this script on the iron dagger:

Scriptname fg109TestObjScript extends ObjectReference  int CountEvent OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)    Count += 1    Debug.Notification(Count)EndEvent

I used the console to give myself an iron dagger, but no message showed up. I dropped it on the ground. No message showed up. I picked it up, and got the message "1". I dropped it, no message. I picked it up, got "1". I dropped it, got "2". Picked up, "3". Dropped, "2". Picked up, "3". Dropped, "4". Picked up, "5". Dropped, "4". Picked up, "5". Dropped, "6". Etc.

So it looks like the game is actually swapping out two different daggers although I only had one in my inventory. I remember last week there was a topic about how RegisterForUpdate() called from OnLoad() for a weapon wouldn't work. I think it's related to this.

Anyway, I restarted the game and started picking up and dropping daggers again. This time, it took an extra couple of times before the messages showed up. I tried giving myself another dagger and dropping them one at a time. This messed up the alternating order so I got numbers all over the place. It's as though I had 10 daggers and when I dropped or picked them up, I chose them at random.

I also tried restarting the game and giving myself 10 daggers. I dropped and picked them up 10 at a time. I did it 7 times without any messages appearing. Then when I dropped them the 8th time, they disappeared. No longer in my inventory but not in the world either.

I restarted and started dropping 10 daggers again. I did it 20 times but they didn't disappear this time. But of all the times I dropped them or picked them up, I didn't get a single message.

I can live with not being able to keep track of variables, but for the script to not work at all is a problem.
User avatar
Laura Wilson
 
Posts: 3445
Joined: Thu Oct 05, 2006 3:57 pm

Post » Wed Jun 20, 2012 11:03 am

Well the initial one is understandable, since it's being created in your pocket, so it technically didn't "change" containers yet...but not sure what's going on otherwise...it's just plain weird.

Each dagger should have it's own "ChangeContainerCount" number according to the script. Try putting a notification in some other events like "OnInit" and "Onreset" to see if something might be resetting the variables. Being put in a stack of items in your inventory probably plays hob with the script too.
User avatar
Chloe Mayo
 
Posts: 3404
Joined: Wed Jun 21, 2006 11:59 pm

Post » Wed Jun 20, 2012 8:28 am

I'm not actually making any mods, so this doesn't affect me much. I just thought people would want to know about problems like this. I did some more tests with this script:

Spoiler
Scriptname fg109TestObjScript extends ObjectReference  GlobalVariable property GlobalInt autoint MyInt = -1Event OnInit()	if (MyInt < 0)		MyInt = GlobalInt.GetValueInt()		GlobalInt.SetValueInt(MyInt + 1)	endif	Debug.Notification("OnInit - " + MyInt)EndEventEvent OnLoad()	if (MyInt < 0)		MyInt = GlobalInt.GetValueInt()		GlobalInt.SetValueInt(MyInt + 1)	endif	Debug.Notification("OnLoad - " + MyInt)EndEventEvent OnEquipped(Actor akActor)	if (MyInt < 0)		MyInt = GlobalInt.GetValueInt()		GlobalInt.SetValueInt(MyInt + 1)	endif	Debug.Notification("OnEquipped - " + MyInt)EndEventEvent OnContainerChanged(ObjectReference akNewContainer, ObjectReference akOldContainer)	if (MyInt < 0)		MyInt = GlobalInt.GetValueInt()		GlobalInt.SetValueInt(MyInt + 1)	endif	Debug.Notification("OnContainerChanged - " + MyInt)EndEvent

I had to duplicate the iron dagger and put the script on that one instead. Otherwise, I started getting a ton of OnInit messages, presumably from every iron dagger in the game.

These are the resulting messages from only playing around with one dagger:

Spoiler
additem: OnInit - 0, OnContainerChanged - 0equipped & drawn: OnEquip - 0dropped: OnContainerChanged - 0, OnInit - 1, OnLoad - 0picked up: OnContainerChanged - 0equipped & drawn: OnEquip - 0dropped: OnContainerChanged - 0, OnInit - 2, OnLoad - 0picked up: OnContainerChanged - 0equipped & drawn: OnEquip - 0dropped: OnContainerChanged - 0, OnInit - 3, OnLoad - 0picked up: OnContainerChanged - 0equipped & drawn: OnEquip - 0dropped: OnContainerChanged - 0, OnInit - 4, OnLoad - 0picked up: OnContainerChanged - 0equipped & drawn: OnEquip - 0dropped: OnContainerChanged - 0, OnInit - 5, OnLoad - 0picked up: OnContainerChanged - 0equipped & drawn: OnEquip - 0

So I guess there's not really a problem as long as you have an OnInit event in your script. This is when I decided to add a second dagger:

Spoiler
additem2: OnInit - 6, OnContainerChanged - 6dropped2: OnContainerChanged - 6, OnInit - 7, OnLoad - 6dropped1: OnContainerChanged - 0, OnInit - 8, OnLoad - 0picked up2: OnContainerChanged - 6picked up1: nothingequipped & drawn: OnEquip - 6dual-wield: nothingdropped: OnContainerChanged - 6, OnInit - 9, OnLoad - 6dropped: OnInit - 10, OnInit - 11picked up: OnContainerChanged - 6picked up: nothingequipped & drawn: OnEquip - 6dual-wield: nothingdropped: OnContainerChanged - 6, OnInit - 12, OnLoad - 6dropped: OnInit - 13, OnInit - 14picked up: OnContainerChanged - 6picked up: nothingequipped & drawn: OnEquip - 6dual-wield: nothingdropped: OnContainerChanged - 6, OnInit - 15, OnLoad - 6dropped: OnInit - 16, OnInit - 17picked up: OnContainerChanged - 6picked up: nothing

I kind of lost track of which dagger I was dropping and picking up. This is when I decided to add another 8 daggers so that I could drop and pick them up in bulk:

Spoiler
additem: OnInit - 18dropped: OnContainerChanged - 6, OnInit - 19, OnLoad - 6picked up: OnContainerChanged - 6equipped & drawn (one of them): OnEquip - 6dropped: OnContainerChanged - 6, OnInit - 20, OnLoad - 6picked up: OnContainerChanged - 6equipped & drawn (one of them): OnEquip - 6dropped: OnContainerChanged - 6, OnInit - 21, OnLoad - 6picked up: OnContainerChanged - 6equipped & drawn (one of them): OnEquip - 6

So I guess the game can only keep track of one instance of a base object in your inventory. The rest don't run their scripts.
User avatar
Lucie H
 
Posts: 3276
Joined: Tue Mar 13, 2007 11:46 pm

Post » Wed Jun 20, 2012 10:27 am

OK, I was testing something else out with iron daggers again. I had a really simple script:

Scriptname TestObjScript extends ObjectReference  Event OnEquipped(Actor akActor)    Debug.Notification("Equipped test weapon.")EndEventEvent OnHit(ObjectReference akAggressor, Form akSource, Projectile akProjectile, bool abPowerAttack, bool abSneakAttack, bool abBashAttack, bool abHitBlocked)    Debug.Notification("Test weapon's OnHit triggered.")EndEvent

With the original iron dagger, no matter how many times I equipped and dropped it, no messages showed up. A weird thing that happened was that if I was equipped with the dagger when I tried to drop it, a dagger did drop. It did disappear from the inventory, but my character was still holding a dagger. (I should probably test some more by giving the iron daggers an enchantment and seeing if my 'nonexistent' dagger is actually nonexistent by hitting some NPCs.)

With a dagger that I duplicated in my test mod and added through the console (therefore only one copy of it in the game) the OnEquipped message showed up immediately. I tried equipping and dropping it a couple dozen times and it worked every time.

So here are some guesses I have to explain the results I've had:

Given the same scripted base object, only a limited number of copies can have the script running at a time.

In my previous post, I had mentioned a flood of OnInit messages from the iron daggers. I had counted up to 60 before I got impatient, which would seem to indicate that all their scripts ran, but this is obviously not the case from the results I got from my first post. So the explanation for the flood of messages is that the game is processing the scripts from all the daggers in a queue. Each and every dagger is getting their script processed in the beginning. But sooner or later, the number of daggers will surpass the limit, which means the daggers that were in the beginning of the queue will start dropping off, and not having their scripts processed anymore.

It's possible to have a whole stack of scripted objects in your inventory, and not a single one of them will have a working script.

As I said at the beginning of this post, no matter how many times I tried, it was impossible to see the message for OnEquipped. If there are a large number of copies of the scripted object in the game (as is the case for iron daggers) then it's quite likely that the one with a working script is in someone else's inventory.
User avatar
-__^
 
Posts: 3420
Joined: Mon Nov 20, 2006 4:48 pm

Post » Wed Jun 20, 2012 6:02 am


So it looks like the game is actually swapping out two different daggers although I only had one in my inventory.

It's as though I had 10 daggers and when I dropped or picked them up, I chose them at random.

OR, it's as if multiple scripts are running on your single dagger and the game engine is randomly picking which ones to process. :cool:

I read somewhere in the Creation Kit website that you have to be careful when running scripts, as in some situations you can cause another instance of the script to run on your object while the original instance is still running. I can't remember which page this info was on though.
User avatar
darnell waddington
 
Posts: 3448
Joined: Wed Oct 17, 2007 10:43 pm

Post » Wed Jun 20, 2012 9:46 am

You might be referring to the http://www.creationkit.com/Threading_Notes_%28Papyrus%29 page. I'm pretty sure that wasn't what was happening with my tests though.

One new thing I've found with scripted objects in inventory:

Unless the object is persistent (eg fills a Reference Alias), then trying to register for animation events (and possibly other types of things) would fail and result in an error.

Totally unrelated to the previous problems, but it's something important to consider.
User avatar
ShOrty
 
Posts: 3392
Joined: Sun Jul 02, 2006 8:15 pm


Return to V - Skyrim