KJA1258 Posted November 15, 2014 Share Posted November 15, 2014 (edited) 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 November 15, 2014 by KJA1258 Link to comment Share on other sites More sharing options...
golem09 Posted November 23, 2014 Share Posted November 23, 2014 (edited) 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 November 23, 2014 by golem09 Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 23, 2014 Share Posted November 23, 2014 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 asIf (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 More sharing options...
KJA1258 Posted November 23, 2014 Author Share Posted November 23, 2014 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 More sharing options...
IsharaMeradin Posted November 23, 2014 Share Posted November 23, 2014 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 can1. be certain that the value is constant throughout the current script execution2. reduce processing time. Link to comment Share on other sites More sharing options...
Terra Nova Posted November 23, 2014 Share Posted November 23, 2014 Or just use the PlayerRef actor property, which is 1000 times faster than Game.GetPlayer() and can be called as many times as needed without having to store it, unlike Game.GetPlayer(). Link to comment Share on other sites More sharing options...
KJA1258 Posted November 23, 2014 Author Share Posted November 23, 2014 Was about to ask which is faster, property or storing Game.GetPlayer() in a variable (or pointer? since Papyrus is object oriented). Thanks again for everybody that answered, helps a lot while still learning Papyrus. Wiki's a stub. Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 23, 2014 Share Posted November 23, 2014 Property would be fastest. IMHO Link to comment Share on other sites More sharing options...
KJA1258 Posted November 24, 2014 Author Share Posted November 24, 2014 What does IMHO mean? I've seen that acronym quiet often, but as an ESL student I don't know what it means. Link to comment Share on other sites More sharing options...
IsharaMeradin Posted November 24, 2014 Share Posted November 24, 2014 (edited) In My Humble Opinion. Some might refer to the H as honest, humorous or any other "fitting" adjective that starts with an H. Edited November 24, 2014 by IsharaMeradin Link to comment Share on other sites More sharing options...
Recommended Posts