Results 1 to 8 of 8

Thread: Pointers, can they be called in scar?

  1. #1
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Pointers, can they be called in scar?

    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.

    SCAR Code:
    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

  2. #2
    Join Date
    May 2007
    Location
    Ohio
    Posts
    2,296
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

    That Work?

  3. #3
    Join Date
    Jul 2007
    Location
    Norway.
    Posts
    1,938
    Mentioned
    3 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by Timer View Post
    SCAR Code:
    Type
      MyTypes = Record
        intPtr: Integer;
        funcPtr: function(i: Integer): Boolean;
        ProcPtr: procedure;
        intReDef: Integer;
      End;

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

  4. #4
    Join Date
    Jun 2006
    Posts
    3,861
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    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:
    SCAR Code:
    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.

  5. #5
    Join Date
    Feb 2006
    Location
    Amsterdam
    Posts
    13,692
    Mentioned
    146 Post(s)
    Quoted
    130 Post(s)

    Default

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



    The best way to contact me is by email, which you can find on my website: http://wizzup.org
    I also get email notifications of private messages, though.

    Simba (on Twitter | Group on Villavu | Website | Stable/Unstable releases
    Documentation | Source | Simba Bug Tracker on Github and Villavu )


    My (Blog | Website)

  6. #6
    Join Date
    Dec 2007
    Location
    Somewhere in Idaho
    Posts
    480
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by EvilChicken! View Post
    Wow. Explain how and what to use
    SCAR Code:
    ProcPtr: procedure;
    and
    SCAR Code:
    funcPtr: function(i: Integer): Boolean;
    for please?
    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
    SCAR Code:
    roadWalkerDirections(roadColorProc : Function : Integer; startAngle : Integer; directions : array of string; maxDistances : array of Integer);
    It even compiles!

  7. #7
    Join Date
    Aug 2006
    Location
    London
    Posts
    2,021
    Mentioned
    2 Post(s)
    Quoted
    0 Post(s)

    Default

    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.
    Code:
    var
      opcode: integer;
    
    opcode:= ReadNextByteFromSocket();
    packet_handler_array[opcode]();
    i cant think of much use for scar colour clicky though
    Join the Official SRL IRC channel. Learn how to Here.

  8. #8
    Join Date
    Dec 2006
    Location
    Banville
    Posts
    3,914
    Mentioned
    12 Post(s)
    Quoted
    98 Post(s)

    Default

    Use a Delphi plugin to return them like:

    SCAR Code:
    {...}

    var I: TVariant;

    {...}

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

    {...}
    The jealous temper of mankind, ever more disposed to censure than
    to praise the work of others, has constantly made the pursuit of new
    methods and systems no less perilous than the search after unknown
    lands and seas.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. c++(pointers) help
    By hackncrack1 in forum C/C++ Help and Tutorials
    Replies: 10
    Last Post: 05-25-2009, 01:38 PM
  2. Pointers?
    By mat_de_b in forum C/C++ Help and Tutorials
    Replies: 8
    Last Post: 11-05-2007, 09:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •