• 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.
  • entries
  • comments
  • views

Blob Grappling Hooks

Sign in to follow this  
Followers 0



Just a quick update on the soft body game. Now have the basic prototypes for the left and right mouse button grappling hooks working in terms of the physics. No idea yet how I'm going to render these arms in a decent way but it is showing some promise.

Thanks for all the positive feedback on my recent article.

A few posters commented on the issues I'd missed to do with deleting events or delegates during the execution of an event call and, funnily enough, shortly after this I discovered I needed to do exactly this in the game.

The solution I have is two-fold. Firstly I created a lockable vector to represent the storage of the Event class. This basically works as a normal vector unless locked. When locked, instead of removing elements, it just swaps them out with nullptrs and any additions to the vector during a lock are added to a second vector. When unlock() is called, it cleans out the nullptrs and adds any of the added items to the main vector. This means that while the vector is locked, the iterators to the main list can't be invalidated:

template class delegate_vector{public: delegate_vector() : x(0), n(false) { } void lock(){ ++x; } void unlock(){ if(!--x) clean_up(); } bool locked() const { return x; } void push_back(const T &t){ x ? a.push_back(t) : v.push_back(t); } void remove_all(const T &t){ x ? remove_all_lock(t) : remove_all_unlock(t); } typename std::vector::iterator begin(){ return v.begin(); } typename std::vector::iterator end(){ return v.end(); }private: void erase(std::vector &v, const T &t){ v.erase(std::remove(v.begin(), v.end(), t), v.end()); } void clean_up(){ if(n) erase(v, nullptr); for(auto i: a) v.push_back(i); n = false; } void remove_all_lock(const T &t){ for(auto &i: v) if(i == t){ i = nullptr; n = true; } erase(a, t); } void remove_all_unlock(const T &t){ erase(v, t); } size_t x; bool n; std::vector v, a;};The next step was to change the Event class to be called BaseEvent, then wrap it inside another Event class. This way, when the Event is deleted, I can postpone deleting the BaseEvent until it has finished its current processing. This involves the dreaded delete this but I believe it to be a good use here. Looks like this now:

template class Event;template class BaseEvent{public: BaseEvent() : d(false) { } ~BaseEvent(){ for(auto i: v) i->remove(this); } void connect(AbstractDelegate &s){ v.push_back(&s); s.add(this); } void disconnect(AbstractDelegate &s){ v.remove_all(&s); }private: friend class Event; BaseEvent(const BaseEvent&); void operator=(const BaseEvent&); void call(Args... args){ v.lock(); for(auto i: v) if(i){ i->call(args...); if(d) break; } v.unlock(); if(d) delete this; } bool d; delegate_vector*> v;};template class Event{public: Event(){ e = new BaseEvent(); } ~Event(){ if(e->v.locked()) e->d = true; else delete e; } void operator()(Args... args){ e->call(args...); }private: friend class Delegate; Event(const Event&); void operator=(const Event&); BaseEvent *e;};So if the Event gets deleted during its own processing, the deletion of the BaseEvent which contains the actual state data is postponed until after the execution has a chance to break out of the loop. delete this is to be used with extreme caution. I accept no responsibility for the many bad things that can happen if you stuff this up. smile.png

The reason I find delete this acceptable here is this is a non-copyable class that is only ever created as a member of an Event. So at the point d is true, we know the only class that could be referencing this one is already dead and gone. Thus we avoid the many potential problems normally associated with delete this that are generally related to having multiple references to an instance.

The posters in my article were discussing an approach to this that involved maintaining some static state, but I prefer to avoid that wherever possible. My approach avoids the need for any static state but adds a minor overhead that all Events now have an additional indirection when invoked.

In terms of the game, the way I have Box2D wrapped is that my Body class emits a collided event. This is all handled inside the Physics class, so when you create a Body you can just connect to its collided event via the delegate system.

In the case of the grapple, when it collides with the scenery, I need to delete its body and form a distance joint between the previous link in the grapple rope and the scene object. Rather than hack and postpone removing the body until the next frame, I wanted to be able to delete the Body inside its own collided() event hence the need to work out the system above.

As far as I can grok, the above approach protects against pretty much any combination of connects, disconnects etc that you throw at it. I've also tested making a new connection to an Event inside its own event invocation and that also seems to work okay, but I wouldn't bet much on there being no nasties still lurking somewhere, even in the single-threaded context it is designed for.

I may create a new article on this at some point if anyone is interested.

Sign in to follow this  
Followers 0


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