• 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
cozzie

Passing and getting const refs (XMFLOATxxx)

4 posts in this topic

Hi,

 

Lately I've been struggling a bit/ playing around with when to pass a const ref and when to pass by value, concercing XMFLOAT3/4's and XMFLOAT4X4 types. The advice I've got was to prevent doing it all by const ref, because the mentioned objects are quite 'small'. So I've thought of the following 'hand rule':

 

- when I retrieve a vector/ matrix I always return the class member by value

- when I have to pass a vector/ matrix to another object, I always do it as const ref

- always applying both rules should make it safe and prevent passing 'empty references'/ local adresses

 

Here's an example:

// INTERFACE

class objectTypeA
{
public:
	DirectX::XMFLOAT4 GetPosition() const;

private:
	DirectX::XMFLOAT4 mPositon;
};

// IMPLEMENTATION

XMFLOAT4 objectTypeA::GetPosition() const
{
	return mPosition;
}


// INTERFACE 2

class objectTypeB
{
public:
	void AdaptPosition(const DirectX::XMFLOAT4 &pPos);

private:
	DirectX::XMFLOAT4 mPosition;
};

// IMPLEMENTATION

void objectTypeB::AdaptPosition(const XMFLOAT4 &pPos)
{
	mPosition = pPos;
}


// CALLING THE FUNCTIONS

objectB.AdaptPosition(objectA.GetPosition());

?

?

?

?

When I have a mix of both, I get into situations where I always first have to create a local (scope) const variable, before I can pass it safe to another function that expects a const ref. Which I find makes my code not quite readable, here's an example:

// INTERFACE

class objectTypeA
{
public:
	DirectX::XMFLOAT4& GetPosition() const;

private:
	DirectX::XMFLOAT4 mPositon;
};

// IMPLEMENTATION

XMFLOAT4& objectTypeA::GetPosition() const
{
	return mPosition;
}


// INTERFACE 2

class objectTypeB
{
public:
	void AdaptPosition(const DirectX::XMFLOAT4 &pPos);

private:
	DirectX::XMFLOAT4 mPosition;
};

// IMPLEMENTATION

void objectTypeB::AdaptPosition(const XMFLOAT4 &pPos)
{
	mPosition = pPos;
}


// CALLING THE FUNCTIONS

const XMFLOAT4 localVec = objectA.GetPosition();
objectB.AdaptPosition(localVec);

I would appreciate your opinions and advice if this would be a good rule of thumb (or how you handle/ approach this).

It's basically the same for other relative small objects, like a XMFLOAT4X4 or a AABB/ OBB.

Edited by cozzie
0

Share this post


Link to post
Share on other sites

To be perfectly honest, I would design your return types around the access patterns that you want to provide to your users, rather than trying to determine if returning by reference is the more performance oriented technique.  I would find it hard to believe that returning by const reference vs making a duplicate of the object and returning it on the stack is either better or worse in all situations...

 

Have you tried to profile either of these cases to see if there is an appreciable difference?  Even if it is just a test case (i.e. not the actual use case in your engine) I think that will show that there isn't much difference between the two.

0

Share this post


Link to post
Share on other sites
Thanks. I'm actually not worrying about performance (anymore), and for now conclude that vectors and matrices aren't that big/ I don't experience performance issues. By having some rule of thumb I want to prevent that I'm passing local adresses causing difficult to track bugs.

Another option would be to always pass/ return by value, but I was just wondering why not retrieve all by value and pass by reference, which should be safe (and prevent unneccessary copying even though I don't have performance issues).
0

Share this post


Link to post
Share on other sites

Have you tried out using a static code analyzer?  There is one available in Visual Studio, which can most likely catch it if you try to return a reference to a temporary object.  Other than using something like this to check your code, then you pretty much just have to make sure you don't return a reference unless the data is a member or (gasp) a global variable.

 

The arguments should be passed by constant reference if you don't plan to modify them.  Like you said, the return value should be a value and not a reference, unless you intend for the returned data to be modified outside of the class.

1

Share this post


Link to post
Share on other sites

Thanks. I believe I'm doing it now exactly as you're saying (gladly :))

 

- I do const return by valye only for members of my classes.

For example: XMFLOAT3 Cd3dcamera::GetPos() const { return mPos; }, where mPos is a class member

- I absolutely don't want to modify the class members from outside the class (especially because most of them are private), so that's a confirmation I should return by const ref, like: XMFLOAT& CD3dcamera::GetPos() const.

I'll look into the feature in Visual Studio, the last time I had an issue with this, I actually got a compiler error telling me that I was returning a local address.

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