SRL Archive - General Botting Help

How to get GE prices?


Spaceblow

2014-08-18 07:59:16

Yes I know, in the recent past I've already made a few threads in this section asking some pretty obvious question but I only do that if I've already tried some time to figure it out and if I still don't know what to do. So I want to get the actual GE prices, I tried to figure out by looking at…

The Mayor's snippet:

const
EXCHANGE_PRICE = 1;
LOW_ALCH = 2;
HIGH_ALCH = 3;

function getItemPrice(itemString: string; priceType: integer): integer; //#1
var
wikiaPage, priceString: string;
begin
wikiaPage := getPage('http://runescape.wikia.com/wiki/Exchange:' + itemString);
case priceType of
1: priceString := between('GEPrice">', '</span>', wikiaPage); //#2
2: priceString := between('<b>Low Alchemy:</b> ', '</li>', wikiaPage); //#3
3: priceString := between('<b>High Alchemy:</b> ', '</li>', wikiaPage);
end;
result := strToIntDef(ExtractFromStr(priceString, numbers), -1); //#4
end;

begin
clearDebug;
writeLn('Fire orb GE price: ' + toStr(getItemPrice('Fire_orb', EXCHANGE_PRICE)));
writeLn('Fire orb Low Alch price: ' +toStr(getItemPrice('Fire_orb', LOW_ALCH)));
writeLn('Fire orb High Alch price: ' +toStr(getItemPrice('Fire_orb', HIGH_ALCH)));

writeLn('Purple Phat GE price: ' + toStr(getItemPrice('Purple_partyhat', EXCHANGE_PRICE)));
end.
Fire orb GE price: 3194
Fire orb Low Alch price: 120
Fire orb High Alch price: 180
Purple Phat GE price: 1175384160
Successfully executed.

But I really don't know what he's talking about. I would rather want to understand everything from his snippet than just copy it into my script. I placed numbers in his snippet for asking my questions:

#1: I don't understand why he's doing
(itemString: string; priceType: integer): integer;
I always wondered why people put those stuff between paretheses when I looked at their scripts. Is it because it's a function that you have to do it? I mean, why don't they just put it under var and declare it there? And the
: integer;
is used for
getItemPrice
and not the stuff between the paretheses, right?


#2: There's a lot I don't understand here. I get that with
'GEPrice">'
he's looking for text. But why is there a " and a > ? If for example I want to get the fire orb GE price and I go to the fire orb wikia. And I search (CTRL + F) for 'GEPrice' I can't find it, so why does he use that then? I guess he's aiming for this:


But nothing matches with his lines? And then he has also this:
'</span>'
What is it for? I looked it up and <span>…</span> looks for a piece of text on the webpage, but why does he only use the second part of it? I mean, what is he closing with the trailing slash if he didn't even use the first part of it?


#3: So I guess that with
'<b>Low Alchemy:</b> '
he's looking for the text 'Low Alchemy' that's written in bold. But then comes this:
'</li>'
Again, what is it used for? I really have no idea, and why does it only have the second part of it (the part with the trailing slash)?

And for stuff like:
between(..., ..., ...);
Is it somewhere in the SRL-6 docs or do we have to figure it out by ourselves?


#4: Again, there's a lot I don't understand, almost nothing to be honest. Why is it being assigned as 'result' if that's not even being used? And if I read this:
strToIntDef
I understand:
string To Integer ???
So I really don't know what he means with 'Def'. And after 'Def' he has:
(ExtractFromStr(priceString, numbers), -1);
I really don't understand anything of that, simply nothing… Could someone clarify this for me please?


To be honest, it took me a while to make this thread. I hope you guys could answer my questions so I could understand everything I type instead of just leeching someone's snippet. Thanks in advance!

The Mayor

2014-08-18 08:07:22

I will answer these questions in 8 hours when I wake up, unless anyone else does beforehand.

Press Play

2014-08-18 08:49:58

#1. Writing a function.
The "stuff between paretheses" are the variables for that particular function. You are right - we can declare and define variables outside the function and use them instead. So, why don't we? This is due to a thing called scope. In short, we only want to use these variables inside the function (, not globally!). The
: integer;
at the end of the function is the type that the variable returns. ie. this is the type of the variable 'result'.

#2. Getting a webpage.
To understand the function
between(..., ..., ...);
one must first look at the
getPage();
function. This function returns the source of the page as a string. ie. all the html stuff behind the scenes! To have a look at this "source", Right-click -> View page source (in Google Chrome).

#3. Between.
Say for example you have a really big string (the html webpage!) - lets call this 'wikiaPage'. The between function looks through 'wikiaPage' returns the (first) string between 'str1' and 'str2'.
between(str1, str2, wikiaPage);

#4. strToIntDef.
To be totally honest, I forget why this is used instead of strToInt. I think it returns the integer value of a string even when there are other characters in the string too? eg.
strToInt('999999') // --> 999999 
strToIntDef('999, 999') // --> 999999
E: See latter posts :)

Spaceblow

2014-08-18 10:32:11

From: Press Play
#1 . Writing a function.
#2 . Getting a webpage.
#3 . Between.
#4 . strToIntDef.
Thank you very much for your very well explained answers. I think I kind of understand answer #1 but I guess I'll only be able to fully understand it if I gain some more experience. Answer #2 and #3 I fully understand now and #4 is still vague for me, I still don't know why something is assigned to 'result' while 'result' isn't even used in the fuction, well I atleast think it isn't.
  result := strToIntDef(ExtractFromStr(priceString, numbers), -1);
And I still don't know what 'Def' stands for or why it's being used or what everything after 'Def' is. :p

Thank you very much Press Play, you made things way more clear for me, I really appreciate it!


From: The Mayor
I will answer these questions in 8 hours when I wake up, unless anyone else does beforehand.

Kind of funny to know that you're sleeping at the moment, I'm looking forward to your answer on my fourth question. :)

bonsai

2014-08-18 11:22:46

From: Spaceblow
I still don't know why something is assigned to 'result' while 'result' isn't even used in the fuction, well I atleast think it isn't.
  result := strToIntDef(ExtractFromStr(priceString, numbers), -1);
And I still don't know what 'Def' stands for or why it's being used or what everything after 'Def' is. :p


result is an automatic variable that holds the return value for a function. If a function returns an integer, you will see result being set to some integer within the function. That's what goes back to the code that called it.

The def stands for default. The -1 in the function call is the default value that gets returned if it can't get a good number from the string.

so the breakdown is…

ExtractFromStr(priceString, numbers)

Remove any non-digit (0..9) from the string priceString and return another string with just those digits.

strToIntDef(ExtractFromStr(priceString, numbers), -1)

Turn that string of digits into a real integer variable. If the string is not valid, make the integer -1

result := strToIntDef(ExtractFromStr(priceString, numbers), -1);

Take that integer and assign it as the result of this function.

The Mayor

2014-08-18 16:32:17

It looks like everything has been answered. You should read the section about the difference between procedures and functions in my tutorial. I'll just explain them here anyway:

The things inside the parentheses are the function (or procedure) parameters. These are normally variables that you input, which the function then takes and uses during it's calculations.

All functions also retun a result. The result is just a variable of any type. The variable type the function returns as it's result is indicated at the end of the function. The following function take no parameters, and it results in a boolean (i.e. it can result either true or false).

function amIOld(): boolean;

Now to finish the 'amIOld' function, lets get the person to input their age as a parameter, and the function will result true if they are over 30. The function now takes a paraneter called 'age' and the function knows that this variable has to be an integer.

function amIOld(age: integer): boolean;

Now we will use the 'age' parameter in our calculations. You can see the function results true if age is greater than or equal to 30, else it results false.


function amIOld(age: integer): boolean;
begin

if age >= 30 then
result := true
else
result := false;

end;

Now I can use that function in my other code. As this function results in a boolean, I can use it within a conditional statement (if..then.. etc.). For example, I can write:

  if amIOld(40) then
writeLn('I am old')
else
writeLn('You are young');

I wrote the function the long way so you would understand it better, but it could be shortened to:


function amIOld(age: integer): boolean;
begin
result := age >= 30;
end;

Another example is a function which takes 3 integers as parameters, adds them together and then return that number (an integer) as the result:


function addStuff(num1, num2, num3: integer): integer;
begin
result := num1 + num2 + num3;
end;

writeLn(addStuff(10, 10, 3));

Spaceblow

2014-08-19 06:33:26

From: bonsai
result := strToIntDef(ExtractFromStr(priceString, numbers), -1);
That's really nice explained. So without the def the '-1' couldn't be used, right? And in this case it's used so you could know if the webpage somehow changed and it couldn't find the price anymore, is that correct?

From: The Mayor
functions

Thank you once again The Mayor. I tried it out:
program new;
function labradors(dogs: integer): integer;
begin
result := dogs + 2
end;

procedure doggies();
begin
writeLn(intToStr(labradors(2)));
end;

begin
doggies();
end.


Compiled successfully in 250 ms.
4
Successfully executed.

And
program new;
function labradors(dogs: integer): boolean;
begin
result := dogs > 2
end;

procedure doggies();
begin
writeLn(boolToStr(labradors(3)));
end;

begin
doggies();
end.

Compiled successfully in 249 ms.
True
Successfully executed.

So I think I somehow understand it now but I don't get why, for example in @KeepBotting's script, he doesn't have 'result' in his function? How can it then return something? And I've sometimes seen 'self', like in his function, what is it used for?

function TPointArray.centerPoint():TPoint;
var
tmpArr:TPointArray;
begin
if (length(self) < 1) then exit;
tmpArr := self;
tmpArr.sortFromPoint(self.getMiddle);
result := tmpArr[0];
end;

And I'm also wondering what functions are usually used for? I've never used one before.

The Mayor

2014-08-19 07:12:15

From: Spaceblow


So I think I somehow understand it now but I don't get why, for example in @KeepBotting's script, he doesn't have 'result' in his function? How can it then return something? And I've sometimes seen 'self', like in his function, what is it used for?

function TPointArray.centerPoint():TPoint;
var
tmpArr:TPointArray;
begin
if (length(self) < 1) then exit;
tmpArr := self;
tmpArr.sortFromPoint(self.getMiddle);
result := tmpArr[0];
end;

And I'm also wondering what functions are usually used for? I've never used one before.


Good to see you've been practicing. It looks like you understand basic function parameters and returning different results. You even sussed out the boolToStr :)

IDK what script you are referring to, but if you don't return a result, the function would still work, but it could probably just be a procedure instead. It may have been a function at one stage but the result wasn't needed so it was removed, but it was never changed to a procedure (I've done this before).

The 'self' thing is a little more complicated as it's getting into object oriented programming. Basically in that snippet, 'TPointArray' is the object and the function is referring to itself (i.e. it's getting the center point of itself).

myTPA.centerPoint()
will return the middle TPoint of itself, (the midpoint of 'myTPA'). You could actually just use middleTPA(myTPA) though.

The SRL6 include is structured this way if you want to read some of the include files.


EDIT; Just saw your last question. What are functions used for????

Well, let me give you a few examples of questions which boolean functions can answer:

  • Is the bank screen open, true or false?
  • Is my backpack full?
  • Am I moving (e.g. chopping a tree)?
  • Is my HP less than 20% ?

Now let me give you examples of questions which integer functions can answer:

  • What is my woodcutting level?
  • How many items are in my backpack?
  • How many items are in bank slot #10?
  • How much prayer points do I have left?

All of these questions can be answered with functions currently in the SRL6 include. You've used a lot of them already:

 if bankScreen.isOpen() then writeLn('The bank is open');

bankscreen.isOpen is just a boolean function, just like:

tabBackpack.count()

results in an integer of how many items are currently in your backpack :)

KeepBotting

2014-08-19 08:16:08

From: Spaceblow


So I think I somehow understand it now but I don't get why, for example in @KeepBotting's script, he doesn't have 'result' in his function? How can it then return something? And I've sometimes seen 'self', like in his function, what is it used for?

function TPointArray.centerPoint():TPoint;
var
tmpArr:TPointArray;
begin
if (length(self) < 1) then exit;
tmpArr := self;
tmpArr.sortFromPoint(self.getMiddle);
result := tmpArr[0];
end;

And I'm also wondering what functions are usually used for? I've never used one before.


The Mayor pretty much knocked the nail on the head, but I figure I'll pitch in because you mentioned me :P

Every function has a result. Or if it doesn't, it shouldn't be a function, it should be a procedure.

A function returns a value in the global scope, whereas a procedure just does what it's told. That's the main difference between the the two.

The 'self' identifier is used when you associate a function with one of the types that are built into SRL-6. Using my TPointArray.centerPoint() as an example, TPointArray itself is defined within SRL-6 as an array of TPoints.

TPointArray isn't a name, it's a thing. The fact that I have TPointArray in my function's name indicates that it can be used for any TPointArray.

Here's an example:

bankerTpa.centerPoint();

bankerTpa is a TPointArray, thus I can use it in the function.

All 'self' does is tells the function to perform an operation on itself, which in the example above, would be the TPointArray called bankerTpa.
And if I didn't use the 'self' identifier, I'd have to edit the function every single time I wanted to use it for something different, like so:

function someTpa.centerPoint():TPoint;
var
tmpArr:TPointArray;
begin
if (length(someTpa) < 1) then exit;
tmpArr := someTpa;
tmpArr.sortFromPoint(self.getMiddle);
result := tmpArr[0];
end;

I just woke up so let me know anything I explained badly or you didn't understand :P

Spaceblow

2014-08-19 17:41:51

From: The Mayor

IDK what script you are referring to
I was talking about this one, if you still would want to know. :)

From: The Mayor

EDIT; Just saw your last question. What are functions used for????
Again, thank you very much for making stuff clear. I started exploring the include and thanks to your reply and by looking at the functions I now understand way more.

From: KeepBotting

A function returns a value in the global scope, whereas a procedure just does what it's told. That's the main difference between the the two.
I don't really understand what this 'scope' is that you're talking about, could you please give a little bit of info? I guess it's something complicated but I would like to know what that is. I've already searched it but I really don't understand anything of that, this is the first scripting language that I've ever tried to learn.

From: The Mayor

'self'


From: KeepBotting

'self'

So for example, if this is my function:
function darkBlueCar.centerPoint(): TPoint;
And if I would be using 'self' it would just refer all the time to 'darkBlueCar'? So 'self' would be like a placeholder for 'darkBlueCar' so I wouldn't have to type it all the time, right? Again, thank you, both of you, I greatly appreciate your help, guys like you make villavu look good. :)

KeepBotting

2014-08-19 18:04:44

From: Spaceblow

I don't really understand what this 'scope' is that you're talking about, could you please give a little bit of info? I guess it's something complicated but I would like to know what that is. I've already searched it but I really don't understand anything of that, this is the first scripting language that I've ever tried to learn.


So for example, if this is my function:
function darkBlueCar.centerPoint(): TPoint;
And if I would be using 'self' it would just refer all the time to 'darkBlueCar'? So 'self' would be like a placeholder for 'darkBlueCar' so I wouldn't have to type it all the time, right? Again, thank you, both of you, I greatly appreciate your help, guys like you make villavu look good. :)


Scope refers to whether a variable (or constant or type or … I'll use variable as a blanket term for this explanation) is local or global.

A variable in the local scope can only be accessed by the procedure or function in which it's declared. For example:


procedure theProcedure();
var
theLocalInteger:integer;
theLocalString:string;
begin
end;

theLocalInteger and theLocalString are both local variables (variables in the local scope). If you tried to access these variables from any other part of the script, you'd get an unknown identifier(?) error.

Next part is the globally scoped variables. They can be declared anywhere in your script, but the important part is that they're not declared inside of any procedure or function. All you have to do is stick 'em under a var keyword, like so:


var
theGlobalInteger:integer;
theGlobalString:string;

theGlobalInteger and theGlobalString are both global variables (variables in the global scope). You can access these variables from any other part of the script, and reference them inside any procedure or function.


***

As far as your second question is concerned, yes, you could do that, but that's not how the function is meant to be used.

TPointArray.centerPoint() actually takes a TPointArray when you call it. Think of it not as TPointArray.centerPoint(), but simply as .centerPoint() (or even insert your TPointArray here.centerPoint())

You can append any TPointArray onto the beginning of .centerPoint(), and it'll return the center point of the TPointArray.

TPointArray is a variable type defined in SRL-6, it's not actually the name of the function. The TPointArray part basically just signals that this is a function which performs an operation (centerPoint) on a TPointArray.

In closing, maybe an example will bring all my blithering together nicely:


const //global scope! :D
darkBlueCar:TPointArray;
lightRedCar:TPointArray;
uglyGreenCar:TPointArray;

function TPointArray.centerPoint():TPoint; //the TPointArray function
var
tpa:TPointArray;
begin
if (length(self) < 1) then
exit;
tpa := self;
tpa.sortFromPoint(self.getMiddle());
result := tpa[0];
end;

procedure getMiddlePoints(); //where the magic happens
var //local scope! :D
darkBlue, lightRed, uglyGreen:TPoint; //they have to be TPoints because .centerPoint() returns a TPoint
begin
darkBlue := darkBlueCar.centerPoint(); //the power of 'self' tells .centerPoint() to perform
lightRed := lightRedCar.centerPoint(); //the operation on whatever TPointArray we put to the left of the . (period)
uglyGreen := uglyGreenCar.centerPoint();
end;


Hope I explained that all well :P

Spaceblow

2014-08-19 18:51:26

From: KeepBotting
Scope refers to whether a variable (or constant or type or … I'll use variable as a blanket term for this explanation) is local or global.
Oh so that's what scope is, thank you!

From: KeepBotting

***

As far as your second question is concerned, yes, you could do that, but that's not how the function is meant to be used.

TPointArray.centerPoint() actually takes a TPointArray when you call it. Think of it not as TPointArray.centerPoint(), but simply as .centerPoint() (or even insert your TPointArray here.centerPoint())

You can append any TPointArray onto the beginning of .centerPoint(), and it'll return the center point of the TPointArray.

TPointArray is a variable type defined in SRL-6, it's not actually the name of the function. The TPointArray part basically just signals that this is a function which performs an operation (centerPoint) on a TPointArray.

In closing, maybe an example will bring all my blithering together nicely:


const //global scope! :D
darkBlueCar:TPointArray;
lightRedCar:TPointArray;
uglyGreenCar:TPointArray;

function TPointArray.centerPoint():TPoint; //the TPointArray function
var
tpa:TPointArray;
begin
if (length(self) < 1) then
exit;
tpa := self;
tpa.sortFromPoint(self.getMiddle());
result := tpa[0];
end;

procedure getMiddlePoints(); //where the magic happens
var //local scope! :D
darkBlue, lightRed, uglyGreen:TPoint; //they have to be TPoints because .centerPoint() returns a TPoint
begin
darkBlue := darkBlueCar.centerPoint(); //the power of 'self' tells .centerPoint() to perform
lightRed := lightRedCar.centerPoint(); //the operation on whatever TPointArray we put to the left of the . (period)
uglyGreen := uglyGreenCar.centerPoint();
end;


Hope I explained that all well :P
You know, to be honest, I've been looking at that snippet for like literally 30 minutes and analyzing everything and trying to understand it. And unfortunately, I'm still very confused. Maybe it's because it's almost 1AM here and I'm in need of some sleep or maybe I'm just not smart enough to understand that… If you have enough time and if you're willing to do it, could you please make another snippet? With the same type of comments you did (//…) but other examples. Maybe the TPointArray or centerPoint is confusing me too much, or is 'self' only being used for that kind of stuff? Again, thank you in advance, you're great a help and I really appreciate it a lot! And you don't have to help me, tell me if you don't want to, no offence will be taken, I promise! :) I just have the feeling of being annoying asking the same stuff over and over, maybe I just need to forget 'self' exists and make scripts without it. :p

KeepBotting

2014-08-19 19:04:13

From: Spaceblow


You know, to be honest, I've been looking at that snippet for like literally 30 minutes and analyzing everything and trying to understand it. And unfortunately, I'm still very confused. Maybe it's because it's almost 1AM here and I'm in need of some sleep or maybe I'm just not smart enough to understand that… If you have enough time and if you're willing to do it, could you please make another snippet? With the same type of comments you did (//…) but other examples. Maybe the TPointArray or centerPoint is confusing me too much, or is 'self' only being used for that kind of stuff? Again, thank you in advance, you're great a help and I really appreciate it a lot! And you don't have to help me, tell me if you don't want to, no offence will be taken, I promise! :) I just have the feeling of being annoying asking the same stuff over and over, maybe I just need to forget 'self' exists and make scripts without it. :p


Okay so, from the ground up (pictures stolen from The Mayor):

This is a TPoint:

This a TPointArray (an array of TPoints):

This is TPointArray.centerPoint(), a function the takes a TPointArray and gives you the center TPoint of it.

function TPointArray.centerPoint():TPoint;
var
tpa:TPointArray;
begin
if (length(self) < 1) then
exit;
tpa := self;
tpa.sortFromPoint(self.getMiddle());
result := tpa[0];
end;

The TPointArray part of TPointArray.centerPoint() signals that this is a function that actually accepts a TPointArray.

'TPointArray' is signaling the type of variable that the function accepts. It'll be replaced with the name of the variable of that type when you call it.

Maybe doing it another way will help you understand:

function centerPoint(input:TPointArray):TPoint;
var
output:TPointArray;
begin
if (length(input) < 1) then
exit;
input := output;
output.sortFromPoint(output.getMiddle());
result := output[0];
end;

You would call the first example like
darkBlueCar.centerPoint();
, and you would call the second example like
centerPoint(darkBlueCar);

The difference is all in how the parameters are presented. Notice how in the second example, we don't use "self", because the input parameter is declared as a local variable.

Regardless, both of these functions will return the center TPoint of the TPointArray you specify (refer to pictures)

And don't think that you're being a bother at all :) feel free to ask the same question as many times as you need until you get the answer in a form that helps you understand it best.

Spaceblow

2014-08-20 06:42:53

From: KeepBotting

And don't think that you're being a bother at all :) feel free to ask the same question as many times as you need until you get the answer in a form that helps you understand it best.
You're really a nice person. :)

So it's just a different way of getting a result? So the translation of
darkBlueCar.centerPoint();
would be 'subject -> get the center point'.

And the translation of
centerPoint(darkBlueCar);
would be 'get the center point of (subject)', right?

So if I would have to explain your snippet to someone who has never heard anything about scripting it would be:
function TPointArray.centerPoint():TPoint; //subject -> get the center point of it and return a TPoint, (subject could be replaced by for example [Point(117, 162), Point(118, 145)]
var
tpa:TPointArray; //you're saying tpa is a TPointArray
begin
if (length(self) < 1) then //if the length, of the TPointArray that you've input (the one from 'TPointArray.centerPoint():TPoint;') is shorter than 1 then it will exit the procedure
exit;
tpa := self; //tpa (so the TPointArray that you've made in var) is now being assigned to TPointArray (the one you can find in 'TPointArray.centerPoint():TPoint;'), so now their value is equal
tpa.sortFromPoint(self.getMiddle()); //so you have your tpa (TPointArray like you said in var), you make it sort the points from the middle TPoint of the TPointArray (I'm talking about the one from the first line of this snippet, not the one under var), and the information gets stored in 'tpa', so the var that you've made
result := tpa[0]; //the result, so the stuff that's going to be returned if I would be using the function, would be the information stored in the three first letters of the previous line: 'tpa'. And the [0] means it's going to start from 0 and that will be the middle TPoint
end;

I hope I have atleast a few things right. Tell me if you don't undestand something, English isn't my native language. :)


As for your second snippet, I actually got a little bit confused. You arranged stuff differently than in your first snippet (the one with 'self'), is this by mistake or on purpose?



I tried to make it clear by using Paint. :p

By looking at my Paint drawing I've also seen that you've switched 'tpa := self;' to 'input := output'. So that in the second example the variable is placed after the assignment operator and not in front of it. Was this also on purpose or doesn't it matter at all whether the var is after the ':=' or before?

KeepBotting

2014-08-20 07:57:56

From: Spaceblow

So it's just a different way of getting a result? So the translation of
darkBlueCar.centerPoint();
would be 'subject -> get the center point'.

And the translation of
centerPoint(darkBlueCar);
would be 'get the center point of (subject)', right?

Ding ding! We have a winner! :) yes that's correct.

From: Spaceblow

So if I would have to explain your snippet to someone who has never heard anything about scripting it would be:
function TPointArray.centerPoint():TPoint; //subject -> get the center point of it and return a TPoint, (subject could be replaced by for example [Point(117, 162), Point(118, 145)] OR the name of a global TPointArray we've declared somewhere else
var
tpa:TPointArray; //you're saying tpa is a TPointArray, a temporary variable we're using just for the duration of the function
begin
if (length(self) < 1) then //if the length, of the TPointArray that you've input (the one from 'TPointArray.centerPoint():TPoint;') is shorter than 1 then it will exit the procedure, because less than 1 is 0, meaning there are no TPoints in the Array, thus it's impossible to return a center point
exit;
tpa := self; //tpa (so the TPointArray that you've made in var) is now being assigned to TPointArray (the one you can find in 'TPointArray.centerPoint():TPoint;'), so now their value is equal, and we have a local variable to work with, inside the function
tpa.sortFromPoint(self.getMiddle()); //so you have your tpa (TPointArray like you said in var), you make it sort the points from the middle TPoint of the TPointArray (I'm talking about the one from the first line of this snippet, not the one under var), and the information gets stored in 'tpa', so the var that you've made
result := tpa[0]; //the result, so the stuff that's going to be returned if I would be using the function, would be the information stored in the three first letters of the previous line: 'tpa'. And the [0] means it's going to start from 0 and that will be the middle TPoint
end;

This was all fundamentally correct but I've added a bit on to a few of your original comments ^^

From: Spaceblow

As for your second snippet, I actually got a little bit confused. You arranged stuff differently than in your first snippet (the one with 'self'), is this by mistake or on purpose?



I tried to make it clear by using Paint. :p

By looking at my Paint drawing I've also seen that you've switched 'tpa := self;' to 'input := output'. So that in the second example the variable is placed after the assignment operator and not in front of it. Was this also on purpose or doesn't it matter at all whether the var is after the ':=' or before?

Assigning 'input' to 'output' and then performing operations on 'output' was purely to show the distinction between the parameter than the user will input, and the value that the function will output. It was on purpose but it was also purely redundancy.

Most of the time, it does matter what's on which side of the assignment operator, but not in function centerPoint(input:TPointArray):TPoint;'s case. The assignment operator there is almost completely redundant, if I really wanted to make a .centerPoint function without using 'self', I'd do:


function centerPoint(input:TPointArray):TPoint;
begin
if (length(input) < 1) then
exit;
input.sortFromPoint(input.getMiddle());
result := input[0];
end;

Spaceblow

2014-08-20 09:23:38

From: KeepBotting
*
Alright, I think I fully understand it now. I'll keep in mind to always use something like 'inputstuff := outputstuff' and not the opposite. You've helped me a lot, I'm so thankful for that. I think a lot of beginners can learn very much from this thread. I'm forever in your debt! :)

EDIT: I have another question about 'result' in for example, this snippet:
function location:TPoint;
var
places:TPointArray;
stiles_dist, dock_dist:integer;
begin
if (not isLoggedIn()) then
exit;

result := karamja.getPlayerPos;
if result.equals([-1, -1]) then exit;

stiles_dist := distance(result, walk_ways[0][high(walk_ways[0])]);
dock_dist := distance(result, walk_ways[0][low(walk_ways[0])]);

if stiles_dist < 20 then players[currentPlayer].location := 'stiles' else
if dock_dist < 20 then players[currentPlayer].location := 'dock' else
players[currentPlayer].location := 'unknown';
writeDebug('Our location is currently: '+ players[currentPlayer].location);
end;
Does it actually matter where your 'result' is placed? Is there any difference if you place it in the beginning of your function or at the end of it? Or does it always run 'result' as the last line and it actually doesn't matter at all where you place it? I think it's logical to place it at the end.

KeepBotting

2014-08-21 11:41:40

From: Spaceblow
Alright, I think I fully understand it now. I'll keep in mind to always use something like 'inputstuff := outputstuff' and not the opposite. You've helped me a lot, I'm so thankful for that. I think a lot of beginners can learn very much from this thread. I'm forever in your debt! :)

EDIT: I have another question about 'result' in for example, this snippet:
function location:TPoint;
var
places:TPointArray;
stiles_dist, dock_dist:integer;
begin
if (not isLoggedIn()) then
exit;

result := karamja.getPlayerPos;
if result.equals([-1, -1]) then exit;

stiles_dist := distance(result, walk_ways[0][high(walk_ways[0])]);
dock_dist := distance(result, walk_ways[0][low(walk_ways[0])]);

if stiles_dist < 20 then players[currentPlayer].location := 'stiles' else
if dock_dist < 20 then players[currentPlayer].location := 'dock' else
players[currentPlayer].location := 'unknown';
writeDebug('Our location is currently: '+ players[currentPlayer].location);
end;
Does it actually matter where your 'result' is placed? Is there any difference if you place it in the beginning of your function or at the end of it? Or does it always run 'result' as the last line and it actually doesn't matter at all where you place it? I think it's logical to place it at the end.


Nope, it doesn't matter where you assign your result. Some functions actually start off by setting the result to false, preemptively at the beginning of the function.

It's most logical to assign a result at the end, but as long as you're assigning the result to something that's been declared previously, the script doesn't care. Just be aware than unless you specifically tell the function to exit after assigning a result, it'll keep on going until the end of the function. You may or may not want that.

An example being; you can't assign the result to the value of theVariable, but then only give theVariable a value until later, or the function will always return 0, or false, or '' depending on what type theVariable is.

Spaceblow

2014-08-21 12:35:24

From: KeepBotting
Nope, it doesn't matter where you assign your result. Some functions actually start off by setting the result to false, preemptively at the beginning of the function.

It's most logical to assign a result at the end, but as long as you're assigning the result to something that's been declared previously, the script doesn't care. Just be aware than unless you specifically tell the function to exit after assigning a result, it'll keep on going until the end of the function. You may or may not want that.

An example being; you can't assign the result to the value of theVariable, but then only give theVariable a value until later, or the function will always return 0, or false, or '' depending on what type theVariable is.
Thank you once again, because of you this isn't a mystery to me anymore. :)