SRL Archive - Help and Support

is this possible? array of function


Bonfield

2010-09-16 04:34:03


function Open_Bank(Bank: String);
var
func: array of function;
i: function;
begin
func := [OpenBankFast(Bank), OpenBankGlass(Bank, False, False), OpenBank, OpenBankNPC, OpenBankQuiet];

for i := func[0] to func[5] do // change to low(func) high(func)
if(func[i]) then
Result := True;
else
Result := False;
end;

can this be done?

Shuttleu

2010-09-16 04:38:21

From: Bonfield

function Open_Bank(Bank: String);
var
func: array of function;
i: function;
begin
func := [OpenBankFast(Bank), OpenBankGlass(Bank, False, False), OpenBank, OpenBankNPC, OpenBankQuiet];

for i := func[0] to func[5] do
if(func[i]) then
Result := True;
else
Result := False;
end;

can this be done?


as far as i am aware, no.

besides, when it gets to this line
func := [OpenBankFast(Bank), OpenBankGlass(Bank, False, False), OpenBank, OpenBankNPC, OpenBankQuiet];
it will just try to open the bank and return a load of booleans
so func would acctually have to be a array of boolean

~shut

Bonfield

2010-09-16 04:42:46

i had this

if (OpenBankFast(Bank)) then Result := True else
if (OpenBankGlass(Bank, False, False)) then Result := True else
if (OpenBank) then Result := True else
if (OpenBankNPC) then Result := True else
if (OpenBankQuiet) then Result := True else
Result := False;

but wanted to do it another way just cause it looks ugly, i dont really understand how it would return booleans though

Shuttleu

2010-09-16 04:46:22

well
func := [OpenBankFast(Bank), OpenBankGlass(Bank, False, False), OpenBank, OpenBankNPC, OpenBankQuiet];
it will set func to the array, now when it sets array 0 it will do OpenBankFast which will return a boolean, then it will try to set array 1 which will run OpenBankGlass, which will then return another boolean, etc

~shut

Bonfield

2010-09-16 04:55:21

yeah i see, in my head adding exit; will fix it but i think thats way to simple

could i create a type so i can use a array of functions?

bugger0001

2010-09-16 04:58:31

func := [OpenBankFast(Bank), OpenBankGlass(Bank, False, False), OpenBank, OpenBankNPC, OpenBankQuiet];
I think this kind of syntax can be a bit unclear sometimes, but if you think what it's all about then probably you'll get it:

func[0] := OpenBankFast(Bank); //this means executing OpenBankFast and then setting func[0] to whatever OpenBankFast results
func[1] := OpenBankGlass(Bank, False, False);//this means executing OpenBankGlass and then setting func[1] to whatever OpenBankFast results
func[2] := OpenBank; //etc
func[3] := OpenBankNPC; //etc
func[4] := OpenBankQuiet; //etc


EDIT:
Ok…How about this to make your initial code nicer?

Result := True;
if not (OpenBankFast(Bank)) then
if not (OpenBankGlass(Bank, False, False)) then
if not (OpenBank) then
if not (OpenBankNPC) then
if not (OpenBankQuiet) then Result := False;

Bonfield

2010-09-16 05:01:33

yeah i understood that part but using
 array of function
give me identifier expected errors

EDIT: thanks i will probbaly use that now, im still curious if it is possible to use an array of function

Sir R. M8gic1an

2010-09-16 05:13:25

I see nielsie is on the thread, so he's reply will probably be more accurate >.>


var
func: array of function(params): ResultType;
begin
SetLength(func, 3);
func := [@DoThis, @DoThat, @DoOther];

Result := Func[1]();
end;

However, the function would all need to take the same parameters.

~RM

Bonfield

2010-09-16 05:26:23

i though of trying this

type
func = procedure();

function Open_Bank(Bank: String): Boolean;
var
What_Function: array of func;
i: func;
begin
func{should be What_Function} := [OpenBankFast(Bank), OpenBankGlass(Bank, False, False), OpenBank, OpenBankNPC, OpenBankQuiet];
// ^ internal error line
for i := What_Function[@0] to What_Function[@5] do
if(What_Function[@i]) then
begin
Result := True;
writeln('Open_Bank worked');
Exit;
end else
Result := False;
end;

that gives an internal error, so im guessing im calling this wrong

EDIT

However, the function would all need to take the same parameters.


this would mean copying all the banking functions from banking.scar to modify so there is no/all the same parameters

Shuttleu

2010-09-16 05:33:56

maybe you could do a smart bank finding?
so it goes down the list till it finds one which works, then it will skip the ones which didnt work previously, thus minimizing the bottish looks :)

it may be a bit off topic but i thought it may help

~shut

Bonfield

2010-09-16 05:51:06

its a good idea i like it, i originally wanted to use it as a failsafe for banking thats all

EDIT: anyone know what Internal error (20) means

Sir R. M8gic1an

2010-09-16 06:38:59

a loop uses an integer…

for i:= 0 to …

you're trying to use a func instead of an integer.

for i:= 0 to High(What_Func) do
if What_Func[i]() then
//...

~RM