Jump to content

Papyrus ignores IF condition


KJA1258

Recommended Posts

Hello everyone.

The topic title suggest a very unusual problem (if you can call it that way) I've stumbled upon while writing a Papyrus script. Although I'm new to Papyrus, I'm no complete beginner to programming.

Concerning the error/problem, it appears as if Papyrus simply ignores the if condition.

 

Here is the code:

Scriptname AutoCombatCamPlayerScript extends Quest  
{Forces Player into FP Cam on Combat Initiation and when Bow or XBow is drawn}

GlobalVariable Property myKey Auto

int camState
int hotkey = 76
int enabled

Event OnInit()
	Debug.Notification("Started AutoCombatCam")
	RegisterForSingleUpdate(1)
EndEvent

Event OnKeyDown(Int KeyCode)
	If  Input.IsKeyPressed(hotkey) ; Only run code when the status changes
		Debug.Notification("Hotkey Pressed")
		If enabled == 1
			Debug.Notification("Turned On")
			RegisterForUpdate(1)
			Debug.Notification("Started")
			enabled = 0
		Else ; If enabled == False
			Debug.Notification("Turned Off")
			UnregisterForUpdate()
			Debug.Notification("Killed")
			enabled = 1
		EndIf
	EndIf
EndEvent

Event OnUpdate()
	Debug.Notification("EXEC")
	RegisterForKey(hotkey)

	hotkey = myKey.getValueInt()
	If enabled == 0
		If (Game.GetPlayer()).IsInCombat()
				Game.ForceFirstPerson()
		Else
			;Not in combat, switch to FP on Bow and XBow draw
			If (Game.GetPlayer()).IsWeaponDrawn()
				If (Game.GetPlayer()).GetEquippedItemType(0) == 7
					Game.ForceFirstPerson()
				ElseIf (Game.GetPlayer()).GetEquippedItemType(0) == 12
					Game.ForceFirstPerson()
				EndIf
			EndIf
		 EndIf
	EndIf
	RegisterForSingleUpdate(1)
EndEvent

I would be pleased if anyone could figure this out, since I'm, though knowing Obj-C, too inexperienced to know why this is caused or to resolve it.

 

EDIT: I should have mentioned which IF statement it actually is. The affected statement is the

if enabled == 0

in the onUpdate() event.

Kilian

Edited by KJA1258
Link to comment
Share on other sites

  • 2 weeks later...

You are going pretty wild with your brackets. Every if statements has to have brackets around the whole condition(s)

 

if (enabled == 0)

 

would be correct.

 

Around your Game.GetPlayer() you always have too many brackets:

 

If (Game.GetPlayer()).IsWeaponDrawn()

 

It should only be:

 

If (Game.GetPlayer().IsWeaponDrawn()

Edited by golem09
Link to comment
Share on other sites

It may be that it is not being ignored but the subsequent conditions are failing? Try adding a debug.trace or notification statement for testing purposes immediately after the If enabled == 0 line in question. Ensure that the condition is being triggered.

 

Anyway, I'd consider cleaning it up a bit.

 

from this

 

 

		If (Game.GetPlayer()).IsInCombat()
				Game.ForceFirstPerson()
		Else
			;Not in combat, switch to FP on Bow and XBow draw
			If (Game.GetPlayer()).IsWeaponDrawn()
				If (Game.GetPlayer()).GetEquippedItemType(0) == 7
					Game.ForceFirstPerson()
				ElseIf (Game.GetPlayer()).GetEquippedItemType(0) == 12
					Game.ForceFirstPerson()
				EndIf
			EndIf
		 EndIf 

to this

		Actor PlayerRef = Game.GetPlayer()
		Int ItemType = PlayerRef.GetEquippedItemType(0)
		If 	PlayerRef.IsInCombat()	\
		||	(	!(PlayerRef.IsInCombat())	\
			&&	PlayerRef.IsWeaponDrawn()	\
			&&	(ItemType == 7 || ItemType == 12)	)
			Game.ForceFirstPerson()
		EndIf

All three conditions were triggering the exact same function. Better to use logical operators to shorten up the code and call the function in only one location. I split the condition line up so that it would be easier to see how they group together than to have one long line that might be difficult to read.

 

It should be noted that Every if statements has to have brackets around the whole condition(s) is not true. Only conditions that MUST have parenthesis around them are those that NEED to be kept separate from any others. Note the rewritten example above.

 

If enabled == 0

would have the same result as

If (enabled == 0)

 

Alternatively, can swap the integer out for a bool. Since all you are doing is toggling between 0 & 1, you can just as easily toggle between true & false.

Link to comment
Share on other sites

Thanks a lot for the input. It was actually triggering correctly, I just renamed the file and script before and that kinda messed Skyrim up.

I will clean it up, however, my example here is by now legacy code.

Thanks for the logical operators, I couldn't find them anywhere. Interestingly, they are the same as Objective-C, my native language.

 

As a side question, is it better to call

Actor PlayerRef = Game.GetPlayer()

instead of Game.GetPlayer() directly where I need it?

Link to comment
Share on other sites

Game.GetPlayer() causes the current thread go out to the Game script to process the GetPlayer() function. Once is not an issue. A couple times not that big of a deal. But a lot of them can increase the amount of processing time needed to execute the script. Sure, we are talking about milliseconds or nanoseconds here but still why bog things down unnecessarily.

 

If you get it once and store it in a variable you can

1. be certain that the value is constant throughout the current script execution

2. reduce processing time.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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