Learning to use the Delphi Language
By DragonPhinn [Howard N Smith]
Personally, I dislike tutorials that start off with a whole history of the language. I'm sure that if that's what you wanted you could go somewhere else to find it. Here, however you are going to learn how to use Object Pascal, and apply it to your Game Developing needs. Before we get started, I'd like you to be familiar with The Basics of Delphi Programming. You should know how to get into the Unit code editor, and how Events work.
Before we start programming, it's always good to outline exactly what you wish to accomplish in making your program. Sometimes it may be as simple as displaying some text, to as advanced as a full fledged 3d engine. For now, we aren't going to get into programming a 3d engine. All we are going to learn in this tutorial is the basic syntax of object pascal and how to use the Borland Help menu's to learn and understand new functions and methods. To that end, we are going to start off changing the Caption on a button.
Now that we got that out of the way, lets get started. First off, load up your copy of Delphi and add a button to the main form. Double click the button to get into the Unit code area. Now, keeping in mind what our goal for this program is (changing the text of a button) lets write out first piece of code.
Button1.Caption := 'Changed the Caption';
Ok, it looks pretty simple right? Well it is. First off you say "Button 1", which tells the program that Button one is the component you want to use. We then use a Dot Operator, or period, to access the Button's property's and methods. My version of Delphi (Delphi 4) brings up a list of functions, methods, and properties of the referenced control. Your's may or may not do that (depending on the version). If you recall, each control has a list of properties outlined on the Object Inspector. One of the Button's properties happens to be "Caption" which defines what text is displayed on the objects face. So, we accessed this property by writing the control name and then the property we would like to change or read. Consquentially, we can access other of the buttons propertys as follows:
Button1.Visible Button1.Top Button1.Left Button1.Font
All of these are valid statments. However, if you notice in the other code snippet, there is a symbol following the reference. This is called the Assignment Operator, and is used to set variables. The assignment operator is read "set to". For example, the above statment ( Button1.Caption := 'Changed Caption'; ) is read: Button1's Caption set to 'Changed Caption'. In other words, Set Button1's Caption to 'Changed Caption'. You'll notice that the statement ends with a ";" (semicolon). The semicolon in Pascal acts much like a period in an english sentence. It tells the compiler that the statement is finished.
You can use the Assignment Operator to achieve various results. For example, if you add an TEdit box to your form, (assuming you didnt change the Name property of the Edit1, then you can say:
Button1.Caption := Edit1.Text;
Type something into the Edit1 text field and press the button: they should match! Amazing eh?
There is one last bit of information regarding pascal I want to impart before you begin creating amazing programs of you own. These are called variables. Variables are data stored by your program for future reference. They include things like Character hit points, names, locations, and just about everything else. You can add variables to your project by adding the var statement before the begin statement in any procedure. For example:
procedure TForm1.Button1Click(Sender: TObject); var MyInteger : integer; begin MyInteger := 1; end;
This will create a container for a number called MyInteger, and then set that number to 1.
OK! So now you are feeling pretty saucy about your Delphi Object Pascal knowledge. I suggest you use the Borland Help files to learn more verbs, functions, and events for your programs. Just remember - everyone started somewhere. So keep at it, and don't give up. Programming is a rewarding and entertaining passtime (or a profession).
DragonPhinn