SRL Archive - General

Pointers, can they be called in scar?


boberman

2008-05-03 14:34:46

Hey everyone, a new script I am working on would benefit greatly if scar can use pointers, specifically function/procedure pointers. I have failed so far to get one implemented and I am wondering if it is just a syntax issue (since scar isn't exactly pascal)

Here is what I have tried.

type
intPtr = ^Integer; // Fail
funcPtr = ^function(i : integer); // Fail
ProcPtr = ^procedure; // Fail
intReDef = Integer; // Success

So, can it be done? Or do I have to work around it, *sniff* I miss my pointers :)

Timer

2008-05-03 16:10:27

Type
MyTypes = Record
intPtr: Integer;
funcPtr: function(i: Integer): Boolean;
ProcPtr: procedure;
intReDef: Integer;
End;

That Work?

EvilChicken!

2008-05-03 16:14:21

From: Timer
Type
MyTypes = Record
intPtr: Integer;
funcPtr: function(i: Integer): Boolean;
ProcPtr: procedure;
intReDef: Integer;
End;

That Work?


Wow. Explain how and what to use
ProcPtr: procedure;
and
funcPtr: function(i: Integer): Boolean;
for please? :rolleyes:

bullzeye95

2008-05-03 16:28:54

You can't have REAL pointers in SCAR, but you can have kind of fake pointers to functions and procedures by doing something like this:
program New;
var
Proc: Procedure;

procedure lol1;
begin
writeln('1');
end;

procedure lol2;
begin
writeln('2');
end;

procedure lol3;
begin
writeln('3');
end;

begin
case Random(3) of
0: proc:= @lol1;
1: proc:= @lol2;
2: proc:= @lol3;
end;
proc(); //note the () at the end. you must have them
end.
As far as variables, however, I'm not sure if it's possible to have pointers… possibly through a plugin, but I don't know.

Wizzup?

2008-05-03 16:44:14

Raymond once made a plugin for SCAR which allowed to set and get Variant's pointers.

boberman

2008-05-03 17:48:21

From: EvilChicken!
Wow. Explain how and what to use
ProcPtr: procedure;
and
funcPtr: function(i: Integer): Boolean;
for please? :rolleyes:


Actually it can be quite useful. For example, I want to write a script that calls a function to find the road color, however, I don't want to bind the script to one road color finding function or another. What do you do? Why, you pass in the road finding function to the new function to allow someone else's function to do the work (say that five times fast).

Assigning a function/procedure the address of another function or procedure might just work the way I want it to. Ultimately I'm thinking of trying to make an array of functions (a stack of commands?). Though, I've about convinced myself not to do that, I don't quite see the benifit that I once was going for.

Anyways, thanks for the pointers…. (couldn't resist)

… Muha ha ha … Thanks guys, your tips work great and solve the problem quite nicely. Imagine a function declaration that looks like this procedure
roadWalkerDirections(roadColorProc : Function : Integer; startAngle : Integer; directions : array of string; maxDistances : array of Integer);
It even compiles!

Yakman

2008-05-03 19:36:59

you can have an array of function pointers, then when implementing a socket connection, use the opcode to index the array
where each function in the array can handle the packet properly

eg.

var
opcode: integer;

opcode:= ReadNextByteFromSocket();
packet_handler_array[opcode]();

i cant think of much use for scar colour clicky though

R0b0t1

2008-05-03 21:32:40

Use a Delphi plugin to return them like:


{...}

var I: TVariant;

{...}

ptrI := GetVariablePointer(I {: TVariant});

{...}