Results 1 to 3 of 3

Thread: Split up code into functions

  1. #1
    Join Date
    Jun 2009
    Location
    The Netherlands
    Posts
    79
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default Split up code into functions

    Hey again ,

    My barbarian outpost runner is coming along pretty well so far, now passing the first tree obstacles without any problems .
    My problem is that I now have three procedures that actually do the same, but turning it into a function will result in a huge amount of arguments...

    My question: How (where) could I efficiently split up this code into functions so that I can use for other obstacles without duplicate code? I attached the script .

    PS: I'm not asking anyone to completely rewrite the whole script, I can do that .
    Last edited by The Little; 06-24-2009 at 08:46 PM. Reason: Typo

  2. #2
    Join Date
    Mar 2007
    Posts
    3,042
    Mentioned
    1 Post(s)
    Quoted
    14 Post(s)

    Default

    Well, you could try making a function that encompasses what those three other functions do by making a lot of the information unique to each function a parameter. You would want to add things like the position of the object, ending position, object ID(s), uptext, color, tolerance, coordinates, and whatever else you might need.

    Also, when you call something like "FindObjectEx(NetPoint, NetID, 5)", NetPoint is intended to have the location of the tile after FindObjectEx is completed. It looks like you set it earlier, and if the object isn't there, it will be reset to -1, -1. If you don't want this to happen, I suggest using a temporary variable to solve this issue.
    :-)

  3. #3
    Join Date
    Jun 2009
    Location
    The Netherlands
    Posts
    79
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Thanks Method , I'll start on it now :P

    Never knew about FindObjectEx resetting the value! It does explain some weird bugs I had though

    EDIT 3: How does this look:
    Code:
    function OwnObstacleR (ObjectTile, FrontTile : TPoint; UpText, Option : string; ObjectID, Height, Length : Integer): boolean;
    begin
      MyPosition := GetMyPos;
    
      if not ((MyPosition.x = FrontTile.x) and (MyPosition.y = FrontTile.y)) then
      begin
        Result := False;
        WriteLn('ERROR: REFLECTION: Not in position!');
        Exit;
      end;
      if not FindObjectEx(ObjectTile, ObjectID, Length) then
      begin
        Result := False;
        WriteLn('ERROR: REFLECTION: Object not found!');
        Exit;
      end;
    
      MSPoint := TileToMS(ObjectTile, Height);
      MMouse(MSPoint.x, MSPoint.y, 5, 5);
    
      Wait(100+random(50));
    
      if IsUpText(UpText) then
      begin
        GetMousePos(MSPoint.x, MSPoint.y);
        Mouse(MSPoint.x, MSPoint.y, 0, 0, False);
        R_ChooseOption(Option);
        Result := True;
      end else
      begin
        Result := False
        WriteLn('ERROR: REFLECTION: UpText not found!');
        Exit;
      end;
    end;
    Wasn't nearly as hard as I thought :P
    Last edited by The Little; 06-24-2009 at 11:33 PM. Reason: See edit 3

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
  •