Results 1 to 9 of 9

Thread: Help with pointers..

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default Help with pointers..

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

    For example, consider this array of functions:

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

    Simba 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

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

    Simba Code:
    writeln(PBool(ptr)^);

  2. #2
    Join Date
    Oct 2013
    Location
    East Coast USA
    Posts
    770
    Mentioned
    61 Post(s)
    Quoted
    364 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    Simba 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.
    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...

    Simba Code:
    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???

  3. #3
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by bonsai View Post
    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...

    Simba Code:
    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???
    Simba Code:
    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

  4. #4
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Simba Code:
    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.
    Last edited by Zyt3x; 12-06-2013 at 10:25 PM. Reason: FUCK MENTION-TAGS

  5. #5
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    access violation, and im pretty sure I have to use pointers
    For some reason [@funct1. @funct2] doesn't work.

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

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

  6. #6
    Join Date
    Jan 2007
    Posts
    8,876
    Mentioned
    123 Post(s)
    Quoted
    327 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    For some reason [@funct1. @funct2] doesn't work.

    set them like this:
    Simba Code:
    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

  7. #7
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by Zyt3x View Post
    Simba Code:
    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?

    Simba Code:
    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?

  8. #8
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Quote Originally Posted by Officer Barbrady View Post
    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.

    Simba Code:
    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 @test1...
    Working on: Tithe Farmer

  9. #9
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default

    Quote Originally Posted by masterBB View Post
    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.

    Simba Code:
    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 @test1...
    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;

Thread Information

Users Browsing this Thread

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

Posting Permissions

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