OBSE Event Handler and Funcition Help

Post » Wed Nov 28, 2012 6:03 pm

I am creating a dog companion mod for myself and have run into a small problem. First off, in a past life I was a computer coder so I do have a good understanding on how this coding stuff works.

What I am trying to do is keep a count of how many kills my dog has. I was first just using a test to see if the dog's enemy was dead but that didn't cut it. I mean it worked but if I dealt the death blow to his enemy he still got credit. So that wasn't the way.

I started checking out what OBSE could do and was excited to see that I could create Event Handlers to intercept the enemy's OnDeath function. Here is the code I used in the dog's script:

short sDogKillCount ; holds number of kills for the Dog
ref rDog ; reference to Dog
ref rEnemy ; reference to Dog's current Combat target

SetEventHandler "OnDeath", aaaMTRcDogDeathCountFN, "ref"::rEnemy, "object"::rDog

This is the function that handles the event:

scn aaaMTRcDogDeathCountFN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This is only to be used as an OnDeath Event Handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ref target
ref attacker
print $target + " was killed by " + $attacker
end


So I load the game and go find the dog a victim. I find a Goblin and set the dog to attack, When the the Goblin dies the message "Goblin was killed by Traxx" prints to console. By the way the dog's name is Traxx. I was happy to see that it worked. So now I add some code to update the dog's Kill count. Thinking all I need to ad was this line to the event code:

Let attacker.sDogKillCount += 1

I try to save and the complier says the variable sDogKillCount cannot be found, I thought about it for awhile then realized that the complier had no idea what Reference I had passed it so the variable couldn't be found. It made sense. But now I don't know what to do. In my coding days I just would have created a variable in the Event Handler of TYPE myDog and used it. That way the complier would know exactly what type of Reference it was and have access to the variable sDogKillCount.

So it seems I cannot pass a Reference to a custom function and be able to use any variables that are unique to the script attached to the Object. That kind of takes away the usefulness of functions. I did find a way to make it work but I had to hard code a check to see which dog killed the the enemy, That definitely defeats the purpose of fucntions.

This is what I ended up with:

scn aaaMTRcDogDeathCountFN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; This is only to be used as an OnDeath Event Handler
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

ref target
ref attacker
string_var strAttacker
string_var strTraxx
string_var strShadow

begin Function { target, attacker }
Set strAttacker to attacker.GetName
Set strTraxx to sv_Construct "Traxx"
Set strShadow to sv_Construct "Shadow"

if eval sv_Compare "%z" strAttacker, strTraxx == 0
Let MTRcDogTraxxRef.sDogKillCount += 1
elseif eval sv_Compare "%z" strAttacker, strShadow == 0
Let MTRcDogShadowRef.sDogKillCount += 1
endif

Set strAttacker to sv_Destruct
Set strTraxx to sv_Destruct
Set strShadow to sv_Destruct
print $target + " was killed by " + $attacker
end


I tried using just he Editor reference to check which dog did the killing but it didn't work, I had check against the name of the dog, but that is another story!!

So if anyone knows how I can use variables from an Objects script in functions with having to hard code anything that would be real helpful

Oh yea if anyone also knows an easier way to get a kill count for and object that would be nice, but my main question on the functions is what I really am after. Without it I can't do all the other cool things planned for this mod.
User avatar
Heather M
 
Posts: 3487
Joined: Mon Aug 27, 2007 5:40 am

Return to IV - Oblivion