Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411468 Posts in 69368 Topics- by 58422 Members - Latest Member: daffodil_dev

April 23, 2024, 12:59:14 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Generic functions and game structure
Pages: [1] 2
Print
Author Topic: Generic functions and game structure  (Read 3280 times)
davidv
Level 0
**


View Profile
« on: November 30, 2014, 10:49:15 AM »

I am working on a strategy/roguelike in C++. I have some functions for generating maps (generating random points, voronoi) and other functions that are not dependent on game code. Then of course I have classes like Map, Entity etc.

I haven't used C++ for some time now and I can't decide how to best organize my code. Should I create a namespaces for these functions, e.g. "voronoi" with generate function and other required functions for voronoi algorithm. Another approach would be to create classes with static methods.

What approach do you use in your games? It would be great if you could show some examples.
Logged
kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #1 on: November 30, 2014, 11:29:53 AM »

Class with static methods would be the thing I'd go for. I'd also avoid namespaces.
Logged

oahda
Level 10
*****



View Profile
« Reply #2 on: November 30, 2014, 11:50:39 AM »

I say the exact opposite to what kamac says. Unless you can somehow find a way to use voronoi objects – which you shouldn't try to if you already have a good functional system – there is no reason at all to use a class with static methods only. This isn't Java. You don't have to use a class at all times. A namespace comes with the exact same syntax (double colon) when calling these functions anyway.
Logged

Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #3 on: November 30, 2014, 11:51:56 AM »

I am working on a strategy/roguelike in C++. I have some functions for generating maps (generating random points, voronoi) and other functions that are not dependent on game code. Then of course I have classes like Map, Entity etc.

I haven't used C++ for some time now and I can't decide how to best organize my code. Should I create a namespaces for these functions, e.g. "voronoi" with generate function and other required functions for voronoi algorithm. Another approach would be to create classes with static methods.

What approach do you use in your games? It would be great if you could show some examples.

Namespaces are the proper C++ solution.  Classes with static methods are largely a hack used by languages like Java that don't support free functions.
Logged



What would John Carmack do?
oahda
Level 10
*****



View Profile
« Reply #4 on: November 30, 2014, 11:54:11 AM »

Gentleman
Logged

kamac
Level 10
*****


Notoriously edits his posts


View Profile
« Reply #5 on: November 30, 2014, 11:56:47 AM »

Prinsessa and Average Software are right  Waaagh!
Logged

Boreal
Level 6
*


Reinventing the wheel


View Profile
« Reply #6 on: December 01, 2014, 11:58:50 AM »

The only reason I would ever use a struct (not class) with only static methods over a namespace is if it needs to go into a template.  Namespaces can't be template parameters.
Logged

"In software, the only numbers of significance are 0, 1, and N." - Josh Barczak

magma - Reconstructed Mantle API
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #7 on: December 01, 2014, 12:23:21 PM »

Namespaces can't be template parameters.

How I wish they could.  I'd also like to be able to make templated namespaces.  I was going to write both of those up as proposals for the next language revision, but got lazy and didn't do it.
Logged



What would John Carmack do?
Cheezmeister
Level 3
***



View Profile
« Reply #8 on: December 01, 2014, 10:33:10 PM »

Namespaces are the proper C++ solution.  Classes with static methods are largely a hack used by languages like Java that don't support free functions.

In other words, Namespaces are what JC would do.
Logged

෴Me෴ @chzmstr | www.luchenlabs.com ቒMadeቓ RA | Nextris | Chromathud   ᙍMakingᙌCheezus II (Devlog)
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #9 on: December 02, 2014, 05:34:43 AM »

Namespaces are the proper C++ solution.  Classes with static methods are largely a hack used by languages like Java that don't support free functions.

In other words, Namespaces are what JC would do.

I'm assuming that's John Carmack, in which case no, he would just do it in C.
Logged



What would John Carmack do?
anthnich
Level 1
*



View Profile WWW
« Reply #10 on: December 02, 2014, 09:09:58 AM »

A namespace will be fine, but I personally only use namespaces for "global" free functions that need containment, or to resolve anything that might cause a name conflict. If I'm going to be storing any sort of data, I prefer to use a Singleton.
Logged

Turnover @ Steam
Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #11 on: December 02, 2014, 04:40:52 PM »

A namespace will be fine, but I personally only use namespaces for "global" free functions that need containment, or to resolve anything that might cause a name conflict. If I'm going to be storing any sort of data, I prefer to use a Singleton.

This is what the unnamed namespace is for.  You put the data in the source file like so:

Code:
#include "my_header.h"

namespace
{
    // Unit data.
    int thing;
    int the_other_thing;
}

This makes the data completely inaccessible from outside the source file.  Singletons, like "static classes," are a hack used in a pure object-oriented languages like Smalltalk in order to fake this sort of internal linkage.  There's really no place for them in C++, which natively supports the features that singletons simulate.
Logged



What would John Carmack do?
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #12 on: December 03, 2014, 11:07:45 AM »

^^^ assuming the build system you use doesn't use source merging. Otherwise that flies out the window.


Logged

Sik
Level 10
*****


View Profile WWW
« Reply #13 on: December 03, 2014, 11:46:34 AM »

I'm assuming that's John Carmack, in which case no, he would just do it in C.

I avoid C++ like the plague, but if I were to use it, I'd use namespaces for this as well.

This makes the data completely inaccessible from outside the source file.  Singletons, like "static classes," are a hack used in a pure object-oriented languages like Smalltalk in order to fake this sort of internal linkage.  There's really no place for them in C++, which natively supports the features that singletons simulate.

Don't forget the part that the initialization order of singletons is completely unpredictable so you can never tell for sure when initializing if they're ready to use yet.
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #14 on: December 03, 2014, 12:46:57 PM »

What do you mean about initialization order. What if you explicitly call init on the singleton?

Are you saying the initialization order is bad because you're expecting someone to just get the singleton and have it initialize if it's the first attempt to get it? Because that's entirely avoidable and you would have a similar issue with a namespace that holds state.
Logged

Sik
Level 10
*****


View Profile WWW
« Reply #15 on: December 03, 2014, 12:50:01 PM »

Classes that get constructed before main executes (which is normally what singletons do) can end up being constructed in any completely arbitrary order, meaning you have to be careful that they don't rely on each other during that period or you'll hit a bug sooner or later. Note that this is not an issue with POD types (including the basic types) since they don't have constructors to be called.
Logged
Cheesegrater
Level 1
*



View Profile
« Reply #16 on: December 03, 2014, 12:58:43 PM »

Singletons normally get constructed the first time you ask for an instance, not before main executes.
Logged
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #17 on: December 03, 2014, 01:05:34 PM »

Yeah a singleton gets constructed on first get. If I have to use them I do like having an explicit init that must be called before the first get to prevent human error.
Logged

Average Software
Level 10
*****

Fleeing all W'rkncacnter


View Profile WWW
« Reply #18 on: December 03, 2014, 04:29:50 PM »

Singletons normally get constructed the first time you ask for an instance, not before main executes.

I find this behavior completely obnoxious and it amazes me that people sometimes claim this is a benefit of singletons (not that I'm saying you're arguing that).  I want my stuff to either initialize when I tell it to so I can handle errors, or before main() so I don't even start running if there are problems.  Having possibly critical code initialize at some undisclosed future time is just a bug waiting to happen.
Logged



What would John Carmack do?
InfiniteStateMachine
Level 10
*****



View Profile
« Reply #19 on: December 03, 2014, 04:45:58 PM »

That's why I'm a fan of the explicit init.

namespace with state would be like

SomeThingThatHasState::Init();

or singleton

SomeThingThatHasState::Init();

But back on topic, yes, I agree that singletons in C++ don't need to be used. It's probably just that a lot of people initially used languages where singletons are more necessary that force a class on everything and picked up singletons as a habit. I suppose you could make the argument that even in those languages you could have a static class with an init function but that would be more tedious to enforce an init call on.

I have spoken to some people that are in favor of singletons in c++ because they imply that they are holding state while a namespace might imply non-mutating functions that hide data in the cpp file via anonymous namespaces etc. I don't personally agree but it's certainly an argument.
Logged

Pages: [1] 2
Print
Jump to:  

Theme orange-lt created by panic