Jump to content

Need help optimizing cloak script


Magnusen2

Recommended Posts

I'm trying to implement a disguise feature in my perk mod through a cloak ability that is cast on every NPC that meets the faction conditions for that particular disguise. So far so good.

 

But the problem comes when i came with the realization that it would be best to add some sort of racial system that makes harder for certain races to disguise in particular factions (Stormcloaks would certainly note a tail XD).

 

 

 

Scriptname xxxxxStealthSpeechDesguiseFaction extends ActiveMagicEffect  


Faction Property MagicCharmFaction Auto

bool Property bMakePlayerTeammate = false Auto

Actor Property PlayerRef Auto

Race Property ArgonianRace Auto
Race Property BretonRace Auto
Race Property DarkElfRace Auto
Race Property HighElfRace Auto
Race Property ImperialRace Auto
Race Property KhajiitRace Auto
Race Property NordRace Auto
Race Property OrcRace Auto
Race Property RedguardRace Auto
Race Property WoodElfRace Auto

int Property RaceModifierArgonian Auto
int Property RaceModifierBreton Auto
int Property RaceModifierDarkElf Auto
int Property RaceModifierHighElf Auto
int Property RaceModifierImperial Auto
int Property RaceModifierKhajiit Auto
int Property RaceModifierNord Auto
int Property RaceModifierOrc Auto
int Property RaceModifierRedguard Auto
int Property RaceModifierWoodElf Auto

int PlayerRaceModifier

Actor TargetActor


Event OnEffectStart(Actor akTarget, Actor akCaster)

	Race PlayerRace = PlayerRef.GetRace()    
	TargetActor = akTarget
	 
	if PlayerRace == ArgonianRace
		PlayerRaceModifier = RaceModifierArgonian
		
	Elseif PlayerRace == BretonRace
		PlayerRaceModifier = RaceModifierBreton

	Elseif PlayerRace == DarkElfRace
		PlayerRaceModifier = RaceModifierDarkElf

	Elseif PlayerRace == HighElfRace
		PlayerRaceModifier = RaceModifierHighElf
		
	Elseif PlayerRace == ImperialRace
		PlayerRaceModifier = RaceModifierImperial
		
	Elseif PlayerRace == KhajiitRace
		PlayerRaceModifier = RaceModifierKhajiit
		
	Elseif PlayerRace == NordRace
		PlayerRaceModifier = RaceModifierNord
		
	Elseif PlayerRace == OrcRace
		PlayerRaceModifier = RaceModifierOrc
		
	Elseif PlayerRace == RedguardRace
		PlayerRaceModifier = RaceModifierRedguard
		
	Elseif PlayerRace == WoodElfRace
		PlayerRaceModifier = RaceModifierWoodElf

	endif
	 
	akTarget.AddToFaction(MagicCharmFaction)      ;adds NPC to friendly faction
	akCaster.StopCombat()                         ;Stops combat 
	akTarget.StopCombat()                         ;Stops combat
	if bMakePlayerTeammate
		akTarget.SetPlayerTeammate(true, false)       ;Turns NPC into player teammate
	endif
	
	if PlayerRaceModifier < 60 && PlayerRaceModifier > 30   ;if the race modifier for the faction is below 60 but not below 30, NPCs get suspicious
		RegisterForSingleUpdate(8)
		GoToState("Suspicious")
		
	elseif PlayerRaceModifier < 30   ;;if the race modifier for the faction is below 30, NPCs discover player disguise and attack
		RegisterForSingleUpdate(8)
		GoToState("Discovered")
		
	endif
	 
EndEvent



State Suspicious
	Event OnUpdate()
		PlayerRaceModifier -= 10        ;Decrement player race modifier on every update to simulate NPCs getting more and more suspicious
		Debug.Notification("Nearby NPCs are getting suspicious of you...")
		if PlayerRaceModifier < 30                      ;if player modifier falls below this, NPCs find disguise
			GoToState("Discovered")
		endif
		RegisterForUpdate(5)
	EndEvent
EndState



State Discovered
	Event OnUpdate()
	TargetActor.RemoveFromFaction(MagicCharmFaction)                                         
		if bMakePlayerTeammate                                             
				TargetActor.SetPlayerTeammate(false, false)                
		endif
	Debug.Notification("You have been discovered!")	
	EndEvent	
EndState


Event OnEffectFinish(Actor akTarget, Actor akCaster)
		UnregisterForUpdate()
		akTarget.RemoveFromFaction(MagicCharmFaction)
		if bMakePlayerTeammate
				akTarget.SetPlayerTeammate(false, false)
		endif
EndEvent

 

 

 

Anything i can do with the above code to improve it's performance? Also if anyone find any errors please point out.

Edited by Magnusen2
Link to comment
Share on other sites

If you want to include custom races at some future point, you may wish to use a formlist or an array for the races and race modifier. Also if you do your PlayerRaceModifier condition checks from lowest value up, you don't need to check to make sure it is between the two numbers.

 

Your script modified using arrays and the adjusted condition checks. I did not check for compilation.

 

 

Scriptname xxxxxStealthSpeechDesguiseFaction extends ActiveMagicEffect  


Faction Property MagicCharmFaction Auto

bool Property bMakePlayerTeammate = false Auto

Actor Property PlayerRef Auto

Race[] Property PlayableRace Auto
Int[] Property PlayableRaceModifier Auto
;the above arrays must having matching indexes
;i.e. index 0 = ArgonianRace and previous value of RaceModifierArgonian

int PlayerRaceModifier

Actor TargetActor


Event OnEffectStart(Actor akTarget, Actor akCaster)

	Race PlayerRace = PlayerRef.GetRace()    
	TargetActor = akTarget

	Int ix = PlayableRace.Length - 1
	While ix >= 0
		If PlayerRace == PlayableRace[ix]
			PlayerRaceModifier = PlayableRaceModifier[ix]
			ix = -1 ; got it so we break while loop
		EndIf
		ix -= 1
	EndWhile
	 
	akTarget.AddToFaction(MagicCharmFaction)      ;adds NPC to friendly faction
	akCaster.StopCombat()                         ;Stops combat 
	akTarget.StopCombat()                         ;Stops combat
	if bMakePlayerTeammate
		akTarget.SetPlayerTeammate(true, false)       ;Turns NPC into player teammate
	endif

;put the lower value first so that if it fails it goes into the next higher level
	if PlayerRaceModifier < 30   ;if the race modifier for the faction is below 30, NPCs discover player disguise and attack
		RegisterForSingleUpdate(8)
		GoToState("Discovered")
	Elseif PlayerRaceModifier < 60   ;if the race modifier for the faction is below 60 but not below 30, NPCs get suspicious
		RegisterForSingleUpdate(8)
		GoToState("Suspicious")
	endif
	 
EndEvent



State Suspicious
	Event OnUpdate()
		PlayerRaceModifier -= 10        ;Decrement player race modifier on every update to simulate NPCs getting more and more suspicious
		Debug.Notification("Nearby NPCs are getting suspicious of you...")
		if PlayerRaceModifier < 30                      ;if player modifier falls below this, NPCs find disguise
			GoToState("Discovered")
		endif
		RegisterForUpdate(5)
	EndEvent
EndState



State Discovered
	Event OnUpdate()
	TargetActor.RemoveFromFaction(MagicCharmFaction)                                         
		if bMakePlayerTeammate                                             
				TargetActor.SetPlayerTeammate(false, false)                
		endif
	Debug.Notification("You have been discovered!")	
	EndEvent	
EndState


Event OnEffectFinish(Actor akTarget, Actor akCaster)
		UnregisterForUpdate()
		akTarget.RemoveFromFaction(MagicCharmFaction)
		if bMakePlayerTeammate
				akTarget.SetPlayerTeammate(false, false)
		endif
EndEvent 

 

 

 

 

Link to comment
Share on other sites

 

 

 

If you want to include custom races at some future point, you may wish to use a formlist or an array for the races and race modifier. Also if you do your PlayerRaceModifier condition checks from lowest value up, you don't need to check to make sure it is between the two numbers.

 

Your script modified using arrays and the adjusted condition checks. I did not check for compilation.

 

 

Scriptname xxxxxStealthSpeechDesguiseFaction extends ActiveMagicEffect  


Faction Property MagicCharmFaction Auto

bool Property bMakePlayerTeammate = false Auto

Actor Property PlayerRef Auto

Race[] Property PlayableRace Auto
Int[] Property PlayableRaceModifier Auto
;the above arrays must having matching indexes
;i.e. index 0 = ArgonianRace and previous value of RaceModifierArgonian

int PlayerRaceModifier

Actor TargetActor


Event OnEffectStart(Actor akTarget, Actor akCaster)

	Race PlayerRace = PlayerRef.GetRace()    
	TargetActor = akTarget

	Int ix = PlayableRace.Length - 1
	While ix >= 0
		If PlayerRace == PlayableRace[ix]
			PlayerRaceModifier = PlayableRaceModifier[ix]
			ix = -1 ; got it so we break while loop
		EndIf
		ix -= 1
	EndWhile
	 
	akTarget.AddToFaction(MagicCharmFaction)      ;adds NPC to friendly faction
	akCaster.StopCombat()                         ;Stops combat 
	akTarget.StopCombat()                         ;Stops combat
	if bMakePlayerTeammate
		akTarget.SetPlayerTeammate(true, false)       ;Turns NPC into player teammate
	endif

;put the lower value first so that if it fails it goes into the next higher level
	if PlayerRaceModifier < 30   ;if the race modifier for the faction is below 30, NPCs discover player disguise and attack
		RegisterForSingleUpdate(8)
		GoToState("Discovered")
	Elseif PlayerRaceModifier < 60   ;if the race modifier for the faction is below 60 but not below 30, NPCs get suspicious
		RegisterForSingleUpdate(8)
		GoToState("Suspicious")
	endif
	 
EndEvent



State Suspicious
	Event OnUpdate()
		PlayerRaceModifier -= 10        ;Decrement player race modifier on every update to simulate NPCs getting more and more suspicious
		Debug.Notification("Nearby NPCs are getting suspicious of you...")
		if PlayerRaceModifier < 30                      ;if player modifier falls below this, NPCs find disguise
			GoToState("Discovered")
		endif
		RegisterForUpdate(5)
	EndEvent
EndState



State Discovered
	Event OnUpdate()
	TargetActor.RemoveFromFaction(MagicCharmFaction)                                         
		if bMakePlayerTeammate                                             
				TargetActor.SetPlayerTeammate(false, false)                
		endif
	Debug.Notification("You have been discovered!")	
	EndEvent	
EndState


Event OnEffectFinish(Actor akTarget, Actor akCaster)
		UnregisterForUpdate()
		akTarget.RemoveFromFaction(MagicCharmFaction)
		if bMakePlayerTeammate
				akTarget.SetPlayerTeammate(false, false)
		endif
EndEvent 

 

 

 

Thank you. That seems to do it. But now there's another problem.

 

I created the effects and spells to test on bandits, The effect gets applied, but they still attack me.

 

This is not the case with certain bandits (The ones on Fort Greymoor), Before i made some edits, they warned my character to back off or they would attack. With the code below they don't. I can even talk to them with no problems.

 

 

 

 

Event OnEffectStart(Actor akTarget, Actor akCaster)

    Race PlayerRace = PlayerRef.GetRace()    
    TargetActor = akTarget

    Int index = PlayableRace.Length - 1
    While index >= 0
        If PlayerRace == PlayableRace[index]
            PlayerRaceModifier = PlayableRaceModifier[index]
            index = -1 ; got it so we break while loop
        EndIf
        index -= 1
    EndWhile
    
    akCaster.StopCombat()
    akTarget.StopCombat()
    akTarget.AddToFaction(MagicCharmFaction)
    akTarget.SetFactionRank(MagicCharmFaction, 0)
   

 

 

How do i make any NPC hit by the effect friendly towards the player?

Edited by Magnusen2
Link to comment
Share on other sites

  • Recently Browsing   0 members

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