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

Templates and Function pointers

7 posts in this topic

Okay so for a bit of background, I need to create a list of function names and their function pointers so I can tell Lua how to use the objects I create. How I've tried to do this is with a template which uses a dynamic_cast to see if it inherits from a class, if it does it'll add the function pointers to the vector and keep checking other classes. I did it like this because as an example I have a GUID class which basically everything will inherit from, if I didn't do it like this I'd have to repeat the function pointer and it's name for every metatable and I'd have a massive list for each class.

 

My issue is I had a method of doing this, that didn't compile

template<typename T> vector<luaL_Reg> createMetatable(){
	vector<luaL_Reg> out;
	T object;
	if (dynamic_cast<Component::ActiveState*>(&object)){
		out.push_back({ "activate", &T::lActivate });
		out.push_back({ "deactivate", &T::lDeactivate });
		out.push_back({ "isActive", &T::lIsActive });
	}
	if (dynamic_cast<Component::Area*>(&object)){
		out.push_back({ "getDimensions", &T::lGetDimensions });			
                out.push_back({ "setDimensions", &T::lSetDimensions });
	}
...

does anyone have a recommendation of an alternative or a way of getting this to work, your help would be much appreciated.

 

The exact issue with this code is that at compile time let's say you pass in Camera to the template, all those if statements that wouldn't run anyway because the dynamic cast will fail, will still have the Camera::lGetDimensions and that'll break the build because it can't find that function.

0

Share this post


Link to post
Share on other sites

I don't really see why createMetatable() is a template function.

Anyway, you try to move the push_back()s into other functions (one for ActiveState*, one for Area*. etc.). That way you should be able to get rid of the compiler errors.

0

Share this post


Link to post
Share on other sites

that raises a question,

if I have a Camera which has a GUID

is it safe to get the a GUID's function pointer for Camera with GUID::get instead of Camera::get

 

that way I could replace T:: with the base class of the function

Edited by Joshhua5
0

Share this post


Link to post
Share on other sites

I recommend reading this, it also has complete source code.

 

Your method won't work, except if the l* functions have the same signature as the Lua C function typedef (which is a bit ugly and that shouldn't be needed). You could also use an existing library like LuaBridge or luawrapper.

2

Share this post


Link to post
Share on other sites

the l* functions are all following the lua_CFunction, because they're the c++ functions that lua can call, I added on the l to make that easier to understand.

0

Share this post


Link to post
Share on other sites

A lot of unneded work...

When you mimic c++ behavior in lua, then metatables created just once.

Every lua object (userdata, holding pointer to c++ object) will share the same metatable, storing class' methods.

But the fact this function exists implies it will be called many times, for every created c++ object, even if it's of the same class.

 

Sorry if it looks offtopic, but I see it as XY problem.

Edited by vstrakh
0

Share this post


Link to post
Share on other sites

I know of an alternative you might enjoy reading about. It is a bit advanced and makes use of some more tricky C++, but overall the idea is pretty simple. Luckily I wrote an article about this already, here's the link (I'll post a short summary here): http://www.randygaul.net/2014/01/01/automated-lua-binding/

 

So there's a few different ways to go about the implementation of binding objects, but there's only two ways to send things to Lua (that I know of). The first way is to send things be value, and the other is by reference. Sending things by reference means you send a pointer to some memory that C++ manages. By value means you create some sort of copy (probably with tables) of a C++ object, and the memory of the copy is managed by Lua. The former is generally easier to implement than the latter (in my opinion).

 

My favorite way to bind functions and class/struct methods is with some kind of introspection that automates the process.

 

As for metatables specifically, I just create one per class type (like you seem to have). With a simple implementation of some introspection you can detect if some class derives from another, and make use of some kind of virtual overriding without much trouble. I haven't really gone into all that simply because I just didn't think it would be necessary for my purposes.

Edited by Randy Gaul
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