Chunky by FelipeFS
Chunky by FelipeFS
GPWiki.org
It is currently Fri Dec 19, 2014 12:10 pm

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Mon Dec 10, 2012 9:09 pm 
Bit Baby

Joined: Tue May 29, 2012 12:50 pm
Posts: 8
Hi all,

I recently started working on a simple networked Battleships game as I thought it'd be within my reach, thought I've run into an interesting problem and I'm not sure how to approach it.

Basically, I'm using a simple entity management class to display animations, and I'd like objects of that class to be able to call an action in the main game or do something else to otherwise let the game know they've finished animating. For example, when you click on a grid square to attack your opponent, I wanted a flashing crosshair animation to play for a few seconds before showing whether you hit or missed your opponent. Then, the game runs a function called "swapPlayers" that simply moves over to the other player's turn. The problem I have here is I'd really like to start the "attack" animation, but only call the "swapPlayers" function once it has displayed for the time I've specified. As it stands I call them both at the same time, which means the animation shows as it should, but also immediately changes turn, which means you can't see if the shot you took actually hit or missed.

Hopefully the above explanation makes some sense. I'd post some of my code by it's already a bit big to just slap on a forum post and may not make things much clearer. I've written a short summary that should explain the function of my code a little.

Code:
//entity class that animations are objects of
class entityManager {
   void animate(); //function simply starts a timer and keeps looping the animation until a time limit is reached
};


//the function in the main game that checks if a shot hit or missed
//and plays the appropriate animation before swapping players
void checkShot() {
   //code to check whether the shot hit or missed would go here

   //the part that's giving me a problem:
   explosion.animate(); //object of entity class
   swapPlayers(); //gets called at the same time as "animate", but ideally needs to be delayed until it's finished.
}


If anyone can offer any advice on dealing with this problem, I'd be very grateful. The solution might be really stupidly obvious, but I've been trying to figure a way around it for a while and have got nowhere.

Thanks


Top
 Profile  
 
PostPosted: Mon Dec 10, 2012 10:16 pm 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
There are a few things you can do. I'm not sure how your entity system is designed so I can only give general answers.

1) create a new entity for swapping turns and have it trigger after a certain delay
2) pass in a callback function to the explosions animate that will swap turns

basically you are just looking to make a timer of sorts and have it call a function when the timer is up.

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
PostPosted: Mon Dec 10, 2012 11:38 pm 
Bit Baby

Joined: Tue May 29, 2012 12:50 pm
Posts: 8
Thanks for the reply,

I was thinking along those lines, so I read into member-function pointers and realised I could use these to call the function from elsewhere. There's just one major snag with that method.

To declare a member function pointer (AFAIK) requires the use of the scope operator, e.g:

Code:
void (myClass::*myFunction)()


The issue with this is that I can't create a pointer to my main game state inside the entity manager class without including the header file for my main game (otherwise it doesn't know what the class is). The problem here is I believe with "circular dependencies" ; the main game (gamestate.h/cpp) already includes "entities.h" - if I tell entities.h to include "gamestate.h" then it causes all sorts of problems during compilation. But how can I possibly declare a function pointer to the gamestate class if I can't include the header?

Hope that makes sense,
thanks again


Top
 Profile  
 
PostPosted: Tue Dec 11, 2012 3:33 pm 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
First make sure you have your ifdefs in your header files, then you can use a forward declaration.

Bob.hpp
Code:
#ifndef _BOB_HPP_
#define _BOB_HPP_

#include "Jane.hpp"

class Bob
{
  ....
  Jane* jane;
};

#endif


Jane.hpp
Code:
#ifndef _JANE_HPP_
#define _JANE_HPP_

class Bob;

class Jane
{
  ....
  Bob* bob;
};

#endif


Jane.cpp
Code:

#include "Jane.hpp"
#include "Bob.hpp"

....

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
PostPosted: Tue Dec 11, 2012 8:14 pm 
Bit Baby

Joined: Tue May 29, 2012 12:50 pm
Posts: 8
This works perfectly, thanks a million :)

The mistake I was making previously was including each header file within the other class's header, and not including one in the cpp file, hence the circular dependencies. I thought my #ifndef statements were enough on their own :doh

If I may ask one final question; is there any way I can make the callbacks generic, so that I can pass in any function pointer I like without the receiving class knowing what it is ahead of time? For example with my current setup I'd need to manually create a pointer for each function that I might want to call, regardless of whether I call it or not.

Thanks


Top
 Profile  
 
PostPosted: Tue Dec 11, 2012 8:54 pm 
Double Guru
User avatar

Joined: Fri Aug 12, 2005 8:58 am
Posts: 2009
Location: LA, CA
You might want to look at using Boost Signals, it will make your callbacks less painless.

http://www.boost.org/doc/libs/1_52_0/do ... gnals.html

_________________
My Development Blog | My Website | My Current 3d Engine


Top
 Profile  
 
PostPosted: Wed Dec 12, 2012 1:49 pm 
Bit Baby

Joined: Tue May 29, 2012 12:50 pm
Posts: 8
As ever, many thanks :)


Top
 Profile  
 
PostPosted: Wed Dec 19, 2012 6:12 pm 
Boost signals will in fact make your callbacks "less painless". I highly recommend avoiding them, they are sloooowwww. Absurdly slow. Several orders of magnitude slower than c style callbacks. I recommend you add a basic c style callback to your animation, mine has 3 void*s which is probably as much callback data as you would need.


Top
  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron

Powered by phpBB® Forum Software © phpBB Group