Jump to content

DOWNLOAD MODS

Are you looking for something shiny for your load order? We have many exclusive mods and resources you won't find anywhere else. Start your search now...

LEARN MODDING

Ready to try your hand at making your own mod creations? Visit the Enclave, the original ES/FO modding school, and learn the tricks of the trade from veteran modders...

JOIN THE ALLIANCE

Membership is free and registering unlocks image galleries, project hosting, live chat, unlimited downloads, & more...

Class #2 - Whats up with If, ElseIf, Else and EndIf


WillieSea
 Share

Recommended Posts

 

 

The Mysterious IF and His Buddies

ElseIf, Else and EndIf

 

Class Two

 

This topic will cover the basic conditional commands of the IF family.

 

 

 

Chapter One - Conditional Logic

 

 

What is conditional Logic? Think of it as layers of a sandwich. Think of it as choices that must be made.

 

gallery_85_79_36670.jpg

 

The IF statement allows you to execute (or not execute) a block of script commands based on one or more comparisons that you specify.

 

What this means is, you can specify WHEN the block of script commands will 'execute'. ( Execute means to run, or perform the script commands.)

 

You do this by using Comparison Operators to specify the 'when'. Operators such as EQUAL TO, GREATER THAN, LESS THAN or NOT EQUAL TO.

 

These Operators use special characters to represent the English meaning.

Operator and Description

== Exactly equal to

!= Not equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

 

This is a BIG concept to understand. The next chapter will attempt to make sense of it.

 

 

 

 

 

Chapter Two - That Tricky IF

The Very Basics

 

 

If is defined as follows:

Pronunciation: \ˈif, əf\

Function: conjunction

Etymology: Middle English, from Old English gif; akin to Old High German ibu if

Date: before 12th century

1 a: in the event that b: allowing that c: on the assumption that d: on condition that

2: whether [asked] [i doubt if I'll pass the course]

3 used as a function word to introduce an exclamation expressing a wish [if]

4: even though : although perhaps [an]

5: and perhaps not even [few] often used with not [difficult] if anything : on the contrary even : perhaps even [if]

 

Now, if you think that is confusing, well you're in for a surprise.

 

Important Note

The IF command simply is the word used to start a set of questions that will answer true or false. Only true conditions will make the IF statement true. When the entire 'If' statement is true, then the contents of the code after the if will execute. I will call this 'executing its contents'. It is the 'do actions' part of the following examples. Think of 'do actions' as the part of the script that does the work.

 

For example:

If ( it is raining ) do actions

If ( it is night time ) do actions

If ( the player has more than 500 gold coins ) do actions

 

For a little more complexity, you can add the 'and' and the 'or' question into the mix.

 

If ( the player is female ) and (( the main quest is finished ) or( the main quest has not started yet )) do actions

 

The parenthesis can be added to group questions together. In the example above, the main quest questions will be evaluated as one.

 

(( the main quest is finished ) or ( the main quest has not started yet ))

 

Since there is an 'or' in the question, either of them can be true statements and the whole block between the parenthesis will become a true statement.

 

Lets pick that set of questions apart a bit more.

If - All questions start with this. Use it well.

 

( the player is female ) - A question is asked, is the player a female? This is true or false. Since the question is by itself, with no parenthesis blocking around it, it must be true for the If statement to be true. This command would only execute its contents when the player character is a female.

 

and The and is used to combine questions. With an and, the questions on both sides of the 'and' must be true for the script to execute its contents.

 

( This is the block parenthesis around the next two questions.

 

( the main quest is finished ) This is the left question.

 

or This says that either the question to the left, or the question to the right can be true to make the If statement true and execute its package.

 

( the main quest has not started yet ) This is the right question.

 

) This is the end block parenthesis that closes off the two questions.

 

Confused? Your not alone. This can be a complex concept to comprehend.

 

Lets try some examples.

The following statements will use this image and ask questions about the individual items.

 

gallery_85_79_22757.jpg

 

If ( A == color purple ) -> This is True The gem A color equals purple.

 

If ( D == heart shape ) -> This is True The gem D shape equals a heart.

 

If ( B == diamond shape ) and ( H == diamond shape ) -> This is True The gem B shape equals a diamond, AND the gem H shape equals a diamond. Since both questions are true (because of the 'and' between the questions), it makes the whole statement true.

 

If ( A > B ) -> This is False. The value of 'A' which is '1' is less than the value of 'B' which is 2. The question asked was 'greater than', not 'less than'. That is why it is false.

 

If ( D == Orange ) and ( F == Purple ) -> This is False. The gem D color equals orange, but the F gem color does not equal purple. The AND between the questions means both must be true for the whole statement to be true. Since both questions are NOT true, the whole statement is False.

 

Lets look at that same question, but with an OR between the questions.

 

If ( D == Orange ) or ( F == Purple ) -> This is True Even though one question is false, the OR says either can be true to make the whole IF statement true. Since the D color is equal to orange, the whole statement is now true.

 

 

 

 

 

Chapter Three - Coding

IF ENDIF

 

 

As I stated in the previous chapter, you cannot have an IF without the ENDIF. You also cannot have the ENDIF without the IF. Your script will not save.

 

Lets start with a simple script. You can use your knowledge gained in the first course to test your scripts in game.

And remember, you must give your script a name and place it inside a BEGIN command.

 

 

 
scn MySecondScript

Begin OnActivate

   if player.GetItemCount Gold001 > 1000

      MessageBox "You have more than 1000 gold septims!"

   endif

End




 

 :smarty:Smarty Says: Some rules I have with scripts and using the IF ENDIF logic. Indent your code. - This is the most important rule I have. It makes the structure of your code clear and easily readable. It also helps you quickly determine if you might have formatting errors.

 

As you may have guessed, this script will display a pop-up message on the screen if the player has more than 1000 gold. If the player has less than 1000 gold, nothing at all will happen. This is where the ELSE or the ELSEIF come in handy.

 

ELSE

When you want to do something 'else' when the IF statement is false, you code an ELSE.

The ELSE will run anytime the IF does not run.

 

 
 
scn MySecondScript
Begin OnActivate
if player.GetItemCount Gold001 > 1000
MessageBox "You have more than 1000 gold septims!"
else
MessageBox "You have less than 1000 gold septims!"
endif
End
Now, you will get a message if you have more than 1000 gold, and a message if you don't have more than 1000 gold. As you may have guessed, the IF ELSE ENDIF code is very important to just about every script created. Any time you need to CHECK something before doing something, these guys will come in handy. ELSEIF But, what is the ELSEIF used for? Sometimes, you have more than one thing you want to check. Instead of putting IF code inside of IF code, like this:
if a == 1 

   player.AddItem Gold001 100

endif

if a == 2

   player.AddItem Gold001 500

endif

if a == 3

   player.AddItem Gold001 1000

endif

if a == 4

   player.AddItem Gold001 5000

endif

if ( a < 1 ) || ( a > 4 )

   player.AddItem Gold001 2

endif

[/code]
 
 :smarty:Smarty Says: The last IF condition is a catch-all. Since the previous checks look for an exact value of 'a', there is a chance that none of them would be true. The Last IF will catch any value of 'a' that is less than '1', or greater than '4'.   You can do this instead:  
 
 
 
if a == 1
player.AddItem Gold001 100
elseif a == 2
player.AddItem Gold001 500
elseif a == 3
player.AddItem Gold001 1000
elseif a == 4
player.AddItem Gold001 5000
else
player.AddItem Gold001 2
endif
 

This is a bit more effecient than having several IF commands

:smarty: Smarty Says: Using the ELSIF has one added benefit. When the first TRUE statement is found, the entire IF logic is then passed to the ENDIF. What this means is, lets say 'a' equals '2'.
1. 'a' will be checked against the value '1'. This is false.
2. 'a' will be checked against the value '2'. This is true.
3. The player will be given 500 gold coins.
4. The code will skip down to the ENDIF.
None of the other conditions need to be checked. This will/could make your code much more effecient and quicker to run.

:smarty: Smarty Says: You should always put the condition that will be true most of the time at the top of the list, and the least likely to be true at the bottom of the list.

 

;) - HOMEWORK - :ahoy:

Its your turn to see if you catch the concepts here.
Assignment: Write a script that will give the player a weapon based on their infamy or fame score.

Some commands you will need to use. Click on these links if you don't understand how to use them to look at the WIKI page.
Begin
Set
GetPCFame
GetPCInfamy
IF ELSE ELSEIF ENDIF Look at the useage of the '||' or symbols. Its two bars '|' next to each other.
Message
Short

Some more information:
1. You will need to create two variables. One to store the players infamy, and one to store the players fame scores.
2. Get the players infamy and fame scores and store them in the variables.
3. Display a message if the player cannot pick up the sword because their infamy or fame are not high enough. Either must be over 50 points. Display a message saying they do not have enough recognition in the world.
4. When checking the players fame and infamy scores, if either are greater than 50, add the proper sword to the player.
5. After you determine one of the scores is greater than 50, If the players infamy is greater than the players fame, give the player the 'DremoraLongSwordEnchFireDmg' sword.
6. After you determine one of the scores is greater than 50, If the players fame is greater than the players infamy, give the player the 'EnchEbonyLongswordStorm' sword.
7. When a sword is added to the player inventory, use a 'doOnce' variable to not allow the player to get more swords from the activator.
8. Use TABS to indent your code properly.

Hints:
1. In my version of the script, I have three nested IF/ENDIF statements. Nesting means you have IF/ENDIF statements inside of other IF/ENDIF's.
This is an example of nesting IF/ENDIF statements.

 
if ( c == 0 )

	If ( a > 10 ) || ( b > 10 )

		if ( a > b )

			;some code

		else

			;some code

		endif

	else

		;some code

	endif

else

	;some code

endif
 


2. To add an item to the player, use the 'player.' reference in front of the AddItem commands.

 

3. To create a variable to be used, use the 'short' function and then supply a name you want to use.

 

 
short doOnce

 

4. To make a variable equal to some specific value, use the 'set' command.

 
set doOnce to 1

 

5. Remember, all variables are initialized at zero, so use that knowledge to your benefit. Using a doOnce variable can allow you to only run the script once. Setting it to another value when a sword is added to the players inventory can prevent the script from running an endless number of times.

  • Upvote 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 4 weeks later...

@Happy-i-am

See step three. You need to make a storage variable name to store the fame points.

Now, you must 'get' the current player fame point and store it in that vaiable.

At the top of the assignment, I have 'links' to the different commands you will have to use. Take a look at the GetPCFame command.

Then look at step four to set your variable you created to the value of the command. (The WIKI for GetPCFame has an example also.)

If you need more help, let me know.

Link to comment
Share on other sites

umm yeah still confused.... this is what i have got  

scn Homeworkshort doOnceBegin onactivate 	if getpcfame >= 50 		MessageBox "You're famous enough!" 			Player.additem 00035E76 1	set DoOnce to 1	 elseif GetPCInfamy <= -50 		MessageBox "You're famous enough!" 			Player.additem 00035E76 1	set DoOnce to 1	else 		MessageBox "You're not famous enough!" 	endif End 

i know theirs no storeing

Link to comment
Share on other sites

The homework is very difficult, I know, so no worries there. You have a very good start on the script.

Some 'Teacher' notes:

Its usually best to 'store' a function value returned instead of calling it over and over. Perhaps its just the way I was trained to code. Not sure...

Anyway, where you have your 'short doOnce' is where you would also declare your fields for storeing the fame and infamy.

Short plrFame

Short plrInfamy

Then you set the function returned value into the new variable you just created.

set plrInfamy to ( getpcinfamy )

set plrFame to ( getpcfame )

In your 'AddItem' code, use the base object name, not the formID. The formID is only used when you are in the console.

The GetPCInfamy should only contain positive values. (You have a -50 in your IF statement)

Keep up the great work and you will soon get it! :pints:

Link to comment
Share on other sites

  • 4 weeks later...
  • 7 months later...

Very true cbh.

I feel it is the programmers responsibility to ensure that the values you are testing in your IF conditions contain valid values. Most function calls 'will' return a valid value, such as the GetAV function. But, putting an invalid reference name in front of it will cause issues as you state with the entire IF statement.

Splitting the conditions into two statement lines is a perfect way to solve the issue. Thank you for bringing this topic up, I will look into integrating it into the class above.

Link to comment
Share on other sites

  • 5 months later...
  • 1 month later...
  • 1 month later...
  • 1 month later...

i took mine a step further and added an elseif (a==b )type comparison also.

i also used lockpicks and repair hammers as the reward items rather then the swords.

I also kept the fame and infamy requirements low for testing purposes.

also williesea you might want to let the student know they need to also supply an number for the items given.

Example: additem Lockpick 1

otherwise additem lockpick throws an error at you. just a thought. great class by the way.

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 months later...

I took mine a little differently, here it is!

[removed script example]

I figured Divines for fame Daedra for infamy, and if you have a bit of both, Daedra for that too!

EDIT: Posting screws up the formating... Oh well, I assure you they are indented lol.

Edited by WillieSea
Removed Script example
Link to comment
Share on other sites

  • 8 months later...

i understand the scn the mesage box and the begen scrept

and part of the if stuff im not understanding the vereable or the get infamy and fame

how do i send you the scrept if i get ii have got atlest part of it

Edited by darlaten
Link to comment
Share on other sites

You can send me your script in a Private Message.

Variable - This is a 'named object' that holds a numeric value for you, like a box. That is all it does. But you can use that box in math functions or in codition checks if something is true or false.

The getting of the infamy or fame is a 'command' that can be called. It returns a value that you can then store in a variable.

Link to comment
Share on other sites

  • 3 weeks later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...