Okay, guys, by request of Flatlander, I've compiled a short tutorial on the use of variables included with the V109 beta 3 release.
Now, to start off ... the title, of course:
Tutorial # Something: Variables
Yeah, I lost track of what tutorial number this is.
Anywho, let's get started!
To begin, I'll list the added conditions and actions that have to deal with variables:
Conditions:
VAREQUAL=X
- returns true if the current variable equals X
VARNOTEQUAL=X
- returns true if the current variable does not equal X
VARGREATER=X
- returns true if the current variable is greater than X
VARLESS=X
- returns true if the current variable is less than X
Actions:
GLOBALVAR=X
- set the current local variable index to wrire/read from to X
LOCALVAR=X
- set the current local variable index to wrire/read from to X
SETVAR=X
- set the value of the current variable to X
INCVAR=X
- increases the value of the current variable by X
DECVAR=X
- decreases the value of the current variable by X
Alright, that is verbatim from Lee's update for V109beta3.
Let us speculate a little about this. I have not used these variables, so let's not count on my own experience to get through this. Have no fear, though, we have Lee's examples to give us some insight!
Let us open the "pickuplockpicks.fpi" that was included with V109beta3:
;Artificial Intelligence Script
;Header
desc = Pickup Lock Picks (for doors that can be picked) [G1]
;Triggers
:state=0,plrdistwithin=40:state=1,playertake,coloff,plrsound=audiobank\misc\ping.wav
:state=1:rundecal=5,globalvar=1,setvar=1
;End of Script
Well, that looks pretty simple. The first line is a generic pickup sequence used in a lot of default scripts for picking stuff up.
The second line, however, is where we see some of our new commands:
:state=1:rundecal=5,globalvar=1,setvar=1
According to our code, we move into this state once the player picks up the item.
Once we get here, we can see these two actions being used: globalvar and setvar.
What are they doing, though?
The GlobalVar action can be a bit confusing at first. What it does is tell FPSC which variable we are wanting to alter. In this case, we want to alter "Global Variable #1", which is why the script says "globalvar=1".
The SetVar action goes hand in hand with the previous action. Once FPSC knows which variable you are wanting to change, this commands does just that. It changes it. It will set the variable to the value in which you specify. So for the current circumstances, this action is setting "Global Variable #1" to a value of 1.
Great! Now, we know that once the player picks up the entity with this script attached to it, then the "Global Variable #1" will be equal to 1!
Moving on with our examples, we have "doorlockpick.fpi"
Let's take a look inside, shall we?
;Artificial Intelligence Script
;Header
desc = Door requiring lock picking (variable used for 'attempts')
;Triggers
;If global variable [G1] is zero (no lock pick), always fail lock picking
:state=0:globalvar=1
:state=0,varequal=0:localvar=1,setvar=1
;If local attempts variable [L1] is not reset, can successfully lock pick door
:state=0:localvar=1
:state=0,plrdistwithin=120,plrusingaction=1,varequal=0:setvar=1,sound=$0
:state=0,plrdistwithin=120,plrusingaction=1,varequal=10:incvar=1,sound=$0
:state=0,plrdistwithin=120,plrusingaction=1,varequal=20:incvar=1,sound=$0
:state=0,plrdistwithin=120,plrusingaction=1,varnotequal=0:incvar=1
:state=0,vargreater=30:state=1
;Door opens
:state=1:incframe=0
:state=1,frameatend=0:state=2,coloff,localvar=1,setvar=20
;Trigger door to close at random point when lock pick effect fades
:state=2:localvar=1
:state=2,random=25,varless=21:decvar=1
:state=2:state=3
:state=3,varnotequal=0:state=2
;Door closes
:state=3:localvar=1
:state=3,varequal=0:decframe=0
:state=3,frameatstart=0:state=0,setframe=0,colon
;End of Script
Whoa, that's a lot of code for a simple door!
But, this isn't just any door. It's a door that the player needs lock picks in order to open!
Now, remembering what we talked about with the other script will help us to decypher this one.
Here's a few rules to go by when reading the parts dealing about variables:
- The variable to be checked or altered is ALWAYS the one that was last set by either "globalvar" or "localvar"
- "Global Variable #1" is DIFFERENT than "Local Variable #1" (Pretty obvious)
- ALWAYS set the variable to be altered before trying to do anything with it. That includes checking it.
Okay, with that said, let's recall that in the other script, "Global Variable #1" will only be set to 1 if the player picked up some lock picks.
Now we can start to read this puppy from the top, starting with state 0.
- Set the variable to be used to "Global Variable #1"
- If the variable is set to 0 (Meaning we HAVEN'T picked up lock picks), then set "Local Variable #1" as the variable to be use, and set it to 1.
- Set the variable to be used to "Local Variable #1" as the variable to be used.
- If the player is close to the door and using the action key AND "Local Variable #1" is equal to 0, then set the variable to 1 and play a sound defined in the entity.
- If the player is close to the door and using the action key AND "Local Variable #1" is equal to 10, then increase the variable's value by 1 and play a sound defined in the entity.
- If the player is close to the door and using the action key AND "Local Variable #1" is equal to 20, then increase the variable's value by 1 and play a sound defined in the entity.
- If the player is close to the door and using the action key AND "Local Variable #1" is NOT equal to 0, then increase the variable's value by 1.
- If "Local Variable #1" is greater than 30, then go to state 1.
That sure does a lot.
Basically, while the user is within range and holds down the use key, they'll hear a couple sounds play while the variable is increasing in value until it gets to 30, then it'll go to state 1.
State 1, translated:
- Increase frames through animation 0
- If animation 0 is at the end, then turn the collision of the door off (So we can walk through it), set "Local Variable #1" to 20, and go to state 2.
That's pretty basic operation of opening the door.
States 2 & 3, translated:
State 2:
- Set the variable to be used to "Local Variable #1"
- If a 25 sided die is rolled and lands with 1 facing up and "Local Variable #1" is less than 21, then decrease "Local Variable #1" by 1.
- Go to state 3.
State 3:
- If "Local Variable #1" is not equal to 0, then go back to state 2.
- Set the variable to be used to "Local Variable #1"
- If the variable is equal to 0, then decrease frames through animation 0
- If animation 0 is back at the start, then set the frame to 0, turn collision back on (So we can't pass through the door), and go to state 0.
This is basically to close the door at a random point in time, and give the player enough time to get through.
Welp, that about does it for usage of variables.
What have we learned so far?
- How to set variables to be used.
- How to check variables.
- How to set and manipulate variables.
- To always set the variable to be used before trying to use it.
Have fun! And Good Luck!!!
The one and only,
~PlystirE~