PDA

View Full Version : Problem with 'FindComponent'



T-Bear
16-06-2011, 03:41 PM
Hi folks. ;D

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

The Code with FindComponent:


TPlayer(FindComponent('Player' + IntToStr(Player))).
TPlayer:


TPlayer = class(TObject)
public
Ressourcen: TRessourcen;
MainBuilding: TPoint;
end;
TRessourcen:


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.

Stoney
16-06-2011, 04:48 PM
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.

T-Bear
16-06-2011, 05:00 PM
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.

cairnswm
16-06-2011, 05:09 PM
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.

User137
17-06-2011, 12:12 AM
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.

azrael11
18-06-2011, 08:35 AM
Take a look in this thread ...

http://www.pascalgamedevelopment.com/showthread.php?6935-How-can-i-do-this

LP
18-06-2011, 03:24 PM
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.

cairnswm
18-06-2011, 05:01 PM
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;