PDA

View Full Version : Creating an object or type in memory?



learner
29-01-2005, 10:00 AM
I hope someone can help me? I have a noobie sort of problem that I just can't work out.

If I have an enemy type that I created, say TEnemy and I want to be able to create them one by one, how can I do it using a TList to track them? I think what I mean is can I just create a TEnemy on the fly in memory that remains there (without just using a variable to hold it), and can be referenced by pointer using a TList? I have used arrays of TEnemy so far, but an array just doesnt seem the right way to do it.

I want to create a TEnemy in memory, point to it using the TList (easy), but how can I just create an object that will occupy its own memory somewhere?

I think part of my problem is not fully understanding classes, types and objects :x

{MSX}
29-01-2005, 10:28 AM
I think part of my problem is not fully understanding classes, types and objects :x

I think this too :P You should check some tutorials about it.

Anyway, you can do exacly what you asked. Use a TList to hold all enemy without having a variable for each.

Remember that an object is a pointer. So when you reassign an object variable, the old object is still there.
For example:

var e:TEnemy;
list:TList;
begin
list:=TList.Create;

e:=TEnemy.create;
list.add(e); // now the object is referenced by the list

e:=TEnemy.create; // now i reassign the "e" variable
list.add(e); // now there are 2 objects on the list

etc. etc.


Now, you have the list full of enemy. So now you can do:

for i:=0 to list.count-1 do
begin
e:=TEnemy(list[i]);
.....
end;


In this way you can access all previously created object.

IHTH

HairyFotr
29-01-2005, 10:43 AM
I also don't know much about objects, classes and stuff like that...
In my game i went for a dynamic array of record.

Dont have the code here right now, but something like:

RCreature = record
X, Y: Real;
Speed: Integer;
.... and a whole bunch of variables
end;

Creature: Array of RCreature;


Is this a good way?

{MSX}
29-01-2005, 11:13 AM
I also don't know much about objects, classes and stuff like that...
In my game i went for a dynamic array of record.


Umm i think the dynamic array are slower when you resize them (add or remove elements).

Also, objects are better then records :)

Paulius
29-01-2005, 11:50 AM
Umm i think the dynamic array are slower when you resize them (add or remove elements).
A TList itself just encapsulates a dynamic array of pointers, with automatic resizing in smart steps.

Also, objects are better then records
Better is a relative concept, there?¢_Ts nothing you can do in OOP which you couldn?¢_Tt do without it. The most useful OOP features like virtual methods are slow compared to simple procedures, but when properly designed OOP code is much more readable, extendable and reusable.

{MSX}
29-01-2005, 12:44 PM
Umm i think the dynamic array are slower when you resize them (add or remove elements).
A TList itself just encapsulates a dynamic array of pointers, with automatic resizing in smart steps.

In fact, the smart sizing make them better :P




Also, objects are better then records
Better is a relative concept, there?¢_Ts nothing you can do in OOP which you couldn?¢_Tt do without it. The most useful OOP features like virtual methods are slow compared to simple procedures, but when properly designed OOP code is much more readable, extendable and reusable.

You're right, i mean that they're better all considered :)

HairyFotr
29-01-2005, 02:19 PM
So if I have a group, that doesent change its size often, its ok to use dynamic arrays.
Well, in my game the particle system would probaly work a lot faster with TList. :)

But if TList is some kind of a layer over dynamic arrays, doesen't this mean, TLists use more memory than a dynamic array of the same size, and maybe slower access to values inside the group?

Paulius
29-01-2005, 05:36 PM
The TList object itself will take up some memory, but the size of the data it holds will be the same. The access will be slower by a very tiny bit just for call overhead, but it is very unlikelly to become a bottleneck.

Almindor
30-01-2005, 10:22 AM
if you use objects, use TObjectList (uses contnrs;)
It's a TList derivative which is ment to hold objects. The good thing is it can take care of your memory.
EG:

type TEnemy = class;

var a: TObjectList;
i: longint;
begin
a:=TObjectList.Create(true); // true because we want it to own
for i:=0 to 10 do
a.add(Tenemy.Create);
a.free; // all objects in the list get freed too
end.