Originally Posted by
T-Bear
I found out that it was me doing something wrong, and it works now. Still thanks for your help.
So i can do something like this:
var
Object: TObject;
ObjectPtr: TObject;
begin
Object:= TObject.Create;
ObjectPtr:= @Object;
end.
Im just wondering caus i need that kind of functionality, which is why i am using pointers in the first place.
What kind of functionality are you talking about? What are you trying to achieve?
If you use your Objects to store data for some ingame entities and you have fixed number of them you can simply use:
Code:
var Entity1: TMyObject;
Entity2: TMyObject;
.....
Entity1 := TMyObject.Create;
Entity2 := TMyObject.Create;
...
But if you have dynamic number of entities in your game go and use TObjectList to store them like so:
Code:
var Entity: TMyObject;
Entities: TObjectList;
//If you use TObjectList.Create(True) ObjectList then Owns the objects you added to it.
//This means that when you will remove one of such objects from ObjectList it will automaticly
//call objects destructor method and thus destroy the object. This will hapen even when you call
//ObjectList.Free to destroy the object list. So it can be useful for quickly destroying all ingame entities.
Entities := TObjectList.Create(False);
for N := 0 to 99 do
begin
Entity := TMyObject.Create;
Entities.Add(Entity);
end;
So basically ObjectList contains pointer to all the object you added to it and you can acces them with:
Code:
//TMyObject is used to tell program which calss structure are you planning to use
//Entities[I] returns you Pointer to the object which is stored in ObjectList at index I
TMyObject(Entities[I]).SomeValue
Even better you could make deriverate class of TObjectList so that it works with TMyObject class insted of TObject one. This will alow you to acces objects stored in TObject list by simply caling: Entities[I].SomeValue.
NOTE: The code exapmles were writen directly from my head so there might be some Typos in it.
Anywhay if you give me more information of what are you trying to do I can write you actual code that you can use.
Bookmarks