I just updated the first post with some things we need, maybe something is missing so keep your eyes opened
Lesson 1: Basics Of ProgrammingOk before I start I recommend you to get the tools I will use while making this game.
- DarkBasic Pro (30 Day Trial)- Zanoza Modeler 1.07 (Freeware)- Photofiltre (Freeware)When you have problems getting darkbasic to run or your trial runs out just call me on msn or pm me.
I m sure most of you know allready how a code looks like and how it works. Though i m going to explain it again.
This is a sample code:
CommandA Parameter1,Parameter2
ReturnValue = CommandB(Parameter1)
CommandC Parameter1
Ok, its easy.
Our compiler (The applicatoin generating the exe files for us) will just read the code like a book, starting from top going to the bottom.
So first CommandA gets executed with the Parameters 1 & 2
After we will use the Value of Parameter 1 in a "function" to get a Return Value
and after that another Command is executed without return values (or we dont need them so we dont store them in a variable)
Sample of Commands:
Blah = ResultAddition 1,8
Blah = SquareRoot(Blah)
Print str$(Blah)
Ok, First the Result of 1 + 8 is saved into the integer variable Blah (Integer is default in DarkBasic),
Then the square root of the value from Blah (8 + 1 = 9 >> Square Root of 9 = 3) is saved.
After that our programm will show us a 3 on the screen.
Now you might ask: Whats the str$ standing for? Well, easy. Our variable is from the type Integer - its a number. But we want to output text
so its must be converted to a text string before (using str$())
These commands doesn't exist in DarkBasic btw.
But here is another sample of the same code with working commands:
Blah = 1 + 8
Blah = sqrt(Blah)
Print str$(Blah)
sqrt is the shortform for SquareRoot. This is probably hard to remember but we wont need it often anyways.
Ofcourse you can mix commands into eachother too. Here is an little example:
rem Blah = ResultAddition 1,8
Blah = SquareRoot( ResultAddition(1,8) )
Print str$(Blah)
Ok, this is probably a bit harder. The first line has an "rem" infront of it, "rem" stands for Remark. So this line will be ignored by the compiler
you can put comments there for example. The second line is a bit tricky.
First your application will get the returnvalue from the function/command ResultAddition 1,8 . The parameters are in brackets so the compiler knows that these
parameters are members of the function ResultAddition.
So our "ResultAddition(1,8)" returns 9. This will be instantly used in SquareRoot. The result of our Square Root is 3 once more and will be saved in the variable Blah.
Then the pc outputs the content of the variable Blah > 3
So basicly our programm will do the following
Blah = SquareRoot( ResultAddition(1,8) )
will become
Blah = SquareRoot( 9 )
which becomes
Blah = 3
and after that the result is given out. First we have had 3 Lines of code. But know you know: we could have done in 1 Line!
Here it comes:
print str$(SquareRoot( ResultAddition(1,8) ))
Ok, these are the basics. Now about String manipulation and conditions
First we will have a variable, i will call it OurText for now:
The $ at the end will mean string value!
OurText$ = "GTAStunting.com Sucks"
the quotion marks will identify a string value.
ok now we let our programm check the code:
if OurText$ = "GTAStunting.com Sucks" then OurText$ = OurText$ + " not!"
print OurText$
Pretty easy eh? First it checks if OurText stores the text: "GTAStunting.com Sucks" (CASE SENSITIVE!) and if so it adds a simple " not!" at the end
Our new text will be "GTAStunting.com Sucks not!".
But now imagine: You detected a collision between your player object and a wall for example. Now you want to rotate your player AND reposition your player.
But the "if [condition] then" only takes one command in the end. The solution is simple:
if [condition]
remstart
Tons of comments here
remend
[Tons of commands here]
else
rem This happens when the condition is not true!
[Tons of commands here]
endif
So first it will check the condition and if it is true then it will execute the code until "endif". Please remember that there is no "then" !
("remstart" starts a remark block and "remend" ends it)
One last thing about conditions:
There are other operators than =
a < b means a is smaller than b
a > b means a is greater than b
a <= b means a is smaller or equals b
a => b means a is greater or equals b
a <> b means a does not equal b
I think now you know about 75% of all stuff needed for a game. Only thing missing are datatypes, arrays.
What you know right now about datatypes is basicly nothing.
You should know there is the integer type and the string type.
Here are all we need:
integer
float
string
there are others which we wont use due heavy bugs in DarkBasic when doing comparsions with the variables of that type.
integer could be a value like this: 4646 or 1337
float could be a value like this: 4646.46 or 1337.0
and
string could be a value like this: "4646.46" or "1337.0" or "Hello World!"
the problem is: you cant compare them against each other
1337.0 <> "1337.0"
Though, i saw newer darkbasic compilers which fix this. Mine doesnt at all!
so we have a little problem here:
rem PlayerPosition on Height Axis (Y)
PlayerPositionY as float
PlayerPositionY = 0.45
rem Check if player is above water level which is at 0.0
if PlayerPositionY > 0 then print "Heya! Player is above water level"
this should give us NO text output (Integer Versus Float)
IT will try to convert our float to an integer which is 0 => And 0 > 0 is false!
So when programming always look out for this kind of comparsions.
There is no real solution for this. What i do is either multiplying the float value by 10000* or something to get an integer (results in overflow/underflow sometimes) or i define an zero variable as float on top
btw: Instead of declaring it as float you can just add an # at the end of the variable (but it wont work either )
tricky eh?
Ok now for arrays
When having a game you have no ways arround arrays i think. Here is an example:
ObjectA = 1
ObjectB = 2
ObjectC = 4
ObjectD = 8
print ObjectA
print ObjectB
print ObjectC
print ObjectD
now almost the same code using arrays:
dim Object(3)
Object(0) = 1
Object(1) = 2
Object(2) = 4
Object(3) = 8
for i = 0 to array count(Object(0))
print Object(i)
next i
Explaination: first we will fill Slot 0,1,2 and 3 of the arry Object (Which as 4 Slots: 0 to 3. In array count use Object Slot 0 always) with values.
After that we setup an for to loop.
a for to loop is simple. first you set a start value (0 in this case) and a variable which should be changed and finally the value you want to reach.
at the end of the loop you place an next [variablename]
so this code will execute:
rem [Setting variables]
rem First Run (i = 0)
print Object(0)
rem Second Run (i = 1)
print Object(1)
rem Third Run (i = 2)
print Object(2)
rem Last Run with 4 (i = 3)
print Object(3)
Thats it!
The last thing you have to know about are the custom datatypes.
Sample:
rem Create Position Variables for 6 soldiers - X,Y,Z for each
dim SoldierPositionX(5) as float
dim SoldierPositionY(5) as float
dim SoldierPositionZ(5) as float
rem Setup first Soldier (Slot 0)
SoldierPositionX(0) = 0.0
SoldierPositionY(0) = 5.0
SoldierPositionZ(0) = 10.0
rem [...]
Ok now imagine you have other things like Health, Rotation (XYZ), Spin (XYZ), Velocity (XYZ), Position (XYZ), Animation ID, ...
for this we have some handy thing called Usertypes:
type matrix
x as float
y as float
z as float
endtype
type PlayerClass
position as matrix
rotation as matrix
health as float
endtype
dim Soldier(5) as PlayerClass
Soldier(0).health=100.0
Soldier(0).position.x = 0.0
Soldier(0).position.y = 5.0
Soldier(0).position.z = 10.0
Soldier(0).rotation.x = 0.0
Soldier(0).rotation.y = 0.0
Soldier(0).rotation.z = 0.0
Thats it! simply define your own datatype with given variables in it.
Owner.Member = Value
Thats all for now. When you have any question just call me on msn or pm me
(Or even bettere ask in the thread)
PS: One last thing! I can say for sure that none of these sample is working. The reason for this is simple: There are no more commands so the execution of our programm stops. To avoid this just add a "Sleep 5000" at the very end of your code to make your programm stop for 5 seconds (5000 milliseconds).
For Compiling your code use either F5 or "Compile > Make EXE/RUN" from the menu
[SPACE FOR LESSON 2: Sin and Cos]