Jump to page: 1 2 3
Thread overview
Pointers or copies?
Dec 20, 2006
Orgoton
Dec 20, 2006
Orgoton
Dec 20, 2006
Orgoton
Dec 20, 2006
BCS
Dec 20, 2006
BCS
Dec 20, 2006
Tom S
Dec 20, 2006
Frits van Bommel
Dec 20, 2006
Frits van Bommel
Dec 20, 2006
BCS
Dec 22, 2006
Tim Keating
Dec 22, 2006
Hasan Aljudy
Dec 21, 2006
Hasan Aljudy
Dec 21, 2006
Frits van Bommel
Dec 22, 2006
Frits van Bommel
December 20, 2006
I'm making a game and I have to keep track of the objects in the game, which inherit from a master object class called "objct". So I would make a class for "baddie", "player" and such. Now, to draw them I have to keep track of their existance, so, when one is created, it adds itself to the "frame queue" and removes itself when it is deleted. To keep track of them I created a "queue" class, which has a protected member called "objct *pters[]". It is an array of pointers so when I recieve a new actor for the queue, I increase the array size accordingly and on the next slot, just add it there. Simple. Now, the queue class has a method called "add" and "remove" both of them take as parameter "objct *target".

To sum it up and finally tell you my question the objct has in it's
constructor "framequeue.add(this);" and a correspondind "remove(this)".

Apparently, "this" is not a pointer... as the compiler claims: "function queue.remoce (objct *target) does not match parameter types (objct)" and that "cannot implicitly convert expression (this) of type objct to objct*". So, does passing "this" create a copy of the object? How do I pass the pointer to the object to the queue? Any other solution?
December 20, 2006
Orgoton wrote:
> I'm making a game and I have to keep track of the objects in the game, which
> inherit from a master object class called "objct". So I would make a class for
> "baddie", "player" and such. Now, to draw them I have to keep track of their
> existance, so, when one is created, it adds itself to the "frame queue" and
> removes itself when it is deleted. To keep track of them I created a "queue"
> class, which has a protected member called "objct *pters[]". It is an array of
> pointers so when I recieve a new actor for the queue, I increase the array
> size accordingly and on the next slot, just add it there. Simple. Now, the
> queue class has a method called "add" and "remove" both of them take as
> parameter "objct *target".
> 
> To sum it up and finally tell you my question the objct has in it's
> constructor "framequeue.add(this);" and a correspondind "remove(this)".
> 
> Apparently, "this" is not a pointer... as the compiler claims: "function
> queue.remoce (objct *target) does not match parameter types (objct)" and that
> "cannot implicitly convert expression (this) of type objct to objct*". So,
> does passing "this" create a copy of the object? How do I pass the pointer to
> the object to the queue? Any other solution?

Unless I am missing something, you shouldn't need to use pointers at all.  Object variables in D are referances to the actual object which lives off in space somewhere. (Where space happens to be the heap.)  I'm also curious as to how this Queue class of yours is designed.  It sounds like there ought to be an easier means.

-- Chris Nicholson-Sauls
December 20, 2006
== Quote from Orgoton (orgoton@mindless.com)'s article
> I'm making a game and I have to keep track of the objects in the game, which
> inherit from a master object class called "objct". So I would make a class for
> "baddie", "player" and such. Now, to draw them I have to keep track of their
> existance, so, when one is created, it adds itself to the "frame queue" and
> removes itself when it is deleted. To keep track of them I created a "queue"
> class, which has a protected member called "objct *pters[]". It is an array of
> pointers so when I recieve a new actor for the queue, I increase the array
> size accordingly and on the next slot, just add it there. Simple. Now, the
> queue class has a method called "add" and "remove" both of them take as
> parameter "objct *target".
> To sum it up and finally tell you my question the objct has in it's
> constructor "framequeue.add(this);" and a correspondind "remove(this)".
> Apparently, "this" is not a pointer... as the compiler claims: "function
> queue.remoce (objct *target) does not match parameter types (objct)" and that
> "cannot implicitly convert expression (this) of type objct to objct*". So,
> does passing "this" create a copy of the object? How do I pass the pointer to
> the object to the queue? Any other solution?

RTM: use operator '&' to get its pointer.
e.g. framequeue.add(&this);

Anyway, I recommend you to use all objects by reference, no pointers at all.
e.g.:
object[] framequeue;
framequeue ~= this;
framequeue ~= another_object;

December 20, 2006
I haven't finished it yet. Well, each frame, the main engine will cycle through all the pointers on the queue with something like

for (i=0; i<queue.size; i++) queue.next().draw();

Or, to save up the callings

queue.drawall()

where all pointers would be called.

I'm used to C++, sorry for the question about pointers and copies. That means I can just make something like

objct objts[];

and use it just like a pointer...

void add (in objct target)
{ objts[objts.lenght-1]=target; }

(on objct)
this
{queue.add(this);}

and it'l work just fine without creating copies of the objects? (and spare me of memory problems along the way :P)
December 20, 2006
== Quote from Orgoton (orgoton@mindless.com)'s article
> void add (in objct target)
> { objts[objts.lenght-1]=target; }

use operator '~=' to 'push_back' objects to an dynamic array :) e.g.:

void add (in objct target)
{ objts ~= target; }
December 20, 2006
Orgoton wrote:
> I haven't finished it yet. Well, each frame, the main engine will cycle through
> all the pointers on the queue with something like
> 
> for (i=0; i<queue.size; i++) queue.next().draw();
> 
> Or, to save up the callings
> 
> queue.drawall()
> 
> where all pointers would be called.
> 
> I'm used to C++, sorry for the question about pointers and copies. That means I
> can just make something like
> 
> objct objts[];
> 
> and use it just like a pointer...
> 
> void add (in objct target)
> { objts[objts.lenght-1]=target; }
> 
> (on objct)
> this
> {queue.add(this);}
> 
> and it'l work just fine without creating copies of the objects? (and spare me of
> memory problems along the way :P)

That's right.  You could also use array concatenation (objts ~= target;) if you aren't wanting to enforce a particular size.  And since you plan to use this in a loop, it might be worth taking a look at foreach.

-- Chris Nicholson-Sauls
December 20, 2006
Orgoton wrote:
> I haven't finished it yet. Well, each frame, the main engine will cycle through
> all the pointers on the queue with something like
> 
> for (i=0; i<queue.size; i++) queue.next().draw();

Take a look at foreach. If queue is an array, this will work:

foreach(item;queue) item.draw();

If it isn't an array, you can implement an opApply.

> 
> That means I can just [...]
> 
> objct objts[];
> 
> void add (in objct target)
> { objts[objts.lenght-1]=target; }
> 
> and it'l work just fine without creating copies of the objects? (and spare me of
> memory problems along the way :P)

yup.

BTW this is the same thing

objts[$-1]=target;



December 20, 2006
And when I want to remove the reference? Something like

void remove(in object target)
the_for:
for (i=0; i<queue.length; i++)
{
if target==queue[i]
{
queue[i]=queue[$-1];
queue.length-=1;
break the_for;
}
}

the compare will work fine, right? I mean, in C++ it would just compare a 4 byte integer, how much data will D compare? hopefully, not he full object data ... ... ...
December 20, 2006
I haven't used it but IIRC cashew has much of what you want.

http://www.dsource.org/projects/cashew
December 20, 2006
Orgoton wrote:
> And when I want to remove the reference? Something like
> 
> void remove(in object target)
> the_for:
> for (i=0; i<queue.length; i++)
> {
> if target==queue[i]
> {
> queue[i]=queue[$-1];
> queue.length-=1;
> break the_for;
> }
> }
> 
> the compare will work fine, right? I mean, in C++ it would just compare a 4 byte
> integer, how much data will D compare? hopefully, not he full object data ... ... ...

Use 'is' instead of '=='. It will compare references.
« First   ‹ Prev
1 2 3