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

Event handling - Lifetime management

9 posts in this topic

Been looking into introducing delegates in my code base and I've run into some lifetime problems I was hoping to get solved.
Currently I'm using fastdelegates for my dispatching.

My problem is the following:

- Object A can raise an event (It contains a delegate)
- Object B registers a method to the delegate in A.
- Object A raises an event and method gets called in B.

Now this can go wrong in two ways:

- Object B is killed and A tried to raise the event. Crash. This is easily solved by making B unregister from A when it dies.
- Object A is killed and B is killed. When B is killed it tries to unregister from A which is no longer there!

The second is giving me problems and I was hoping someone could point me to a proper solution.
0

Share this post


Link to post
Share on other sites
http://code.google.com/p/cpp-events/ has a signals&slots implementation with connection tracking.
0

Share this post


Link to post
Share on other sites
The canonical solution in C++ is to use a smart pointer to keep track of the lifetime of your objects. Be warned that this might get sticky if you need circular references. I recommend doing a thorough bit of research into shared_ptr and weak_ptr.
0

Share this post


Link to post
Share on other sites
[CODE]
struct A
{
RFDelegate1<int> m_kDelegate;
A()
{
std::cout << "A" << std::endl;
}
~A()
{
std::cout << "~A" << std::endl;
}
void DoWork()
{
if (!m_kDelegate.empty())
m_kDelegate(69);
}
};
struct B
{
std::weak_ptr<A> m_wpA;
int m_i;
B(const std::shared_ptr<A>&amp; spA)
{
std::cout << "B" << std::endl;
m_wpA = std::weak_ptr<A>(spA);
spA->m_kDelegate = RFMakeDelegate(this, &amp;B::Handler);
}
~B()
{
std::cout << "~B" << std::endl;
if (!m_wpA.expired())
{
m_wpA.lock()->m_kDelegate.clear();
}
std::cout << "Deleted" << std::endl;
}
void Handler(int i)
{
m_i = i;
std::cout << "Handled " << m_i << std::endl;
}
};

// Usage
{
std::shared_ptr<A> spA = std::make_shared<A>();
B* pkB = new B(spA);
spA.reset();
delete pkB;
}
[/CODE]

Well this is what I was doing now - But it's hardly a "pretty" solution. But I guess that's just C++ for you. Edited by elurahu
0

Share this post


Link to post
Share on other sites
At least in my neck of the woods (C# business apps), events/delegates have fallen largely out of favor for this sort of scenario, precisely for these reasons. They're only used now for scenarios where the event and it's handler are guaranteed to have relative lifetimes (a widget and it's child, two members of a screen mediated by the screen view itself, etc.). This often means creating that sort of mediator that can wire up the event properly and knows enough to unwise it when the time comes (or using a different decoupling approach).
1

Share this post


Link to post
Share on other sites
Telastyn raises a great point.


A much better architecture is to have a dispatcher that contains a list of all objects and all events that need to be shuffled around. The dispatcher runs periodically (say, once a frame) and fires off every event that still makes sense, i.e. is linked to still-valid objects. Then the problem just becomes ensuring things occur in the right order, which is generally not too hard.
0

Share this post


Link to post
Share on other sites
OP, what you need is called "connect tracking", which is available in Boost::signal and libsigc++.
Fastdelegate is not a comlete signal/slot system.
1

Share this post


Link to post
Share on other sites
The library in the url I provided uses the FastDelegates library and implements connection tracking (using scope objects), which is exactly what the OP is asking for. I would however suggest that you try to avoid these types of event systems since keeping track of the flow of execution easily becomes almost impossible.
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