Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: cannon and bullet  (Read 8928 times)

0 Members and 2 Guests are viewing this topic.

Jabberwocky

  • Full Member
  • ***
  • Posts: 157
    • View Profile
Re: cannon and bullet
« Reply #15 on: May 23, 2015, 04:42:09 pm »
I've used singletons in several large production projects, and never run into a single problem with them.  Like any other code construct, it has to be properly designed, and properly used.  There are bad ways to use singletons.  There are also good ways.

The singleton class I use only has a static pointer to the singleton instance.  That pointer is allocated, assigned, and deleted in very precise fashion, on very precise lines of code.  There is no problem with the order of creation and destruction as has been discussed here.  This is a common singleton arrangement.

About the the criticism of global data.  In practice, this is an inconsequential critique.  Singletons should only be used for objects which exist for the lifetime of a game, from initialization to shutdown.  Whether that object lives in the stack, or is global has almost no practical consequence. 

Yes, a singleton can be accessed from anywhere, given you include the header and access functionality via the provided API.  This is exactly the convenience that Singletons provide.  If you used a non-singleton, this does not change where or when you will need to use that class' functionality.  Essentially, you end up doing the exact same thing, except the code to access that class might look a little different, and is often more cumbersome, and may require more layers of indirection.

I agree that, as much as possible, you should attempt to reduce coupling between systems.  But again, in practice, the systems in all but the simplest games will be interrelated to a large degree.  It is practically impossible to write code where your low level systems (graphics, sound, physics, etc) and higher-level gamesystems are not tightly coupled.  Most attempts to do so result in overly-complicated event-driven systems which don't change the interrelation, but just complicate it to the point that debugging and engineering is far more difficult. 

Most practical applications of threading I've seen in games involve easily split, repetitive tasks (e.g. processing updates on particle systems, or processing pathing data).  Using singletons does not make this any easier or any harder. 

Singletons are fine.  Just use a proper singleton class, and understand how and when to use them.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: cannon and bullet
« Reply #16 on: May 27, 2015, 06:39:36 pm »
Singletons are fine.  Just use a proper singleton class, and understand how and when to use them.
I guess we'll just have to agree to disagree.

If what you want is global access, then a global variable will do; you don't need a singleton for that.
If you only want a single instance of a class, then only create one; you don't need singletons for that.

I personally fail to see that singletons provide any real advantage and I'd personally much rather avoid them (and globals in general) and stick to having my objects constructed and destroyed in a deterministic way in deterministic scopes and just pass references or std::unique_ptr's/std::shared_ptr's where needed.

Of course you can use globals (and singletons) safely; it's just harder than just not using them. It's a tool and can have its uses. I just consider it a very sharp tool with very, very few real use cases, that is usually more trouble than gain. But, whatever floats your boat :)
« Last Edit: May 27, 2015, 09:25:10 pm by Jesper Juhl »

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: cannon and bullet
« Reply #17 on: May 29, 2015, 03:30:55 pm »
Meanwhile while these guys are arguing about singletons.


Since the cannon fires the bullet it is a "has a" relationship meaning bullet is a field or just simply created by a method in the cannon class.

class Cannon
{
private:
     Bullet bullet
}

or

class Cannon
{
public:
Bullet fireBullet();
}
 

Just some ideas while these guys are arguing. :)
I have many ideas but need the help of others to find way to make use of them.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: cannon and bullet
« Reply #18 on: May 29, 2015, 04:45:16 pm »
Isn't that more-or-less what I already said as the first reply in this thread? ;)

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: cannon and bullet
« Reply #19 on: May 30, 2015, 02:40:33 am »
Isn't that more-or-less what I already said as the first reply in this thread? ;)

Yes but sometimes there are people that, well for lack of a better term need it simplified.  Sometimes less text is better, while other times making them figure it out is better.  Depends on the person. ???
I have many ideas but need the help of others to find way to make use of them.