SRL Archive - OSR Help

Help with pointers..


rj

2013-12-06 16:09:28

I can't figure out how to assign the value of a function to a boolean.

For example, consider this array of functions:

g := [@test1, @test2];


What I want to do is, assign the value of the executed functions to a boolean array.

I tried following Brandons lape tutorial but I'm not sure if the pointer part applies to functions… Heres my code:

type
PBool = array[0..1] of ^boolean;

var
g:array[0..1] of function:boolean;
bool:Pbool;
ptr:PChar;
i:Integer;

function test1:boolean;
begin
result := false;
end;

function test2:boolean;
begin
result := false;
end;

begin
g := [@test1, @test2];
bool := @g;
for i := 0 to high(g) do
begin
writeln(PBool(ptr)^);
ptr := Ptr + sizeof(Boolean);
end;
end.


I get a acess violation error at

Exception in Script: Can't assign "^array [0..1] of function():(False=0, True=1)" to "array [0..1] of ^(False=0, True=1)" at line 22, column 8

Probably line

    writeln(PBool(ptr)^);

bonsai

2013-12-06 16:30:23

From: Officer Barbrady

type
PBool = array[0..1] of ^boolean;

var
g:array[0..1] of function:boolean;
bool:Pbool;
ptr:PChar;
i:Integer;

function test1:boolean;
begin
result := false;
end;

function test2:boolean;
begin
result := false;
end;

begin
g := [@test1, @test2];
bool := @g;
for i := 0 to high(g) do
begin
writeln(PBool(ptr)^);
ptr := Ptr + sizeof(Boolean);
end;
end.


You still need to loop through the array, you can't call them both by referencing the array.

I didn't try to compile this but something like…


for i := 0 to high(g) do
begin
bool[i] := g[i]();
end;

not sure what you were trying to do with the PBool(ptr)^ stuff???

rj

2013-12-06 16:36:24

From: bonsai
You still need to loop through the array, you can't call them both by referencing the array.

I didn't try to compile this but something like…


for i := 0 to high(g) do
begin
bool[i] := g[i]();
end;

not sure what you were trying to do with the PBool(ptr)^ stuff???



var
g:array[0..1] of function:boolean;
bool:boolean;

i:Integer;

function test1:boolean;
begin
result := false;
end;

function test2:boolean;
begin
result := false;
end;

begin
g := [@test1, @test2];
for i := 0 to high(g) do
bool := g[i];
for i := 0 to high(bool) do
writeln(bool);
end.

access violation, and im pretty sure I have to use pointers

Gu3st...

2013-12-06 17:24:17

var
functionList : array of function(): Boolean;
I : Integer;

function test1: Boolean;
begin
result := False;
end;

function test2: Boolean;
begin
result := True;
end;

begin
setLength(functionList, 2);
functionList[0] := @ test1; // fix this before compiling.
functionList[1] := @test2;

for I := 0 to high(functionList) do
writeLn(functionList[I]);
end.

masterBB

2013-12-06 17:25:55

From: Officer Barbrady
access violation, and im pretty sure I have to use pointers


For some reason [@funct1. @funct2] doesn't work.

set them like this:
  g[0] :=  [MENTION=48801]test1[/MENTION];
g[1] := @test2;

edit:
.. okay.. I see, today we are all going to ninja me. Great.

Gu3st...

2013-12-06 17:36:43

From: masterBB
For some reason [@funct1. @funct2] doesn't work.

set them like this:
  g[0] :=  @<b><u><a href="http://villavu.com/forum/member.php?u=48801" target="_blank">test1</a></u></b>;
g[1] := @test2;

edit:
.. okay.. I see, today we are all going to ninja me. Great.
I'm sorry :(
[video=youtube;8f2YeOsXETA]https://www.youtube.com/watch?v=8f2YeOsXETA[/video]

rj

2013-12-06 17:51:26

From: Zyt3x
var
functionList : array of function(): Boolean;
I : Integer;

function test1: Boolean;
begin
result := False;
end;

function test2: Boolean;
begin
result := True;
end;

begin
setLength(functionList, 2);
functionList[0] := @ test1; // fix this before compiling.
functionList[1] := @test2;

for I := 0 to high(functionList) do
writeLn(functionList[I]);
end.


How come I get a type mismatch though?

var
functionList : array of function(): Boolean;
I : Integer;
bool:boolean;

function test1: Boolean;
begin
result := False;
end;

function test2: Boolean;
begin
result := True;
end;

begin
setLength(functionList, 2);
functionList[0] := @ test1; // fix this before compiling.
functionList[1] := @test2;

for I := 0 to high(functionList) do
writeLn(functionList[I]);
bool := functionList[0];
end.

functionList[0] is a boolean isn't it?

masterBB

2013-12-06 18:05:10

From: Officer Barbrady
How come I get a type mismatch though?
functionList[0] is a boolean isn't it?


Oh, you are using pascalscript.
Remove the space between @ and test1. Also add these () when you call a function. Like ALWAYS DO THAT, OR I WILL BAN YOU. It makes the script more readable and in this case also make it compile.

var
functionList : array of function(): Boolean;
I : Integer;
bool:boolean;

function test1: Boolean;
begin
result := False;
end;

function test2: Boolean;
begin
result := True;
end;

begin
setLength(functionList, 2);
functionList[0] := [MENTION=48801]test1[/MENTION];
functionList[1] := @test2;

for I := 0 to high(functionList) do
writeLn(functionList[I]);
bool := functionList[0]();
end.

edit:
lol someone is called test1 so I can no longer use @test 1…

rj

2013-12-06 18:16:18

From: masterBB
Oh, you are using pascalscript.
Remove the space between @ and test1. Also add these () when you call a function. Like ALWAYS DO THAT, OR I WILL BAN YOU. It makes the script more readable and in this case also make it compile.

var
functionList : array of function(): Boolean;
I : Integer;
bool:boolean;

function test1: Boolean;
begin
result := False;
end;

function test2: Boolean;
begin
result := True;
end;

begin
setLength(functionList, 2);
functionList[0] := [MENTION=48801]test1[/MENTION];
functionList[1] := @test2;

for I := 0 to high(functionList) do
writeLn(functionList[I]);
bool := functionList[0]();
end.

edit:
lol someone is called test1 so I can no longer use @test 1…


woops was supposed to be using lape was running a PS script sorry works in lape :d


rep for both of you


@masterBB how do I do it with procedures?

I'm getting an error when I try

procs[0] := @openBank,
procs[1] := @Progress;