                         THE THIRD DIMENSION

Easy Variables and ideas on how to use them.
--------------------------------------------

     If you are creating a game and have never used any variables 
before then don't worry! I will show you various ways to use easy 
variables even if you don't know what they are or how they work.I 
will also explain about variables and how to use easy variable 
routines.So lets get a variable into your game straight away. 

Create an Initial Condition in kit2 ( kit 1 see below!) then 
Edit the initial condition and give it this line of text-

               SETVAR(O,V2O)

If you are using kit 1 then create a General Condition and add 
the above code to it,then set the Default Initial condition to 
whatever the number of the general condition is that you have just 
created (Probably Condition 1).
     Now RESET your game and try and shoot anything.You can't 
do it because we have just used a variable that disables the 
shooting ( which is variable V2O ).
     That shows how easy it is to use a variable.Very easy!

     So how can we best describe a variable so that you know how 
it works? A  variable ( able to vary ) is like the weather.It can 
be sunny one day and raining the next,in other words it varies 
and that is just what a variable is like.You can make a variable 
change just as the weather does.The best thing is that YOU can 
change your variables to a different state just like God can 
change the state of the weather.You too can change the weather,
that's if you can do a rain dance.(Yes I have mentioned some of 
this before but there are a lot of new things in here too!!)
So if our weather was sunny before you added the line of code 
above because you could go around shooting anything then i have 
suddenly made it rain because now you can't.HA! So now we need to 
make the sun shine again by altering the same variable so that 
you can shoot again.So create a cube and give it this condition-

              IF ACTIVATED? SETVAR (15,V2O)

This line of code is saying that if you touch the cube the 
computer will set our shooting variable ( variable V2O ) back 
to firing again.So try it out by touching the cube then firing.
Yipee! its sunny again.Want to try it again? Press the reset 
button.
     So the SETVAR( set the variable ) command is setting our 
variable ( V2O )from one state ( being able to fire which is 15 )
to another state ( not being able to fire which is O ).E.g.-

     SETVAR (O,V2O) disables firing.
     SETVAR (15,V2O) enables firing. 

     Not very exciting is it? but just think if the cube was made 
into a gun with other objects.You could then start your game off 
with no weapons available so you cannot fire,then find the gun and 
add an Object Condition to enable you to fire.

System and User Definable Variables.
------------------------------------

     There are two types of variables.There are System variables 
and User defined variables.The system variables are reserved for 
use by the Freescape system itself ( although you can alter them 
for your own use ).These are all listed in your user
manual and they range from VO ( variable zero ) to V29. We saw 
last month how to use one of the system variables to 
disable/enable firing.The system variables ( which are the first 
3O ) can also be used to detect your presence,move you around and 
simplify many other functions that you may use in your games.
     User defined variables start from 3O to 256 in kit 1 and 3O 
to 512 in kit 2.You can set these up yourself and use them in 
instruments,to hold values and a lot more.We will be looking at 
some examples to help you understand these and how they work.
     Here is one use for a user defined variable.This routine 
will count how many times that you have collided with an object 
and when it reaches a certain number it cause something else to 
happen e.g when it reaches 2O it could end the game.

Start by making an Initial condition in kit 2 (or a general 
condition 1 in kit 1 which you must then set the initial condition 
in the default menu to 1)then edit it and give it this bit of code.-

     SETVAR (O,V33)

This is telling the computer to set variable V33 to hold the 
value of zero.We use variable V33 because that is how old i am 
and simply because i want to.The variable is used in an initial 
condition (or general condition 1 in kit 1) because i want the 
computer to execute this command as soon as the game starts up at 
the beginning and to work once.
Next we want to add another condition.This time it will be a 
General condition.This  will be General condition 2 in kit 1 or 
General condition 1 in kit 2.Now give it this line of code.-

     IF VAR=? (2O,V33)
     THEN ENDGAME
     ENDIF

This is telling the computer to keep an eye on variable V33 and 
if it reaches 2O then end the game.
Now we need to make the variable V33 reach 2O as it starts at 
zero.
Create a rectangle then move it onto the floor like a big doormat 
then give it this condition.-

     IF COLLIDED?
     THEN ADDVAR (1,V33)
     SOUND(3)
     ENDIF 

Now walk over the rectangle twenty times and the game should 
end.If it does not then raise the rectangle up from off the floor 
a little then try it again.
Wow how exciting you may say but you could have lots of these 
rectangles scattered throughout your game or even apply the same 
condition to walls etc.You could also use it in conjunction with 
an instrument which actually shows the variable working.

A point to note is that variables can be very fussy about how 
they are set out.You may find that when you have made some code 
that includes a variable, it just does not work when you know that 
it should.The answer is to swap the number concerning the 
variable around.E.g-

     SUBVAR(1,V44)    change to          SUBVAR(V44,1)

It should then work if it previously did not.

Here is another example of using a System variable ( remember 
these are variables from O to 29 ).

First thing to do is to clear all previous code then create a 
cube (object 2).Then create a General condition then edit it to 
give it this condition.-

     IF VAR=? (15,V21)
     THEN INVIS (2)
     ENDIF 

This means that if you shoot anywhere fifteen times then the cube 
becomes invisible.Try it out!Reset the area and try again.If 
you have tried it out 5 million times then get some therapy!

The 'IF VAR=? (15,V21)' command is saying that if the variable 
V21 equals a certain amount ( in this case 15 ) you have shot 15 
times then make the cube invisible.Why do we have an ENDIF 
command? The answer is that an Endif command tells the computer 
that it is the end of the IF statement that starts on line 1.  

Right lets clear everything again then start again, this time 
creating 2 cubes side by side.colour one red and the other 
green.Or if you want,one bright purple and the other bright pink 
( i won't tell anyone ).Now create an Initial condition and edit 
it so that it has the following-
     
     SETVAR (7,V5O) 

We use V5O because that is how old i look.HA!
Create a general condition and give it this code-

     IF VAR=? (15,V5O)
     THEN SOUND(5)
     ENDIF

Now give the red cube this condition-

     IF SHOT?
     ADDVAR (1,V5O)
     ENDIF

Give the green cube this condition-

     IF SHOT?
     SUBVAR (1,V5O)
     ENDIF

Now by shooting the Red cube you are adding 1 each time to 
Variable V5O and by shooting the Green cube you are taking one 
away each time.The general condition says that if the variable 
V5O reaches 15 then it will make the sound number 5 play.
    
      So now you should know how to use the ADDVAR,SUBVAR,SETVAR 
and IF VAR=?  commands and also know the difference between 
System variables and User defined variables.

Here are some more variable routines/ideas.-

MOVEMENT VARIABLES

Try using some of the movement variables! These are variables 
V0 to V5! Try this-

Create a cube which is slightly raised off the ground and which 
you will be able to climb onto,something which should resemble
a magic carpet after you have shaped and flattened it.
Now make the carpet Moveable by clicking on the object Attributes 
button and selecting the MOV button (kit2) or changing the STATIC 
button (kit1) to Moveable.Now give the carpet this condition-

IF COLLIDED?
STARTANIM(1)
ENDIF

Next CREATE an Animation then EDIT it to include the following-

INCLUDE(2)         the 2 is the number of our carpet object  
START
LOOP(50)
MOVE(0,0,20)
ADDVAR(20,V2)
AGAIN

Then reset the area and then climb onto the carpet!
You should end up moving across the area on your magic carpet.
Trying using another variable between 0 and 5 instead of V2 
to see different effects! 

SENSORS AND VARIABLES

Create a sensor in kit 1 or make an object in kit 2 into a 
sensor.Make sure that the sensor is set to shoot.
Now create a local/area condition and add the following to it-

IF VAR=?(15,V11)
THEN ENDGAME
ENDIF

Now RESET the area and move towards the sensor.When you have been 
shot 15 times the area resets/the game ends.This is because 
variable V11 detects how many times you have been shot and because 
we have set it to 15,it will reset the game when you have been 
shot 15 times.

INSTRUMENTS AND VARIABLES

You can use an instrument(s) to show a variable working.This 
variable could be used to show your life diminishing via an 
instrument.Here is how-

Create two instruments (one for health and one for lives) 
and edit instrument 1 to have the following-

INSTRUMENT NUMBER 1
NAME = HEALTH
VARIABLE 70
VALUE 1 = 20
VALUE 2 = 0
COL1 = 5
COL2 = 0
TYPE = VERTICAL
 
Edit instrument 2 to have the following-

INSTRUMENT NUMBER 2
NAME = LIFE
VARIABLE = 71
VALUE1 = 3
VALUE2 = 0
COL1 = 5
COL2 = 0
TYPE = VERTICAL

Now create an Initial condition and edit it to include-

SETVAR (20,V70)
SETVAR (3,V71)
UPDATEI (1)
UPDATEI (2)

Now create and edit a General condition and give it the 
following code-

IF VAREQ? (0,V70)
THEN
SUBVAR(1,V710
SETVAR(20,V70)
ENDIF
IF VAREQ? (0,V71)
THEN ENDGAME

You will then end up with 2 instruments controlled with 
variables.One will take away a portion of your health ( when 
activated ) until it reaches zero,then it will call up the other 
variable to take away one of your lives when the first variable 
reaches zero.When all your lives are gone,the game resets.

You need to trigger the health instrument to work by adding some 
code to e.g. a floor object that when stood on looses you some 
life (it could be acid etc).The floor object has this condition-

IF COLLIDED?
SUBVAR (2,V70)
ENDIF

You could spice it up a little by adding some pepper,OOP's! I 
mean adding some text to a new text instrument ( Instrument 
number three ) and also add a sound of some kind.Like so-

IF COLLIDED?
PRINT(" YOU TROD IN ACID!",3)
SOUND(2)
SUBVAR(2,V70)
ENDIF
 
Now reset the area and add some movement controls to a border and 
then test it out.You must be in TEST mode (press F1) to see the 
instruments working.You must also have made some vertical 
instruments set in your border.

REMEMBER!
A VARIABLE is something that can hold any quantity that may 
change.
A CONSTANT is something that represents any absolute value which 
never changes.

     By now you should have an idea of how to use various 
variables.Experimenting is the key! Remember that you cannot harm 
your computer by experimenting with variables.At the worst you 
will end up with a loop or a system crash,but you can always 
reload the 3D kit again.Remember to save regularly and have 
fun!!Help is only a phone call away - 01484 460888 and they will 
still serves chips at my chippy even if you make a mistake?? HA!

Try experimenting with the examples above and try out some of the 
following-

ADDVAR  SETVAR  ORVAR  NEGVAR  NOTVAR  XORVAR  SUBVAR 
CLEARVAR  VAREQ?  VARLT? VARGT? and many others!   

While we are on the subject of variables,it is worth noting the 
following.-

There seems to be quite a few errors in the kit2 manual as 
regards to certain variable commands.Hopefully i will have given 
you the correct form for each of those listed here.

Using the LIMIT command.
It is listed as a Graphics command but i am sure that it should 
be classed as a Variable Modifier.The reason being that it is used 
to modify the limit amount of one variable and then stores the 
result into another variable.
Example-
  
SETVAR(1O,V31)        This sets the value of V31 to 1O
LIMIT(V3O,V31,O,8)    but because the limit of variable V31 is set
                      between O and 8,the value stored in 
                      Variable V3O will be 8.
 
The first variable on the Limit command line after the first bracket 
is where the result of the second variable ( which has been 
modified due to the Setvar command on the previous line )after the 
bracket is stored.Zero and 8 are the minimum limit range and 
maximum limit ranges for variable V31.The result is that V3O 
equals/or holds the value of 8.

Using the MAX command.
It is correctly listed as a Variable Modifier but it gives the wrong 
result in the example contained in the kit 2 manual.
Example-

SETVAR(24,V3O)     Variable V3O is set to hold the value 24.
MAX(V31,V3O,12)    The maximum value of variable V3O is set to
                   hold the value of 12 (as V3O is the source 
                   variable) and the result is stored in the 
                   destination variable V31.

So the end result is that Variable V31 will be set to 12 and not 
variable V3O as mentioned in the manual.This is because Variable 
V31 holds the end result as it is the destination variable and 
V3O is only the source variable. 

Using the MIN command.
It is correctly listed as a Variable Modifier but it gives a 
misleading result in the example contained in the kit 2 manual.
Example-

SETVAR(-24,V3O)    Variable V3O is set to hold the value -24.
MIN(V31,V3O,12)    The minimum value of variable V3O is set to
                   hold the value of 12 (as V3O is the source 
                   variable) and the result is stored in the 
                   destination variable V31.

So the end result is that Variable V31 will be set to 12 and not 
variable V3O as mentioned in the manual.This is because Variable 
V31 holds the end result as it is the destination variable and 
V3O is only the source variable.This can be quite misleading as 
variable V3O may equal 12 as mentioned in the manual after the 
computer has carried out the commands but the result is contained 
in V31.

So to summarise-
LIMIT is used to stop a variable/constant from holding a value 
less than the specified limit (the third variable/constant after the 
first bracket ) or greater than the limit (the fourth variable/constant 
after the first bracket )specified.

MAX is used similar to LIMIT but only effects the highest value 
thus preventing the variable from holding a value 
greater than the one specified.
 
MIN is used similar to LIMIT but only effects the lowest value 
thus preventing the variable from holding a value 
less than the one specified.

In the example- LIMIT(V3O,V31,O,8)
V3O is the destination variable where the computer stores the 
    result
V31 is the source variable where the computer gains the 
    information from in the first place
O   is the lowest/miniumum limit of the variable allowed 
8   is the highest/maximum limit of the variable allowed
So this effectively tells the computer that the variable V31 
(because its our source variable) can only use a value between 
O (the minimum number allowed) and 8 (the maximum number allowed) 
and then store the result in variable V3O ( because its our 
destination variable).

Tony Hartley - A man who varies constantly!    

