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

Varidic templates as additional parameters

6 posts in this topic

Hello,

 

I wonder if it was at all possible to use the c++11 feature of variadic template to act as an optional paramters, aside from some fixed ones? I'm trying to abstract the process of creating a new game state for my state system. All of my main game states (main menu, editor, game) have four fixed objects they need, plus some optional parameters:

 

        class BaseState :
            public IState
        {
        public:
            BaseState(gfx::IGfx3D& gfx3D, ecs::EntityManager& entityManager, ecs::SystemManager& systemManager, ecs::MessageManager& messageManager);
            ~BaseState(void);

        protected:

            template<typename S, typename... Args>
            void NewState(Args&&... args);

            gfx::IGfx3D* m_pGfx3D;
            ecs::EntityManager* m_pEntities;
            ecs::SystemManager* m_pSystems;
            ecs::MessageManager* m_pMessages;

            IState* m_pCurrentState;
        };

        template<typename S, typename... Args>
        void BaseState::NewState(Args&&... args)
        {
            m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages, args...);
        }

Not talking about how I could probably pack the four variables into one to make the constructor less ugly... calling it that way:

 

void MainMenuState::OnNewGame(void)
{
	NewState<GameState>();
}

results in the following error:

 

1>C:\Acclimate Engine\Repo\Editor\Core/BaseState.h(44): error C2661: 'GameState::GameState' : no overloaded function takes 5 arguments
1>          MainMenuState.cpp(26) : see reference to function template instantiation 'void acl::core::BaseState::NewState<GameState,>(void)' being compiled

Obviously, it still takes the variadic template as a fifth parameter, even though normally varidic templates "extinct" if I don't submit any in the call. Does somebody know how to solve that?

 

0

Share this post


Link to post
Share on other sites
You need to handle the case where there are no variatic arguements seperately. If that's not your problem, post you GameState constructors.
1

Share this post


Link to post
Share on other sites

Ah, of course, how could I forget that. Its getting late again :/ Did try that though, but it gave me an ambigous symbol error. I forgot to add the one seperate template:

 

		template<typename S>
		void BaseState::NewState(void)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages);
		}

		template<typename S, typename Arg1, typename... Args>
		void BaseState::NewState(Arg1 arg, Args&&... args)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages, arg, args...);
		}
0

Share this post


Link to post
Share on other sites

Ah, of course, how could I forget that. Its getting late again :/ Did try that though, but it gave me an ambigous symbol error. I forgot to add the one seperate template:

 

		template<typename S>
		void BaseState::NewState(void)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages);
		}

		template<typename S, typename Arg1, typename... Args>
		void BaseState::NewState(Arg1 arg, Args&&... args)
		{
			m_pCurrentState = new S(*m_pGfx3D, *m_pEntities, *m_pSystems, *m_pMessages, arg, args...);
		}

 

That will be a problem for the compiler because, even though named in the second constructor: "void BaseState::NewState( Arg1 arg )" includes "void arg" as a valid template parameter, so it is still equivalent to "void BaseState::NewState( void )" from the first constructor.  So, both constructors can take the void argument which is ambiguous of course.  In order to correct this, Arg1 needs to be specified as being anything non-void.  So, you may have to write out multiple variations which take arg 1 of different types or give it a fixed type, or remove the first version all together and only use the second version.

Edited by AllEightUp
0

Share this post


Link to post
Share on other sites

That will be a problem for the compiler because, even though named in the second constructor: "void BaseState::NewState( Arg1 arg )" includes "void arg" as a valid template parameter, so it is still equivalent to "void BaseState::NewState( void )" from the first constructor. So, both constructors can take the void argument which is ambiguous of course. In order to correct this, Arg1 needs to be specified as being anything non-void. So, you may have to write out multiple variations which take arg 1 of different types or give it a fixed type, or remove the first version all together and only use the second version.

 

Did you try this out? Because this is the standard solution I know (which makes it even more emberassing that I didn't remember), since Arg1 cannot resolve to void, thats what makes the difference. Its like if you have a simple template function

 

template<typename A>
void foo(A a)
{
}

You can't just call foo(), can you? At least my solution above compiled for me, and as I said, thats how I did it since I've first heard about varidic templates.

0

Share this post


Link to post
Share on other sites

That will be a problem for the compiler because, even though named in the second constructor: "void BaseState::NewState( Arg1 arg )" includes "void arg" as a valid template parameter, so it is still equivalent to "void BaseState::NewState( void )" from the first constructor. So, both constructors can take the void argument which is ambiguous of course. In order to correct this, Arg1 needs to be specified as being anything non-void. So, you may have to write out multiple variations which take arg 1 of different types or give it a fixed type, or remove the first version all together and only use the second version.

 

Did you try this out? Because this is the standard solution I know (which makes it even more emberassing that I didn't remember), since Arg1 cannot resolve to void, thats what makes the difference. Its like if you have a simple template function

 

Unfortunately I missread your message and went looking for a reason it would be failing.. That's what I came up with, though looking at it today I realize the function lookup rules allow the case in terms of finding the closest match.  Oops.. :)

 

template<typename A>
void foo(A a)
{
}

You can't just call foo(), can you? At least my solution above compiled for me, and as I said, thats how I did it since I've first heard about varidic templates.

 

Yup, sorry.  It was late and viewed through beer goggles.. :)

0

Share this post


Link to post
Share on other sites

Yup, sorry. It was late and viewed through beer goggles.. smile.png

 

Don't worry, if it hadn't been late already, I shouldn't have come up with that question in the first place. We all make mistakes. Anyway, thanks for trying to help out :)

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