Saturday, June 18, 2011

In Profundis Progress (6/18)

I continue to bash my head bloody against the brick wall of C++.  I have an annoying problem where I try to define a variable in a header, and other files that include that header complain about multiple definition errors despite the presence of a header guard.  This is annoying for me since one design pattern I frequently use is to use one file as a place to store essential global values, like gravity and friction values, for easy reference/changing later, but if I just use that file for those variables the linker balks.

I asked a programmer friend for help and his suggestion managed to resolve the problem (basically to move the values into a different source file), but the reason he gave that it works uses a development metaphor that is nonsensical to me, I don't grasp its context, and so I'm resigned, for the time being at least, to doing it just because it works, not because I understand it, and that makes me nervous.

As for the stuff I'm implementing, I'm working on the data structures used for holding graphics data right now.  Fun!

4 comments:

  1. Welcome to C++. I'm a bit surprised you didn't go with something like Java; it's clearly good enough for modern games (see also: Minecraft, Spiral Knights). I love C++, but it's not a friendly language.

    As for the idiom: C++ inherits the global variable behavior from C. C implements it this way because it's designed to be a really simple language to write a compiler for.

    In essence in your header file you want something like "extern int global_taco_count;" (a declaration) to tell anyone including the file, "Hey, there a global variable called global_taco_count and you can use it." In a source file you say, "int global_taco_count;" (a definition) to say, "Okay, if someone comes looking for global_taco_count, this, right here, is the location to find it." When the linker shows up, it finds everyone who is wondering where global_taco_count is and points them at the right location.

    When you put "int global_taco_count;" in a header file, it, for all intents and purposes, becomes part of each source file that includes it. Now you have a bunch of global_taco_counts and when the linker shows up it has no idea what to do about it and it gives up.

    ReplyDelete
  2. I didn't go for Java because, if I'm going to go with a non-Python language, I might as well get whatever performance benefit I can. I'd also like the game not to be dependent on some company's VM applet, especially when it's one I've had a malware intrusion because of in the past.

    ReplyDelete
  3. Something like that, I would probably put it in a class that loaded a data file. You could have the values queried by a function, or you could probably just have them as class variables (declared as "static" within a class definition). The reason I'd use a data file is so you wouldn't have to recompile every time you wanted to try a different gravity variable. (And hey, maybe you could even have caves on alien planets with different gravity!)

    But of course there are lots of ways to do it. ;)

    ReplyDelete
  4. I am considering doing it with statics, yes. More news soon I hope.

    ReplyDelete