2020-05-15 11:29:04

Hello everyone! Welcome to the second part of my new series on pure basic. In this part we're going to be learning how to use numbers, also known as integers, but more importantly, integer variables. The word variable means that something can be altered. In this case a  number can be added to, subtracted from, divided by, or multiplied (+, -, /, *)
All this can be done with strings, too, but more on that in a later part.
A variable in pure basic is declared simply by writing it's name. In most cases, you will want to declare your variable as global. This can be achieved with:
global MyVariable = 3
That line of code would declare a global variable with the name MyVariable and the value 3. Global means that it can be accessed and altered from anywhere within the code, except for if it is declared inside a module. But that isn't important for now.
If you do not give your variable a scope, it will simply be local. Like so:
MyLocalVariable = 2
If we were to try and access this variable inside a procedure, it wouldn't work, mainly because the code doesn't act like a tree. Just because a procedure is inside the code in which the local variable is declared does not mean that it can access it, because the code more or less acts like a list until you get higher than the first layer of indentation. And no, pure basic does not need indentation. It will format the code automatically for you and add it, but you do not need to write indents yourself. In fact if you weren't satisfied with this, you could go into the preferences screen and simply turn it to no indentation with object navigation. There are also constants, which you can never change. Avariable is identified as a constant when it's name is preceeded by a number upon declaration and references to it.
Let's give an example:
#MyConstant = 10
One of the most important things you can do with variables is checking to see whether they matchs pecified criteria either with another thing of their type, either a static number like 5 or another variable like MyOtherVariable. And all of this is done using the most popular keyword in pure basic: the "if" statement.
This keyword is the basis of almost all programming. Without, the player would be able to use that secret weapon you hadn't meant for the to use, fire at an infinitely fast speed, and, without anything checking if a ke had been pressed down, your player would always be walking in circles, firing his weapon, checking his coordinates and pressing escape to go back to the main menu, quickly destroying the computer in not so very epic style.
But the if statement is here to save the day!
To use it you simply write:
if playershots > 0
This would check to see whether or not the player has more than 0 shots left.
If health < 1
to see if the player is dead
if location = 4
to see if the player is in the weapon shop
or even
if player_location = shop_location
or maybe even
if player_location = turret_location and shots > 0
The "and" strings together lots of comparisons, but the first one must always have an if beforehand.
Usually not doing this would be fine, because
thing > 2
doesn't declare that thing is now anything
but
llocation = shop_location
is a completely different story.
You can also use <=,
if shots <=10
or >=,
if location>=20
or even <> to see if something isn't somethign else,
if location <> shop_location
And that will be all!
In the next part, I'll be showing you how to use these three things together in our first game, mini battle.
Also coming soon, randomness, and repeating!
Stay tuned, and I hope you enjoyed this part! If you did, make sure to leave a thumbs up for me and pure basic!
Fun fact: it's main developer, Fred, has been working on two programming languages, maintaining a forum, and has a job. And he's kept it up for 22 years. You gotta give him credit for that!