Results 1 to 8 of 8

Thread: Problem with 'FindComponent'

  1. #1

    Problem with 'FindComponent'

    Hi folks.

    I am having a little problem with 'FindComponent' in Lazarus.
    Here is my code:

    The Code with FindComponent:
    Code:
    TPlayer(FindComponent('Player' + IntToStr(Player))).
    TPlayer:
    Code:
    TPlayer = class(TObject)
        public
          Ressourcen: TRessourcen;
          MainBuilding: TPoint;
      end;
    TRessourcen:
    Code:
    TRessourcen = class(TObject)
        public
          //Nahrung.
          Korn: integer;
          //Ressourcen.
          Holz: integer;
      end;
    When i use this code, i get an aces violation. I use Lazarus v. 0.9.28.2 beta and windows XP. And while compiling i get the warning "unit1.pas(425,15) Warning: Class types "TComponent" and "TPlayer" are not related".

    I think it my be because TObject isnt a Component, so is there some similar procedure like FindComponent for Objects

    Thnx for any help.
    Last edited by T-Bear; 16-06-2011 at 04:47 PM. Reason: Just adding some info

  2. #2
    PGD Staff / News Reporter Stoney's Avatar
    Join Date
    Jan 2005
    Location
    Germany
    Posts
    316
    Blog Entries
    4
    What kinda worries me here that you don't have or don't seem to use a constructor and a destructor for your classes. Espacially in TPlayer you should initialize Ressourcen and destroy the object in your destructor. You would get an access violation here if you try to access Ressourcen as the object has not been created yet.

    In the case though I would recommend to use a record for TRessourcen instead of a class if that is your complete code and you don't plan on extending that class with some methods and/or properties.
    Freeze Development | Elysion Game Framework | Twitter: @Stoney_FD
    Check out my new book: Irrlicht 1.7.1 Realtime 3D Engine Beginner's Guide (It's C++ flavored though)

    Programmer: A device for converting coffein into software.

  3. #3
    Everything works fine without FindComponent... If I just use Player1.(...) or Player2.(...) it works. The problem (i think) is with the procedure.
    And BTW: I have edited my first post. With some more info.

  4. #4
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,535
    I think that FindComponent only works with a container class like a form/panel etc. As you dont seem to have one you probably get the Access Violation as there is no form to find the component on.

    As the TPlayer object inherits from TObject it is not a component and would therefore not be found anyway.

    I would suggest creating a StringList and allocating each TPlayer to your own Stringlist using the name of the Player as the index.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently inactive)
    MyOnline Games: http://TheGameDeveloper.co.za

  5. #5
    I think you have to use it like
    player.FindComponent(...)

    not

    TPlayer(FindComponent(...))

    That means TPlayer = class(TComponent)
    and you need same root owner TComponent for all your searches so bare that in mind with the TPlayer constructor.

  6. #6

  7. #7
    Quote Originally Posted by T-Bear View Post
    When i use this code, i get an aces violation. I use Lazarus v. 0.9.28.2 beta and windows XP. And while compiling i get the warning "unit1.pas(425,15) Warning: Class types "TComponent" and "TPlayer" are not related".
    It seems that the warning is telling you that TPlayer needs to be inherited from TComponent instead of TObject for you to be able to use that function.

  8. #8
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,535
    Type
    TPlayer = Class
    Name : String;
    End;

    Type
    TPlayerList = Class(TList)
    Procedure AddPlayer(Player: TPlayer);
    Function GetPlayer(Name : String) : TPlayer;
    End;

    Procedure TPlayerList.AddPlayer(Player: TPlayer);
    Begin
    Add(Player); // Add to the list
    End;
    Function TPlayerList.GetPlayer(Name : String) : TPlayer;
    Var
    I : Integer; P : TPlayer;
    Begin
    For I := 0 to Count-1 do
    Begin
    P := Tplayer(Self[I]);
    If P.Name = Name then
    Begin
    Result := P;
    Exit;
    End;
    End;
    Exception.Create('Player with name '+Name+' not found');
    End;
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently inactive)
    MyOnline Games: http://TheGameDeveloper.co.za

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
http://flippulseimages.com/Food/dracula-2000-movie.html