• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
reaperrar

Two-Phase construction...

9 posts in this topic

I'm starting my first big project and I want to stick to a strict coding standard. It is probable in the future other people will be working with my code.

 

I'm a fan of Two-Phase construction(/deconstruction) yet most online sources I've found advise against it. The general pros and cons I've discovered are:

 

Two-Phase:

Pros:

  • Can control initialization and cleanup of object outside allocating space for the object. So if I wanted an array of objects that initialize with certain variables it doesn't get annoying if objects don't have default constructors.
  • Can have a return result for success or failure. Little simpler than throwing an exception & catching it outside object construction.
  • Can call virtual functions within initialization.

Cons:

  • Base class initialize functions not hidden. They can be protected though, it just appears a little less elegant I guess.
  • Less inituitive for other programmers. I'm not sure I 100% agree with this one as if you have a strict coding standard where all your objects need to be initialized manually after construction then it shouldn't be too difficult to get used to.
  • Compilers already enforce order of construction/deconstruction when using constructors/destructors so it is less error prone and re-inventing the wheel, so to speak, may be unneccassary.

Single-Phase:

Pros:

  • The opposite of all two-phase cons.
  • Less invariants? Not 100% clear on this.

Cons:

  • The opposite of all two-phase pros.

So given these points so far I'm still leading to two-phase yet can't bring myself to do it since so many people oppose >< Curious to see what other people on this forum think.

0

Share this post


Link to post
Share on other sites

The answer depends on whether you're thinking of using it EVERWHERE in your code/engine as a rule, or just in certain places or objects.  Generally, trying to use it everywhere sounds like a bad idea.  But, if for example you're thinking of just using it for game objects (stuff that needs to be initialized with data loaded from a file, etc), then that would make more sense.  Maybe not all game objects might need it, but if generally they do, and you prefer that pattern of usage, then that makes sense.  This also makes it nice and easy for your future work, or other devs working with the code later.  The rule would be that the norm is single construct/destruct, unless for game objects, in which case they all go through a 2-stage construct + init process.

 

My philosophy for architectural things like this is that I try to think of all the consequences of the decision, in all cases, in relation to my initial objectives.  If it leads me to any odd or awkward situations, then that's a big red flag that there's something wrong with the design.  If it all works together smoothly and accomplishes my objectives, then that's probably the way to go.  This also applies for when I'm implementing code later, after the architecting phase is over.  If I'm writing code where I'm "working around" stuff, or hacking to allow some code to work with other code... it usually means I'm violating some of my own initial design, or the design was flawed to begin with.  But, usually it's that I'm violating the design.

1

Share this post


Link to post
Share on other sites

I'm a little confused about how could go about doing this...

 

class MyClass
{
	public:
		MyClass(int i) : m_i(i);
		~MyClass();
		
	private:
		int m_i;
}

int main()
{
	const unsigned int uiArraySize = 1000;
	int iInitializeValue = 5;
	
	MyClass oClass[uiArraySize] = {iInitializeValue, iInitializeValue, iInitializeValue...};

	return 0;
}

If I want all instances of the class to intialize with value "iInitializeValue" is there some automatic way this can be done instead of copy paste 1000 times xD?

 

EDIT: ...Without using anything other than syntax? I don't want to be forced to use something in std::

EDIT: Also, I assume acheiving the same thing is out of the question with the new operator when creating an array, though it would then seem more appropriate to use the std::vector.... unless the object provided to be copied from is non-copyable. /sigh

Edited by reaperrar
0

Share this post


Link to post
Share on other sites
You should really do it using std::vector. If you are allergic to std::, you are programming in the wrong language, but you can make an array of char of the correct size, somehow enforce proper alignment, and then use placement new.
0

Share this post


Link to post
Share on other sites
If I want all instances of the class to intialize with value "iInitializeValue" is there some automatic way this can be done instead of copy paste 1000 times xD?

Instead of an argument to the ctor set a static member and initialize the value from that?

Why are you not wanting to use a vector? Edited by Khatharr
0

Share this post


Link to post
Share on other sites
Why are you not wanting to use a vector?

 

If possible, I'd like memory required by the object to be local to the object rather than off in some random place. Plus I'm under the impression the vector will copy the object into each instance (as opposed to call each instance's constructer with the value individually), so if the object is not meant to be copied that could be a problem down the track I'm thinking.

 

I guess I'm just seeing if there are alternatives to the vector though it appears there isn't without two-phase construction.

Edited by reaperrar
0

Share this post


Link to post
Share on other sites
No offence or unfair stereotyping intended, but the only code-bases I've used that were based around two-phase construction, were projects where the majority of staff weren't very good at C++, but were decent at C.

The largest project I've ever worked on took 1min to compile lol. It's hard to grasp the reasons for avoiding T-PC because of this I beleive. I haven't much real c++ experience though I realised my design was wrong because of the research I did into it. Came here to the begginers area for convincing and your post was most helpful, ty.

 

I have used placement new before though didn't make the connection that the allocation/construction happened in a identical way to my two-phase approach. I was using a pool, allocating space for the object and calling initialise when a new object was requested.

 

I'll avoid using two-phase construction flippantly in my design.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0