7 Replies - 1403 Views - Last Post: 04 July 2012 - 07:58 AM Rate Topic: -----

#1 KateBush  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-July 12

C++ text game help

Posted 02 July 2012 - 07:34 PM

I am making a text based game and I am pressured for time. I need something which will allow me to store i.e. 'a key' the player will pickup somewhere within a house. Once the player has collected the key then the computer will remember this so when the player goes to open a door, the key can be used to unlock it.

It doesn't have to store the information but more or less know the player has interacted with the key, so the door will trigger to unlock.

Basically I am not sure what to use, in-order for this function to take place.

Is This A Good Question/Topic? 0
  • +

Replies To: C++ text game help

#2 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4462
  • View blog
  • Posts: 24,905
  • Joined: 10-May 07

Re: C++ text game help

Posted 02 July 2012 - 07:40 PM

When you initialize the character at the beginning of the game, I would suggest making a struct. This would be a table, if you will, of all attributes for the game. Since the character is starting with no key, then the key entry would be a zero integer value. When the key is received in game play, update the struct element to 1. When they is required in the game, simply look at the value of the struct element to see if the key is 0 or 1. Then proceed accordingly.
Was This Post Helpful? 0
  • +
  • -

#3 ButchDean  Icon User is offline

  • Ex-Pro Games Programmer
  • member icon


Reputation: 873
  • View blog
  • Posts: 3,323
  • Joined: 26-November 10

Re: C++ text game help

Posted 02 July 2012 - 09:12 PM

Sounds like you left this last minute.
Was This Post Helpful? 0
  • +
  • -

#4 BBeck  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 561
  • Joined: 24-April 12

Re: C++ text game help

Posted 03 July 2012 - 06:37 AM

You want what's called a "flag". No2pencil is right.

An easier solution is just to declare a global boolean (true/false) variable, set it to false, and then set it to true when the key is picked up. Then you can test whether it's true in order to open the door.

A more advanced solution is to setup a bitmap for this type of switch, where every "flag" is represented by a bit. That way you can store 8 flags per byte. It's the same thing but more memory efficient.

If you have 8 boolean variables in C++, I'm not sure if the compiler will store them all in the same byte or not. If it doesn't, it will waste memory and disk space and the bitmap solution makes a lot more sense.
Was This Post Helpful? 0
  • +
  • -

#5 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 928
  • View blog
  • Posts: 3,997
  • Joined: 14-February 08

Re: C++ text game help

Posted 03 July 2012 - 08:24 AM

View PostBBeck, on 03 July 2012 - 02:37 PM, said:

A more advanced solution is to setup a bitmap for this type of switch, where every "flag" is represented by a bit. That way you can store 8 flags per byte. It's the same thing but more memory efficient.


Why in gods name would you do this? There seriously is no point!

Quote

An easier solution is just to declare a global boolean (true/false) variable, set it to false, and then set it to true when the key is picked up.


Booleans are ints so you are just doing the same thing as no2pencil suggested.
Was This Post Helpful? 1
  • +
  • -

#6 BBeck  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 561
  • Joined: 24-April 12

Re: C++ text game help

Posted 03 July 2012 - 12:26 PM

View Poststayscrisp, on 03 July 2012 - 08:24 AM, said:

View PostBBeck, on 03 July 2012 - 02:37 PM, said:

A more advanced solution is to setup a bitmap for this type of switch, where every "flag" is represented by a bit. That way you can store 8 flags per byte. It's the same thing but more memory efficient.


Why in gods name would you do this? There seriously is no point!

Quote

An easier solution is just to declare a global boolean (true/false) variable, set it to false, and then set it to true when the key is picked up.


Booleans are ints so you are just doing the same thing as no2pencil suggested.


Looks like you are right about Booleans being integers. MSDN says they've been integers since VC++ 5.0. And that is exactly why you would want to store them as a bitmap instead.

There's probably a little overhead dealing with bits, and also a little bit of a learning curve for those not familiar with binary, but there's no doubt that it's more efficient to store a boolean value as a bit rather than a byte when you have a lot of them.

There may be some good reasons not to bother storing booleans as bits that are probably mostly based on the fact that it may just be too much hassle or too much overhead when you're only dealing with a couple of boolean values that may be out of scope in a nanosecond anyway.


Again - as I said, this is really an advanced solution and not the sort of thing you would use in a beginner level game. The reason I say that is that this approach is not likely to pay off until you have thousands of such flags to deal with or are at least checking the flags thousands of times per second. Not exactly the sort of thing you would encounter in a text based game.

Anyway, to answer the question of "why in gods name" would you do that. Storing a bit value, such as a boolean, as an entire byte is extremely "inefficient". You're ignoring seven bits of storage for every flag. Not much of a big deal if you've got a couple dozen of these, but it could be a very big deal if you had something like a million of them. Depending on where it's used in code, you might even see a performance increase from using bit values for booleans with just a a few bytes.

I haven't used assembly language in awhile and don't remember exactly how the code's written, but suffice it to say that you are going to have to do at least 8 tests to 8 boolean values stored as 8 bytes (integers). If they are all bits in the same byte you can basically test all 8 with one instruction which greatly reduces the number of CPU cycles you're burning.

As an example, WIN32 uses bitmasks for dialog boxes to determine whether the dialog box has a cancel button, displays an error icon, displays a warning icon, etc. Booleans are easier for programmers to grasp, so why did Microsoft decide too use bitmasks all over Windows instead?

Again, this is not the type of thing you would use on a simple text game. But what if instead of just needing to know that the key had or had not been picked up, keys were instead worth point. And for each level you need to know which grid squares contain keys and which do not. On a grid that's 1024 by 1024 (not that hard to imagine), you would have 1,048,576 bytes storing all your booleans. They might also not be aranged in contiguous order creating unusable memory fragments and maybe as much memory to manage them as the megabyte that they consume. If you're wasting CPU cycles reading them a million CPU cycles adds up. Further more, you're using up memory and physical ram is a limited commodity. You don't want to run out of physical memory due to storing garbage. And then there's disk space, and the fact that the more you store on disk the more exponentially slower the program is going to run as it waits on disk. If you're sending this data across the network, you're also wasting network bandwith.

You can get an 8 to 1 compression ratio storing a boolean as a bit rather than a byte. If you do that enough times your game will run far more than 8 times faster as it no longer has to read 8 times as much data from disk, transfer 8 times as much data through memory, transfer 8 times as much data through the CPU registers, and 8 times as many CPU cycles if it can do in one bit mask test what would normally require 8 integer tests.

I've had the oppurtunity to look at some commercial software that is designed to manage terabytes of information "under the hood". It uses enormous arrays of bit values to manage almost everything. Essentially, you have management memory pages in memory as well as on disk with a fixed length header and then the rest of the page is just bits with every bit representing a boolean value.

So, it's quite common in commercial software including Windows itself.

I don't know if I would use bit masks every time I need to store a boolean, but there are a lot of times where it can make an enormouse perfomance difference.

In the example of a simple text game, you obviously wouldn't bother using them, but I suppose I was pointing out that bitmasks and bitmaps are worth serious consideration when you get into more advanced projects.

This post has been edited by BBeck: 03 July 2012 - 12:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 928
  • View blog
  • Posts: 3,997
  • Joined: 14-February 08

Re: C++ text game help

Posted 04 July 2012 - 01:13 AM

Oh, don't get me wrong I fully understand how it works and why you might want to use it to squeeze some performance out of a graphically heavy game. But for a text based game like this, created by a beginner it's seriously overkill. Why complicate things for a project like this?
Was This Post Helpful? 2
  • +
  • -

#8 BBeck  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 207
  • View blog
  • Posts: 561
  • Joined: 24-April 12

Re: C++ text game help

Posted 04 July 2012 - 07:58 AM

As I said numerous times, worrying about bitmaps and bitmasks is not a subject for a beginning text based game. Even in my first post I hinted at that by saying it was more of an "advanced" solution although someone might have interpreted that wrong as if I were saying that it were a "better" solution for that particular problem - which is not what I meant.

One reason I mentioned it is that I tend to write in kind of a "stream of conciousness" form, just like I speak, where I say what comes to mind.

But also, I was pointing it out as "something to think about as the OP gets into more complex programming".

More than that, you never know who's reading these posts. The posts aren't "just" about the OP but everyone reading them. There might be someone who's ready to learn about bitmaps and bitmasks, and the connection they make from that insight might send them in the right direction on their own project.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1