SRL Archive - OSR Help

Getting Myself Confused...


**BANNED The Man

2010-12-23 16:41:28

Ok so I want to make a kind of "location" function…
Function WhatUp(T : String);
Begin
Case T Of
'Banked' : WalkToTrees;
'At Trees' : ChopTrees;
'Chopped Trees' : WalkToBank;
'At Bank' : DoBank;
End;
End;

So from there can I just call:
WhatUp('Banked');

I kind of improvised as I wrote this thread so sorry if its bad :P

Thankyoooo
-Boom

TomTuff

2010-12-23 16:53:53

no, what i would do is something like this:


{$i SRL\SRL.scar}


function WalkToTrees: Boolean;
begin
end;

function ChopTrees: Boolean;
begin
end;

function WalkToBank: Boolean;
begin
end;

function DoBank: Boolean;
begin
end;


begin
Case Lowercase(Players[CurrentPlayer].Loc) of
'banked':
if WalkToTrees then
Players[CurrentPlayer].Loc := 'at trees';
'at trees':
if ChopTrees then
Players[CurrentPlayer].Loc := 'chopped trees';
'chopped trees':
if WalkToBank then
Players[CurrentPlayer].Loc := 'at bank';
'at bank':
if DoBank then
Players[CurrentPlayer].Loc := 'banked';
end;
end.

questions?

**BANNED The Man

2010-12-23 16:59:47

So it would be:
{$i SRL\SRL.scar}


function WalkToTrees: Boolean;
begin
//Walking stuff here
Players[CurrentPlayer].Loc := 'At Trees';
end;

function ChopTrees: Boolean;
begin
end;

function WalkToBank: Boolean;
begin
end;

function DoBank: Boolean;
begin
end;


begin
Case Lowercase(Players[CurrentPlayer].Loc) of
'banked':
if WalkToTrees then
Players[CurrentPlayer].Loc := 'at trees';
'at trees':
if ChopTrees then
Players[CurrentPlayer].Loc := 'chopped trees';
'chopped trees':
if WalkToBank then
Players[CurrentPlayer].Loc := 'at bank';
'at bank':
if DoBank then
Players[CurrentPlayer].Loc := 'banked';
end;
end.

Right? :)

Thankyooo
-Boom

TomTuff

2010-12-23 17:06:00

functions return a result, which can be any type that has been defined (so either something in SRL, reflection if your using it, obviously the default types like integers, booleans, and strings, and custom types). Booleans are commonly used to figure out if something actually happened. An example to tell if you opened the bank successfully would be the SRL function "BankScreen". By using booleans, you can decide when to log off and when to keep going.

All you would need to do is modify your current procedures into functions, and add that case statement into a repeat..until(not(LoggedIn)) loop, and you then have a pseudo-state machine!

**BANNED The Man

2010-12-23 17:10:27

Hmm, there you confused me, so you are saying do:
function WalkToTrees: Boolean;
begin
//Walking stuff here
Result := Players[CurrentPlayer].Loc := 'At Trees';
end;

Thankyoooo
-Boom

TomTuff

2010-12-23 17:19:48

Functions can return something. Let's take a common function for an example: Random(). Random generates a number between 0 and whatever integer you feed into it. the number it generates is the result of the function random.

In runescape script, functions that return booleans are commonly used to tell if something worked or not. At the end of the function, some other function of some sort is called to tell if the function carried out properly.

Let's look at the OpenTheBank function from my cooker as an example:


function OpenTheBank: Boolean;
var
CTS, H, x, y, i: Integer;
TPA: TPointArray;
ATPA: Array of TpointArray;
begin
Result := False;
R_FindRandoms;
if not(LoggedIn) then
Exit;
if BankScreen then
begin
Result := True;
Exit;
end;
CTS := GetColorToleranceSpeed;
SetColorToleranceSpeed(2);
SetColorspeed2Modifiers(0.027, 0.031);
FindColorsTolerance(TPA, 3817283, MSX1, MSY1, MSX2, MSY2, 14);
SetColorToleranceSpeed(CTS);
if (Length(TPA) < 1) then
begin
Logout;
Exit;
end;
ATPA := TPAtoATPAEx(TPA, 35, 35);
SortATPAFromFirstPoint(ATPA, IntToPoint(MSCX, MSCY));
if ShowDebugging then
DebugATPABounds(ATPA);
H := Length(ATPA);
for i := 0 to H do
begin
MiddleTPAEx(ATPA[i], x, y);
MMouse(x, y, 4, 4);
if WaitUptext('ank booth', 800) then
begin
ClickMouse2(False);
Result := WaitOption('quickly', 800);
end;
if (Result or BankScreen) then
Break;
end;
FFlag(0);
Result := WaitFunc(@BankScreen, 20 + Random(25), 5000);
Wait(600 + Random(60));
end;


See how on the second to last line, i call "Result := "SOMETHING""? WaitFunc is a function in SRL that calls the function (Defined like @Function) ever x number of seconds (the 2nd parameter) over and over until it either returns true or the total time is longer than the 3rd parameter (in ms). If it finds the Bank screen to be open, then the result is true.

If the result is true, i know i can continue on with my script. However, if it returns false, i know that something went wrong, so the player needs to be logged out.

Catch my drift?

**BANNED The Man

2010-12-23 18:02:49

I understand what you are talking about, but don't understand what you are suggesting xD
Are you suggesting:

Function Bank : Boolean;
Begin
if DoBankStuff then
Result := WalkToTrees;
End;
? :P

-Boom

TomTuff

2010-12-23 18:17:37

no.

depending on the location, it runs the function, and if the function returns true, then it sets the location to the next step.

**BANNED The Man

2010-12-23 18:41:08

From: TomTuff
no.

depending on the location, it runs the function, and if the function returns true, then it sets the location to the next step.


Ohhhhhh :duh:
I get what you are suggesting.
Ok.
Do you have MSN, I might need some more help :D

Thankyoooo
-Boom

Gu3st...

2010-12-23 18:54:07

To be even better practice, the player.loc should be set inside the walking function.

**BANNED The Man

2010-12-23 18:58:19

From: Dynamite
Hmm, there you confused me, so you are saying do:
function WalkToTrees: Boolean;
begin
//Walking stuff here
Result := Players[CurrentPlayer].Loc := 'At Trees';
end;

Thankyoooo
-Boom


From: Zyt3x
To be even better practice, the player.loc should be set inside the walking function.


Like that ^ ?

-Boom

Gu3st...

2010-12-23 19:02:50

Correct :)

TomTuff

2010-12-24 01:31:57

From: Zyt3x
Correct :)


Not correct, that always sets the results to true… so why make it a function at that point?

Dgby714

2010-12-24 04:58:49

From: TomTuff
Not correct, that always sets the results to true… so why make it a function at that point?


^ I agree and also You need a function to find where the player is currently.

Take for instance a player starts the script.
Players[CurrentPlayer].Loc is empty. (Unless you have the player specify where he or she is)
So in the case
case Players[CurrentPlayer].Loc of
'Whatever1': DoSomething;
'Whatever2': DoSomethingElse;
else
Players[CurrentPlayer].Loc := FindWhereImAt;
end;

Gu3st...

2010-12-24 08:57:30

From: TomTuff
Not correct, that always sets the results to true… so why make it a function at that point?
Ow, yeah I misread his post.

I meant
if Result then
Players[CurrentPlayer].Loc := 'At trees';