PDA

View Full Version : Records, Types, and Psuedo-OOP



Tniffoc
03-21-2009, 05:48 PM
Records and Psuedo-OOP

OMG... what do you mean by Psuedo-OOP? :eek:
When I say Psuedo-OOP I mean "fake object oriented programming". Simba doesn't have a totally object oriented way of things to speak of. We do have records however, which is what this tutorial is all about.

What exactly is a record?
I like to think of a record as a collection of data. It, for example, can be a collection of an integer, a string, and another integer. When you are scripting, you've probably run into a thing called a TPoint. You've used these with TPAs or TPointArrays. If you don't know these, they are just a type of data that represents a point on the screen, or a coordinate plane. A TPoint is an example of a record. it holds two integers, x and y. Somewhere, there is code that defines what exactly a TPoint is. It would be coded somewhat like the following. We will break this down line by line in a second.

TPoint = record
x: Integer;
y: Integer;
end;

Now, let's look at the first line :

TPoint = record

"TPoint" shows the name of the new record we are creating. By saying, " = record" we are saying that it is a record (there are a few other things not explained in the tutorial that could take the place of the word "record").

Now let's look at the variable declaring lines :

x: Integer;
y: Integer;

These lines tell us what variables are going to be contained in our record and what their names will be. To give an example of how these could be used, consider the following code where point is a variable of type TPoint. This code would be found in the program long after the definition of the record.

point.x := 5;
point.y := 10;

This code would set the value of variable "x" in the record "point" to 5 and the value of the varible "y" in the record "point" to 10. You can later call these values as in the following code.

Writeln(IntToStr(point.x));
Writeln(IntToStr(point.y));

Now that you understand the basics of what a record is and how one works, let's look at how to make our own records!

In Simba, you know that you can declare variables in the following way.

var
i: Integer;

You also know that you can declare constants in a similar way

const
i = 5;

You can declare types or records in a similar way! Just as with the const and var words. You can put the word type after the "program somename;" line to create records! Here is an example of how you might create the record TStringAndInteger in a program.

program New;
type
TStringAndInteger = record
str: String;
i: Integer;
end;

var
foo: TStringAndInteger;
begin
foo.str := 'Some String Here';
foo.i := 5;
end.

Beyond this point in the tutorial is just an example of how to use records while scripting.
In case some of you curious learners were wondering, records can also hold arrays! I use this when I write reflection walking so that I can associate each path with a name. Here is the record definition that I would use.

type
TRefPath = record
path: TPointArray;
name: String;
end;

Later on in my program, I would set a few TRefPath variables up!

var
willows,oaks: TRefPath;

procedure DefinePaths;
begin
willows.path = [Point(1,1)] // This line would be the TPoint array of all the tiles for the path.
willows.name = 'Willows Path'; // This is the name of the path.

oaks.path = [Point(1,2)] // This line would be the TPointArray for all the tiles in the path
oaks.name = 'Oaks Path'; // This is the name of the path.
end;

Lastly, I would create a procedure to say which path the script was walking by using the name variable and walk the path using the path variable. However, I will not show you this code because I think you should write it yourself.

A Last Blurb on Object Oriented Programming (OOP)
In my opinion, OOP can make your code much easier to understand and quite honestly, easier to write. A record in Simba is an example of an object. If you get deeper and deeper in to object oriented programming, you will see that things called classes can be used to define procedure and functions from within an object. so, you could call something like this:

someObject.WalkThePath(someTPointArray);

If you are interested in Object Oriented programming, specifically in the language of Java, I would check out Chapter 5 of this book : Javanotes (http://math.hws.edu/javanotes).

In object oriented programming, you can create a thing called a pointer a pointer stored the location of a function or a procedure so that you can call the function or procedure under a different name. Take this record as an example.

program New;
type
TNamedMethod = record
p: procedure(s: String);
name: String;
end;
var
rec: TNamedMethod;

procedure say(s: String);
begin
Writeln(s);
end;
begin
rec.p := @say;
rec.name := 'Say';

Writeln('Running: ' + rec.name);
rec.p('HAI');
end.

This shows you that you can run a procedure through a pointer. Now that you've seen that a record can contain a pointer, I'll tell you that you can have a pointer outside of a record as well. See this final example.

program New;
var
p: procedure(s: String);

procedure say(s: String);
begin
Writeln(s);
end;
begin
p := @say;
p('HAI');
end.


Happy Programming! If you have any questions, post them here and I will add them to the tutorial!


This tutorial may not be reproduced in any way, shape, or form without prior written permission from the owner, Tniffoc.

Illkillutill
03-23-2009, 03:58 PM
Nice, I never really understood records, it cleared up quite a bit for me.

Thanks. Rep++

Tniffoc
03-23-2009, 06:18 PM
Nice, I never really understood records, it cleared up quite a bit for me.

Thanks. Rep++

Thanks for posting! I'm glad the tutorial is doing it's job!

Nadeem
03-23-2009, 10:20 PM
w00t nice! :D rep+! Im going to be using this strategy in my scripting from now on xP

Tniffoc
03-24-2009, 05:15 PM
Woot! Glad I could help y'all.

Nose Smasher
03-27-2009, 01:30 AM
"fake object oriented programming"
Hehe... :p

Very nice, I already know bout records and such, but still a good tut ;)

Inc(rep);

Tniffoc
04-01-2009, 04:46 AM
Woot. I'm glad this is working out so well.

WhoCares357
04-14-2009, 09:17 PM
First time I've seen the procedure pointers in SCAR. I wish you had gone a little more in depth with them.

For example, do the arguments have to be the same identifier (like "s" in your example) or do they just need to be the same type? And what's the advantage of using that? It seems simpler and quicker to just use "say('blah');" instead of declaring a pointer, directing it to the procedure, and finally using it through that.

Otherwise a fine tutorial. I actually learned something new :).

Tniffoc
04-14-2009, 11:41 PM
First time I've seen the procedure pointers in SCAR. I wish you had gone a little more in depth with them.

For example, do the arguments have to be the same identifier (like "s" in your example) or do they just need to be the same type? And what's the advantage of using that? It seems simpler and quicker to just use "say('blah');" instead of declaring a pointer, directing it to the procedure, and finally using it through that.

Otherwise a fine tutorial. I actually learned something new :).

Sounds like more on procedure pointers is in demand. I'll write up a section and send you a pm when it is finished.


Brilliant Tutorial, love it. Already read over a few OOP tutorials in my time studying programming - more specifically for C++ and a brief one on Java, but I love the brief blurb on it at the end of the tutorial. The Record section is just as great, simply explained and easy to grasp. Certainly gathered my interest and hope it will do the same for others. :)

Just as a brief few questions - although I'll do some looking myself perhaps you know - is OOP used widely in SCAR at the current time? Or is it just a minor implementation that a few people use? Also, would you advise learning more OOP techniques in the aim of developing Scripts using them?

Cheers. ;)

OOP is used in only the more complex scripts. I use it because...

It looks cool.
It's easier to write for multiple places.


I would advise learning it, yes.