PDA

View Full Version : Using Records in your Scripts



Abu
04-17-2012, 06:58 AM
What are Records?
I like to say that records are a way of storing lots of information within a defined variable. For example, I can store every type of weapon in the game in a variable called 'w.WeaponDTM'. As far as I'm concerned, with records you can store just about any type of data(Integers, TBox etc...). Why use it? Well it just saves you having to write multiple procedures for different for different types of the same item and it removes the necessity of massive if..then structures.


Using Records

Declaring Variables
The first thing you need to do, is declare your entire record as a type. Now you're probably wondering what on earth a type is, well, it is used just like var and const. What goes underneath it? Well first of all you need to write what your record is, so let's do that. I'm going to use a record I used in one of my cooking scripts:
type
TFoodInfo = record // because I want to load the information of my Food
^^ That's pretty simple right?
Now, what you want to put after that is the actual type of TFoodInfo you want to use. For example, do you want to use the colour of the food? The coordinates of the Food? What is that you want to use exactly? In my script I wanted to load my food DTM's and my food colours. So on the next line, you would put the type of information you want to store and obviously what type of data it is. You could name it anything you want, but for easier readability, it is good to name them appropriately, like so:
type TFoodInfo = record // On the same line for readability :)
foodDTM, foodColour: Integer; // they are both integers
end;

Great, so far you have written the type of record you want to use and the declared what you want to load from it. Now what we want to do is declare a variable which represents our record. Don't understand? Just read the example:
var
food: TFoodInfo; ; // 'food' is representing our record

Ok stay with me here. Just to recap, I have created a record of TFoodInfo and under that record we have decided to load the DTM's and colors of my food(s). I am using the variable 'foodDTM' to represent my DTM's and 'foodColour' to represent my colours. Underneath that, I have declared the variable 'food' to represent my record. Did you get that all in one go? Good. If not, be a darling and read over it will ya? ;)

Loading the information
So we're on to the next part, actually loading the information we want to store in the record, in this case - DTM's and colours.
Let's go ahead and start the procedure under the variables we just declared:
type TFoodInfo = record
foodDTM, foodColour: Integer;
end;

var
food: TFoodInfo;

procedure loadFoodInfo(); // See how it fits it's purpose?
begin

The next line is usually always a case ... of, a case of what you ask? Well a case of whatever the person who is using your script has decided to use! Don't get it? When you declare your players, you can create and extra line called Strings[0] or Integers[0]. Then you can make the player enter which type of what they want to use. Still don't get it? Oh my:
with players[0] do
begin
loginName := '';
displayName := '';
password := '';
isActive := ;
isMemeber := ;
bankPin := '';
strings[0] := 'Shrimp'; // the Food you want to use
end;
Ok now do you get it? Finally! Now we can use this Strings[0] in our case ...of. Like this:
type TFoodInfo = record
foodDTM, foodColour: Integer;
end;

var
food: TFoodInfo;

procedure loadFoodInfo(); // See how it fits it's purpose?
begin
case lowercase(players[currentPlayer].strings[0]) of // see, I'm just using strings[0]


Hopefully you know how to use cases. What goes next is your options. In this example I'm going to create a case which supports five fish, because frankly I don't need any more than five examples to show you how it works :p. So it should now look like this:
type TFoodInfo = record
foodDTM, foodColour: Integer;
end;

var
food: TFoodInfo;

procedure loadFoodInfo();
begin
case lowercase(players[currentPlayer].strings[0]) of
'shrimp':

'crayfish':

'sardine':

'anchovies':

'trout':
end;
end;


Now here comes the important part. What goes after your possible option is the variable representing your record followed by a full stop '.' followed by the type of value you want to use(foodDTM, foodColour). So it would look like this:

'shrimp': begin // This is needed because you are doing more than one thing
food.foodColour := ;
food.foodDTM := ;
end;

'crayfish': begin
food.foodColour := ;
food.foodDTM := ;
end;

'sardine': begin
food.foodColour := ;
food.foodDTM := ;
end;

'anchovies': begin
food.foodColour := ;
food.foodDTM := ;
end;


'trout': begin
food.foodColour := ;
food.foodDTM := ;
end;

Finally we can get on to loading the actual DTM and colours. After writing in 'food.foodDTM' and 'food.foodColours' we can now equal it to our actual DTMs and colours. The finished procedure should look like this:
type TFoodInfo = record
foodDTM, foodColour: Integer;
end;

var
food: TFoodInfo;

procedure loadFoodInfo();
begin
case lowercase(players[currentPlayer].strings[0]) of
'shrimp': begin
food.foodColour := 9147041;
food.foodDTM := DTMFromString('Make your own DTM!');
end;

'crayfish': begin
food.foodColour := 2046307;
food.foodDTM := DTMFromString('Make your own DTM!');
end;

'sardine': begin
food.foodColour := 5741142;
food.foodDTM := DTMFromString('Make your own DTM!');
end;

'anchovies': begin
food.foodColour := 6512478;
food.foodDTM := DTMFromString('Make your own DTM!');
end;

'trout': begin
food.foodColour := 10132129;
food.foodDTM := DTMFromString('Make your own DTM!');
end;
end;
end;

Wohooo! We have made our record! Obviously I could have used way more fish than just these five, in fact I have in my script, but just for example purposes, I have only used a small amount :)


Actually using the flipping record
So now you've created you're record, you wanna bloody use it no? Well it's simple, instead of wrting out shrimpDTM, troutDTM, pikeDTM etc... you can just replace it with food.foodDTM and whichever food the person using your script has decided to use, will be used! So like this:
function clickFood(): Boolean;
var
x, y: Integer;
begin
if findDTM(food.foodDTM, x, y, mainScreen.getBounds) then // See? Just food.foodDTM
begin
mouse(point(x, y));
if isMouseOverText(['aw'], 2000) then
begin
fastClick(MOUSE_LEFT);
wait(1200 + Random(200));
result := True;
exit;
end else
result := False;
end;
end;


The final thing you have to do in order to actually use your records is load it. Where do you load it? In your mainloop of course(not scripting mainloop). So here:

begin
setupSRL();
activateClient();
declarePlayers();

loadFoodInfo(); // before starting the actual script :)

PlayScript();

end.

And there you go! That's how to create, load and use records :)

Note

It is Pascal convention to add a "T" before the name of a user-defined type, so in this example it's TFoodInfo instead of just FoodInfo. This is not necessary but desirable.
Standards and functions used in this tutorial are in accordance with srl 6. Records can still however be used with OSR following the same principles.

Frement
04-17-2012, 07:11 AM
As to "Why use it?" I would've mentioned it removes the necessity of massive if..then structures. Anyway, nice tutorial.

'Toxin
04-17-2012, 07:37 AM
Very very nice and detailed tutorial my friend, rep received most certainly. Thanks for the addition, even I learned something here today.

Abu
04-17-2012, 02:57 PM
As to "Why use it?" I would've mentioned it removes the necessity of massive if..then structures. Anyway, nice tutorial.

Thanks and Added :)


Very very nice and detailed tutorial my friend, rep received most certainly. Thanks for the addition, even I learned something here today.

Thanks, I'm glad I can teach some of the more advanced users something new :D

Sir R. M8gic1an
04-17-2012, 03:43 PM
Duuuude...... you're totally mishowing the use of a record, throw some extra attributes in there, it's pretty pointless to have a record for just one attribute.

-RM

Abu
04-17-2012, 03:46 PM
Duuuude...... you're totally mishowing the use of a record, throw some extra attributes in there, it's pretty pointless to have a record for just one attribute.

-RM

Fine fine, I'll add some longer examples in while... *sigh* :p


EDIT: In the examples I loaded DTM's as well as colours and put five examples instead of two. Hopefully this better demonstrates the uses of records

:D

ShawnjohnSJ
04-18-2012, 11:14 PM
Nice tutorial, its very detailed and looks like any beginner can learn about records. Rep++

Abu
04-19-2012, 06:19 AM
Nice tutorial, its very detailed and looks like any beginner can learn about records. Rep++

Thanks. :D

Also, it's in the advanced section because you vary rarely see non-advanced scripters use records(like me for example), but it is very easy to learn and should become a good habit for everyone.

Maybe I should get this moved to intermediates?

Sir Ducksworthy
04-19-2012, 06:51 AM
Nice Abu ty

Echo_
04-19-2012, 05:14 PM
Btw it's Pascal convention to add a "T" before the name of a user-defined type

Abu
04-19-2012, 05:27 PM
Btw it's Pascal convention to add a "T" before the name of a user-defined type

So it would be TFoodInfo?

P1nky
04-19-2012, 05:51 PM
rep+, thanks.

Abu
04-19-2012, 05:54 PM
rep+, thanks.

Thank you! I know have two bars for rep :garfield:

EDIT: Your rep power =14? Holy mother of...

P1nky
04-19-2012, 05:59 PM
Thank you! I know have two bars for rep :garfield:

EDIT: Your rep power =14? Holy mother of...

:) Haha, really? Well you do deserve it, seriously a easy simple tutorial with good examples Mate. Keep it up

Abu
04-19-2012, 06:07 PM
:) Haha, really? Well you do deserve it, seriously a easy simple tutorial with good examples Mate. Keep it up

Thanks and check my graphics thread?

Echo_
04-22-2012, 01:24 PM
So it would be TFoodInfo?

Yep :)

Abu
04-22-2012, 02:40 PM
Yep :)

Ok Thanks added at the end of the tutorial :)

Striken
04-26-2012, 11:14 PM
So your able to make it select multiple resources but can you make it do a different method for the task with the same?

Abu
04-27-2012, 04:38 AM
So your able to make it select multiple resources but can you make it do a different method for the task with the same?

You didn't finish off your sentence there, for the task with the same....

But I think I know where you are getting at and yes. :)

NCDS
04-27-2012, 05:00 AM
Maybe I should get this moved to intermediates?
I would certainly hope this fits there; but yes, nice tutorial. :)

Abu
04-27-2012, 05:14 AM
Alright thanks, I've sent in a request :)

Striken
04-28-2012, 01:09 AM
Know how you have it as "food.FoodColor := Color;"

I get an error with mine but I changed it from "food" to "ore"
[Error] (33:23): Unknown identifier 'ore' at line 32
Compiling failed.
Line 32 is that line that I have said at the very top.

Abu
04-28-2012, 11:43 AM
Know how you have it as "food.FoodColor := Color;"

I get an error with mine but I changed it from "food" to "ore"
[Error] (33:23): Unknown identifier 'ore' at line 32
Compiling failed.
Line 32 is that line that I have said at the very top.

Read through the tutorial rather than just trying to copy and paste. You will find the solution yourself and this will help you in future when using records.

*hint* Read over the first section; Declaring Variables *hint*

Striken
04-28-2012, 05:13 PM
Now I get it. I guess I just read it too quick :p

BraK
04-29-2012, 04:15 AM
Moved on Request ;)

Master BAW
06-18-2012, 07:06 PM
Ty! amazing:) Using this now :D

Sirenia
06-21-2012, 10:34 PM
Nice tutorial

Based Lord
06-28-2012, 07:24 AM
Using this in my next script :) Glad you wrote this or I would be using a less efficient method to find food