Great stuff guy's, keep it up.
Tip/Info – Variables
Variables are parts of memory set aside for data that can be changed at any time. They are referenced with a name (identifier). For example x = 1, x would be the identifier. An identifier can’t be a reserved word (DBP command) like do, loop, if, end etc...
I think anyone getting into programming has a good grasp of mathematics so I don’t need to describe Numerical Values to you, just note, DBP can’t calculate powers (numbers multiplied by themselves) in the way other programming language for example 3 ** 4. That calculation equates to 3*3*3*3. The DBP way would be to use ‘^’ squared, so 3^4.
Info - Variable Assignment
Variable assignment is a fancy way of saying ‘setting up a variable’, for example x = 1. The =, or assignment operator basically is saying “the variable on the left points to the value on the right hand side”. So the example above assigns variable ‘x’ with a value of 1.
When we do this, a = x or a = x + x, we reference the value assigned to the variable, so if x = 4, a = x becomes a = 4 and a = x + x become a = 4 + 4.
Info - Self Assignment
In this case DBP can’t do self assignment in which a variable modifies itself based on a given value, so:
x += 7
x -= 3
x *= 2
`etc...
The first line in that snippet would look like this x =x+7, this would be the DBP equivalent.
Tip – Variable Naming
Ok guys, I seen some ridicules names for variables in my time like this: ObjMesh_RedWall01a_LevelSectionD2_Floor55_NearStairs02a_01... yeah you know who you are. This is madness, if you want to keep track of where your objects are chuck that into comment rather than the variable.
Tip – Undocumented Command – Mod
Mod, which is short for ‘Modulus’ is the name belonging to this sign, %. This is the DBP way of finding remainders, so x = 14 mod 4 or 14/4 would return the remainder which is 2, you can also do use the mod sign twice for mod like 14 %% 4.
Have a play with it, here’s one to start with:
X = 200 Mod 100
X = 200 %% 100
Tip – Global Variables
A Local variable in that when execution goes outside the method, example main loop, function etc... the value is lost. A Global variable retains its value and can be referred to from anywhere in the program. So for example:
Non-Global:
A = 2
Do
PrintA()
Loop
Function PrintA()
Print A
Endfunction
Since variable A is not local to the function nor global to the program, PrintA() would always print 0.
Global :
Global A = 2
Do
PrintA()
Loop
Function PrintA()
Print A
Endfunction
Now our variable A is global, it can be used anywhere in our program and PrintA() will print 2.
Tip – Global Identifier (Recommend)
Because there is no Identifier for a global the common method is writing it like this: gVariable or G_Variable, the reason we do this is so we can identify which variable is a local or global, because if we didn’t it can make understanding the program very difficult, not just to you but to anyone else look at it.
Tip - Conditional Execution
The operation that determines whether or not a specified set of conditions have been satisfied is called conditional execution.
Comparison Operators
These operators compare the given values, either numerical or string, and return the result.
These are DBP’s:
< - Less than
If 2 < 3
`do something
Endif
> - Greater than
If 2 > 1
`do something
Endif
<= - Less than or equal to
If 2 <= 2
`do something
Endif
>= - Greater than or equal to
If 2 >= 2
`do something
Endif
Oddly two were left out, not sure why though:
== - Equal to
!= - Not equal to
For the last two, you can use the = to see if something is equal to for example:
X = (10/5 = 1+1).
X would return 1 if it is equal, 0 if not, but personally it’s doesn’t look right, I think they should adopt the == into the DBP banks.
For != there’s a simple way of checking if something is not equal:
X = -(10/5 = 1+2)+1 .
Ok I might need to explain that one in more detail.
Let’s work out the brackets first,so: 10/5 = 2 and 1+2 = 3.
So there not equal and it would look like this X=(2 = 3), that would equal 0 or false. But we want to see if something is not equal, so we turn the answer into a minus -(2=3). Now that would equal -0, then we add a 1 to finish the formula -0 + 1 = 1 means it is true that it is not equal. If the brackets had produced a -1, again 1 is added which would make 0, which means it is false that it is not equal. Simple.
Tip - Logical Operators
Same as before just that these deal with logic:
and - &&
X = (4 > 3 and 1 + 1 = 2)
X = (4 > 3 && 1 + 1 = 2)
or – II
X = (1 + 1 = 2 or 2 + 2 = 4)
X = (1 + 1 = 2 II 2 + 2 = 4)
not –!
For not DBP doesn’t use ! and ‘not’ doesn’t work as a Logical operator in the way other languages, so:
True = 1
X = (not True) (would equal False or 0)
DBP can’t do this.
Things of Interest
My few odd finds while I was paying about, did you know that if you initialize an array with an – you get and array size of 12099488. Also if you use and string you get an array size of 19296400, so:
Dim Array(-) = 12099488
Dim Array("") = 19296400
Something odd:
x = -- = -2089877568 (the more – the value changes)
x = . = -2147483648
x = -.-..- = -2147483648
x = -..-... = 3
And there's a few more.
Hmm... I have no clue whats going on here, but .. and ... is used for range in other languages but I’m sure it doesn’t work for range in DBP.
A dream is a fantasy, if you achieve that fantasy it was never a dream to begin with.