Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,550 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,578 people online right now. Registration is fast and FREE... Join Now!




Tutorial Help

 
Reply to this topicStart new topic

Tutorial Help, Use of Unions in games

NickDMax
28 Oct, 2007 - 07:04 AM
Post #1

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
Ok, I am trying to finish up this bad idea I started a while back called "Data Modeling for Games in C". The next section is on structures (which is actually what I wanted to talk about to begin with). But I wanted to mention something about Unions too... problem is, I can't think of a real use for Unions in a text based RPG. Now, I KNOW how to use unions (I don't need a tutorial or anything) but can anyone think of some practical uses of C Unions in terms of a text based RPG?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Tutorial Help
28 Oct, 2007 - 11:13 PM
Post #2

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Something like this?
(I know its C++, it's just to give you an idea.)

CODE

class Weapon
{
public:
          string name;
          int price,damage;
};
class Shield
{
public:
          string name;
          int price,defence;
};
class Bag
{
public:
          union Slot
          {
           Weapon wep;
           Shield shield;
          };
          Slot slots[50];
};



This post has been edited by manhaeve5: 28 Oct, 2007 - 11:16 PM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Tutorial Help
29 Oct, 2007 - 04:54 AM
Post #3

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
Not a bad idea. In fact a pretty darn good one. The problem is of course that you don't know if you have a weapon or a shield in the slot (but do you care?). It actually had not occurred to me to talk about polymorphism in C as I have never really found it all that practical.

Any other ideas?
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Tutorial Help
29 Oct, 2007 - 06:30 AM
Post #4

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Maybe they are a bit the same but you now alteast now what you're dealing with
CODE

struct Loot
{
union loot
{
  int gold;//size: 4
  Item *item1;//size: 4
}
int type;//1 is for gold , 2 is for an item
}
struct Monster
{
Loot drop;
int hp,damage;
string name;
}


This post has been edited by manhaeve5: 29 Oct, 2007 - 06:33 AM
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Tutorial Help
29 Oct, 2007 - 07:26 AM
Post #5

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
thank you again... this was more along the lines of what I was thinking of.
User is offlineProfile CardPM
+Quote Post

Pontus
RE: Tutorial Help
29 Oct, 2007 - 09:35 AM
Post #6

Dreaming Coder / Coding Dreamer
Group Icon

Joined: 28 Dec, 2006
Posts: 544



Thanked: 4 times
Dream Kudos: 275
My Contributions
Your welocme. smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 09:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month