Jump to content

Is this If script sufficient, or does it need an ElseIf?


geekminxen

Recommended Posts

I'm trying to prevent an intermittent problem with a mod where a follower gives the player a custom perk twice in a row and then the game freezes. I got advice to add an "If" statement to the dialogue result script where the perk is given, but I'm wondering if it needs an ElseIf, as well. This is what it is right now:

 

If Player.HasPerk CustomPerk == 0
Player.AddPerk CustomPerk
ShowMessage CustomPerkAdd
EndIf

Do I need an "ElseIf Player.HasPerk CustomPerk == 1" line before the EndIf?

 

Thanks for any help. I haven't modded enough for any of this to really stick with me, so I appreciate the guidance.

Link to comment
Share on other sites

Use an "ElseIf" only if you want to "test" before you do something in the case where the player already has that perk. Otherwise use a simple "Else".

An "if /elseif/else" structure is a way of handling a series of specific situations where only one condition can be true and you want to ensure you have covered all the possibilities. So think of it as:

If (test condition1 is true) then
  take action1
ElseIf (test condition2 is true) then
  take action2
ElseIf (as many test conditions as needed)
  ...
Else (no anticipated test conditions are true)
  take some alternative action indicating "no anticipated result"
EndIf (this test is done)

If you do not want to take any action if none of those "test conditions" are true, but don't need to do something when they all fail, then you don't need an "Else" at all. However, it is often a good idea to place a "debug" message in an "Else" section to indicate that the tests all failed, at least until you know that part of the script is working as intended. Otherwise the tests all fail and the "EndIf" terminates the test block with no actions or message and "falls through" to the next line of code.

The main point is that an "ElseIF" requires another test conditoin. An "Else" will only process if no other "If" or "ElseIf" statements were true. It is a "failsafe" for indicating that the test section was processed.

-Dubious-

Link to comment
Share on other sites

  • Recently Browsing   0 members

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