Jump to content

[LE] Modifying an actor value via script


Recommended Posts

Whenever I post a question or issue that I'd like help resolving I continue to try and come up with a solution on my own. Case in point: https://forums.nexusmods.com/index.php?/topic/5408450-help-going-from-keyboard-to-controller/?view=getnewpost

 

In my trials to come up with a solution I developed this script. I'd like to know if there could be any issues with it. It seems to work. I wouldn't mind sharing it as an official mod, but I want to be sure that it wouldn't cause issues for situations where the same actor value is modified by other means. If you should see some problems that could break the script, please let me know.

 

 

Scriptname abim_CWRT_ToggleScript extends Quest

Int Property CWRT Auto
{DXScanCode of desired key press} ;276 = A on controller
Float Property CWRT_Walk Auto
{values of 0.0 - 100.0} ;25.0 seems to be close to normal walk pace
Actor Property PlayerRef Auto

Float SpeedDiff

Bool ToggleWalk = false

Event OnInit()
	RegisterForKey(CWRT)
EndEvent

Event OnKeyDown(Int KeyCode)
	If 	KeyCode == CWRT 			\
		&& (!Utility.IsInMenuMode()) 		\
		&& (!UI.IsMenuOpen("Dialogue Menu")) 	\
		&& (!UI.IsMenuOpen("Console"))		\
		&& (!UI.IsMenuOpen("Sleep/Wait Menu"))	\
		&& Game.GetCurrentCrosshairRef() == None
		GoToState("Busy")
		If ToggleWalk == false
			Debug.Notification("Walk mode activated")
			Float SSM = PlayerRef.GetActorValue("SpeedMult")
			SpeedDiff = SSM - CWRT_Walk
			If SpeedDiff > 0
				PlayerRef.ModActorValue("SpeedMult",-SpeedDiff)
				PlayerRef.ModActorValue("CarryWeight",0.1)
				PlayerRef.ModActorValue("CarryWeight",-0.1)
			EndIf
			ToggleWalk = true
		Else
			Float ESM = PlayerRef.GetActorValue("SpeedMult")
			Float SpeedAdjust = ESM + SpeedDiff
			If SpeedAdjust > 100
				SpeedDiff = 100 - ESM
			EndIf
			PlayerRef.ModActorValue("SpeedMult",SpeedDiff)
			PlayerRef.ModActorValue("CarryWeight",0.1)
			PlayerRef.ModActorValue("CarryWeight",-0.1)
			Debug.Notification("Walk mode deactivated")
			ToggleWalk = false
		EndIf
		GoToState("")
	EndIf
EndEvent

State Busy
	Event OnKeyDown(Int KeyCode)
		If KeyCode == CWRT && Game.GetCurrentCrosshairRef() == None
			Debug.Notification("Busy Processing, please wait.")
		EndIf
	EndEvent
EndState 

 

 

Link to comment
Share on other sites

IsharaMeradin your code looks fine to me.

 

The only problem I can see is that by attempting to precisely control SpeedMult you'll conflict with any mod (i.e. Campfire) that provides any spell or ability which can modify speed. It's because you're trying to force the speed to 25 then restore it to 100. I think you would be better off simply doing a flat reduction by 75 and then restore 75 points.

 

I don't think the Busy state is needed if you simply move the assignment of ToggleWalk to be the first thing changed. Here's what I would try.

Scriptname abim_CWRT_ToggleScript extends Quest

Int Property CWRT Auto
{DXScanCode of desired key press} ;276 = A on controller
Float Property CWRT_Walk_Adjustment Auto
{Reducing by 75.0 seems to be close to normal walk pace}
Actor Property PlayerRef Auto

Bool ToggleWalk = false

Event OnInit()
	RegisterForKey(CWRT)
EndEvent

Event OnKeyDown(Int KeyCode)
	If KeyCode == CWRT 				 \
		&& Game.GetCurrentCrosshairRef() == None \
		&& (!Utility.IsInMenuMode()) 		 \
		&& (!UI.IsMenuOpen("Dialogue Menu"))
		If ToggleWalk == false
			ToggleWalk = true
			PlayerRef.ModActorValue("SpeedMult",-CWRT_Walk_Adjustment)
			PlayerRef.ModActorValue("CarryWeight",0.1)
			PlayerRef.ModActorValue("CarryWeight",-0.1)
			Debug.Notification("Walk mode activated")
		Else
			ToggleWalk = false
			PlayerRef.ModActorValue("SpeedMult",CWRT_Walk_Adjustment)
			PlayerRef.ModActorValue("CarryWeight",0.1)
			PlayerRef.ModActorValue("CarryWeight",-0.1)
			Debug.Notification("Walk mode deactivated")
		EndIf
	EndIf
EndEvent

If I'm wrong and there are timing issues you have to switch to the Busy state before you call any functions which means you would need to write it like this:

Event OnKeyDown(Int KeyCode)
	If KeyCode == CWRT 	
		GoToState("Busy")
		If Game.GetCurrentCrosshairRef() == None \
			; ...
		EndIf
		GoToState("")
	EndIf
EndEvent
Link to comment
Share on other sites

I had already realized that about modifying the speedmult and trying to set it back to 100. That part has since been changed.

 

As far as the state, I don't think it really is needed either. I had included it as a precaution but obviously set it up wrong. States have never been my strong point.

 

One thing I did add was comparing the player's inventory weight to their carry weight and blocking the toggle if the player was over encumbered. The reason being that the player walks by default when carrying too much and if it is activated while carrying too much it halts the player's movement completely.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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