Jump to content

Need Help With A Script


Launius

Recommended Posts

I started scripting about 3 days ago, so bare with me. I am attempting to write a script that will cause certain hostile creatures to not attack the player. The script will be placed on an item. My problem is that I can't seem to figure out how to go about referencing any creature in the proximity of the player character. I thought about maybe using "if (player.SameFaction TargetActor.Ref == 1)" But how can I tell the script that I want it literally to use the targeted actor? Edited by Launius
Link to comment
Share on other sites

"GetCombatTarget" returns the target of an actor. However, this function doesn't work for the player, since the game cannot tell which actor the player targets.

 

For what you want to do, you'll probably need to use OBSE and its GetFirstRef and GetNextRef functions.

 

scn ExampleQuestScript

ref tempref

Begin GameMode

let tempref := GetFirstRef 69 1
while (tempref)
	if (tempref.GetCombatTarget == Player)
;			do stuff
	endif
	let tempref := GetNextRef
loop

End

Edited by fg109
Link to comment
Share on other sites

What some items do to get an effect like this is add you to a faction if the item is equipped. Like in Knights of the Nine, one of the items makes it so animals won't attack you. So you would add the player to the faction when the item was equipped and there would be peace. You would remove the player from the faction when the item was removed and there would be war.
Link to comment
Share on other sites

@FG - Thanks much, that is definitely going to help for this and any future scripts that I write.

@ David - I tried simply adding the player to the faction, but the enemies still attacked, so I'm guessing I need to get more complicated with it, lol.

 

I've been playing with this script for awhile and this is how it looks. Unfortunately, not only does it often crash the game, but it also doesn't seem to be restraining anyone, lol. Any advice?

 

scn CursedArmorScript

ref TargetingActor

Begin OnEquip
player.SetFactionRank UndeadFaction, 1
End

Begin GameMode
set TargetingActor to GetFirstRef 36 1
	while (TargetingActor)
		if (player.SameFaction TargetingActor == 1)
			TargetingActor.SetRestrained 1
		elseif (player.SameFaction TargetingActor == 0)
			TargetingActor.SetRestrained 0
		endif
set TargetingActor to GetNextRef
loop
End

Begin OnUnequip
player.SetFactionRank UndeadFaction, -1
End

Edited by Launius
Link to comment
Share on other sites

So far, the script has evolved into this. It works so far, but when the armor is unequipped, the game still crashes. Any thoughts?

 

scn CursedArmorScript

ref TargetingActor

Begin GameMode
set TargetingActor to GetFirstRef 36 1
	while (TargetingActor)
		if (player.GetEquipped CursedArmor001 == 1)&& (TargetingActor.GetInFaction UndeadFaction == 1)
			TargetingActor.SetRestrained 1
		elseif (player.GetEquipped CursedArmor001 != 0)&& (TargetingActor.GetInFaction UndeadFaction == 1)
			TargetingActor.SetRestrained 0
		endif
set TargetingActor to GetNextRef
loop
End

Edited by Launius
Link to comment
Share on other sites

I think unequipping the armor probably breaks the loop, and breaking the loop improperly leads to crashes, but I'm not sure...

 

Are you sure what David suggested doesn't work? If all you need it to do is stop undead creatures from attacking you while wearing the armor, it should be pretty easy.

 

scn example

Begin OnEquip

Player.SetFactionRank UndeadFaction 0

End

Begin OnUnEquip

Player.SetFactionRank UndeadFaction -1

End

Link to comment
Share on other sites

I've tried that exact script and furthermore tried it several different ways. I even went as far as to directly find the NPC "player" and added UndeadFaction to its faction list. I tested it repeatedly against skeletons that I triple checked belonged to the UndeadFaction. Lol, I'm at a loss. I had updated that script I'd posted, so I should say that it wasn't crashing on unequip anymore. My only problem was that on unequip, the affected actors stayed restrained, even though I used "TargetingActor.SetRestrained 0"

 

Edit: So I turned it into two separate scripts and I got it to work. Now when the item is equipped, all skeletons are set to restrained, and when the item is unequipped they resume attacking. Thanks for the help.

Edited by Launius
Link to comment
Share on other sites

That's weird how such a simple script doesn't work...

 

Try loading the game without your mod, then opening up the console and typing "player.SetFactionRank 9DB1F 0" to put yourself in the undead faction. Then try running up to those skeletons you mentioned again and see if they attack you.

 

I tried it and I didn't get attacked by a zombie. If those skeletons do attack you, then perhaps you have a mod running that changes their faction alignments.

Link to comment
Share on other sites

Damn, I learned a lot of cool s*** while trying to write a script for that, but wow, that could have been really simple.

 

Okay, so it worked as a console command, but I still can't get it to work as an 'equip/unequip' script. I'm going to try starting from scratch and see if maybe that fixes that little problem.

 

Okay, started from scratch. With the console command it still works, but with the mod script it doesn't. I'm starting to think that maybe the OnEquip\OnUnequip breaks the code, I just have no idea why.

 

Evidently there is a bug with the SetFactionRank function, according to this link. It must be true, because this is what got my script to work:

Begin OnEquip player
player.SetFactionRank 0009DB1F 0
player.SetFactionRank 0009DB1F -1
player.SetFactionRank 0009DB1F 0
End

Begin OnUnequip player
player.SetFactionRank 0009DB1F 0
player.SetFactionRank 0009DB1F -1
End

Edited by Launius
Link to comment
Share on other sites

  • Recently Browsing   0 members

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