Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

neonfaktory

Cancelling a Constructor?

This topic is 5489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering if it is possible to ''cancel'' a constructor from inside after its been called. To clarify, or if you''re curious, I have particles in my game that get culled based on certain aspects. Some of these particles fill these "cull" aspects right from the start, so they are added, only to get culled with the culling function next game tick. Waste of resources and cycles, it seems. Now, one would suggest that since I have to have coordinates to call the constructor with, I can just run the checks manually before calling it and cancel the constructor before it even starts. Well, I may end up doing that, but it''s called from so many different places it would be a pain to find and add that to all of them. Expecially compared to if I could cancel the constructor. So, if anyone could help me on this, that''d be great .

Share this post


Link to post
Share on other sites
Advertisement

class Sex
{
Sex( double x, double y, bool construct = true )
{
if( construct )
{
this->x = x;
this->y = y;
}
}
};

Share this post


Link to post
Share on other sites
As another note, you shouldn't be constructing and deleting your particles. Set a flag in the particle class isAlive, if its true then when your particle engine is processing particles it just immediately skips that particle.

One easy way is to store your particles as an array ( since you generally know the max number of particles you will ever have, and you can insure no memory fragmentation of an array easily.. as compared to a dynamically sized linked list which is what I assume you're currently using ). Now, create a stack which you will use for insertion into the array. The stack will contain numbers of offsets into the array which aren't already used up. So each time you want to 'create' a particle you would do something like this:

particleList[ particleOffsets.pop() ].Instantiate( xlocation, ylocation, color, etc )

And when you wanted to 'delete' a particle you would simply do:

particleList[ 123 ].isAlive = false;
particleOffsets.push( 123 );

This is much more logical and efficient than continually allocating/deleting.


[edited by - haro on July 7, 2003 4:31:32 PM]

Share this post


Link to post
Share on other sites
Haha, I knew someone would say something about the Particle method. But in all honesty, I don't know the max number of particles I could have. The Particle system handles the "pretty" particles, as well as the weapon particles, so it all depends on what is happening and who is shooting, etc. It would kinda suck if too many people were shooting and suddenly you couldnt anymore because the max particles were reached. I *do* like the method you suggested, though, only if I knew the max particle #.

About your constructor cancelling code, I have a few concerns.
First, I had mentioned I would rather not have to run the checks before the constructor, because that would be a huge pain to go through every piece of code that generates particles and add that.
Second, it seems kind of pointless because if you already know whether you are going to construct it or not (you have to determine "construct" bool before hand to send as a param), why would you call the constructor in the first place if you already tested whether or not you want to construct the object?
And finally, it seems as if the sex object is still created (not what I want), you just didn't copy over the parameters to the "sex" data members.

Perhaps I'm missing something? Oh, and don't take this wrong or anything - I appreciate any feedback and help I can get, thanks for the response .

[edited by - neonfaktory on July 8, 2003 12:29:14 PM]

Share this post


Link to post
Share on other sites
If you could cancel your constructor, what would happen to the variable you just created.

Particle p(x,y); // Ooooops we canceled the constructor
Particle* p = new Particle(x,y); // Idem


Throwing an exception would act as a ''cancel'', but you would have to add exception-handling code ... which doesn''t seem to be what you want.

One thing you *can* do though, is to write a function that does the test and returns a Particle* or 0 if culled, but then you would have to deal with null pointers ...




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
i think this is legal:


class Arrrrrg
{
Arg()
{
if( something )
{
delete this;
return;
}
}
}


although you would be better off creating one small function, such as "bool CreateParticle( data, data, data )" which has code that checks to see if the data is valid, and if it is, THEN initalizes the particle. All of the places in your code that create particles call this function instead of being in charge of creating the particles themselves. Its good coding practice to force a standard method on yourself because it will make sure that you always create them correctly.

[edited by - Mulligan on July 8, 2003 12:50:49 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Mulligan
i think this is legal:



ha ha hah ho hee hee hee ha

good one.

what? you''re really serious? oh boy...

Share this post


Link to post
Share on other sites
As I guess you're interested in an answer to your question and not an alternative method - here is the closest I think you'll get.

(as I don't think throwing an exception is really valid for this scenario - the object isn't broken it just shouldn't have been constructed in the first place - you also face an unnecessary overhead as the object has already been partially constructed also exceptions have an overhead too).

Have a static factory method in your class that returns particles - or NULL. That way your culling condition is in one place and the instance is never created if it is already culled.

eg.

class Particle
{
public:

Particle* CreateParticle(float x, float y, float z, otherstuff..)
{
if(visible(x,y,z))
return new Particle(x,y,z, ...);

return NULL;
}

protected:
Particle(){};
};


[edited by - stubble on July 8, 2003 1:04:02 PM]

[edited by - stubble on July 8, 2003 1:18:00 PM]

Share this post


Link to post
Share on other sites
AFAIk the only way to "Cancel" a constructor is to throw an exception, as someone stated before.

Exception handling can make your life easier if you use it correctly (I''m not claiming I actually am using it the right way, I''m a n00b).

Particle:article()
{
initparticle();
if (cullparticle()) {
throw;
}
}

.. later on ..

Particle *p;
try {
p = new Particle();
particle_list->add(p); // won''t be reached if Particle:article throws
} catch ( ... ) { // not sure if this is correct
// well... no particle added... no harm done
}

This method is probably slow though.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
quote:
Original post by Mulligan
i think this is legal:



ha ha hah ho hee hee hee ha

good one.

what? you''re really serious? oh boy...

Another half-witted comment from a half-witted AP. "delete this" is completely legal syntax (though not what is required here).


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!