Skip to main content
GameDev.net gamedev.net
🔒 Locked

unions

Started by The Alchemist Oct 5, 2000 at 6:17 PM 6 replies 600+ views
Original Post
The Alchemist
The Alchemist
i not realy familiarizedwith unions, but i think that it can solve um problem that i´m having, does anyone know a page or a online book with a deep aproach of it?? ======== There´s no ugly woman You that didn´t drink enough A brazilian popular belief
"Everything works out in the end, if it doesn't then it is not the end"
Facehat
Facehat
Well, I can explain it for you. The basic premise is that you're storing two variable types in the same memory space, but the catch is that you can only use 1 of the variables in each instance of the union. For example, say you had a union like this:

//structure which holds our union
struct MyStruct
{
union
{
char c;
double d;
}
};


Then, in the code you could do something like this:

MyStruct Something;
Something.c = '!';

and

MyStruct Something2;
Something2.d = 234.1218;

but, you couldn't do this:

MyStruct Something;
Something.c = '!';
Something.d = 234.1218; //Error! Both are using the same memory spot!

In other words, using a union tells the compiler that in your code you're going to be using only one of those variables per structure.

----------------------------------------
"Before criticizing someone, walk a mile in their shoes.
Then, when you do criticize them, you will be a mile away and have their shoes." -- Deep Thoughts

Edited by - The Senshi on October 5, 2000 8:41:45 PM
The Alchemist
The Alchemist
well i think that i get it.

but i still don´t know if it can solve my problem...

anyway thanks!


========
There´s no ugly woman
You that didn´t drink enough
A brazilian popular belief
"Everything works out in the end, if it doesn't then it is not the end"
acw83
acw83
Please share with us, what is your problem?
arsenius
arsenius
quote:
In other words, using a union tells the compiler that in your code you're going to be using only one of those variables per structure


I don't think that is exactly correct.
a union is two variables using the same memory, but it doesn't tell the compiler anything except that the space is used for two variables. You won't get an error by doing

    union{
char c;
float d;
}

c = 'a';
d = 3.1415;


it will just overwrite 'a' with 3.1415
it's basically up to you to determine what you are using when, so that you aren't overwriting important things. The reason they aren't used much anymore is because we have huge amounts of memory nowadays.
hope that helps/makes sense

oh, and it doesn't have to be used in structures, btw.



-arsenius
'after three days without programming, life becomes meaningless' -The Tao of Programming


Edited by - arsenius on October 6, 2000 1:14:00 AM
The Alchemist
The Alchemist
two things:

first. you can put a array as a member of a union? ( i think that you can, but i´m geting some weird errors)

and second.

my problems is: i´m updating my battle game engine in a way that in a near future i can put graphics on it. my first measure is totaly isolate the text printing from the rest of the program, so i will don´t have text printing calls in the middle of the logic, to do that i will create a class that do that and make calls to it(so when i try to put graphics on the programm i only have to change the class)

now is the problem, the class most be able to format and print the stats of the caracters, print given strings(if you know my demo it is the the effects of the atacks),and a lot another things. and it most print it in the order that it received the calls.

so i thought( this is a very rare thing to happen ) i could make a array of unious, fill each element with the type of information that is need when the interfaces are called, and print it in the order...

but is far more easy to talk than make

!

while i was writing this i get a idea. i will fill the union with diferent types of objects that stores the right information, and make each one with a print(), so each one also know how print itself... but i already have 8 classes in this thing i realy don´t wanna make this thing a class party... if you have a better sugestion

========
There´s no ugly woman
You that didn´t drink enough
A brazilian popular belief



Edited by - The Alchemist on October 6, 2000 3:00:38 AM
"Everything works out in the end, if it doesn't then it is not the end"
null_pointer
null_pointer
Hmm...that's a use for unions that I hadn't thought of...


Anyway, you might be better off to make some higher-level classes instead of making lots of smaller classes, and just place the code inside of them instead of inside the main loop. For example:


// This is an empty class that just lets the program know
// what functions the different modes have.

class mode
{
public:
virtual ~mode(); // necessary for deleting a mode*
virtual void frame() = 0;
};

// Derive menu, battle, and map modes from the mode class,
// then override the virtual functions with their own code.

class menu
{
public:
menu() { /* initialize anything */ }
virtual ~menu() { /* clean up anything */ }
virtual void frame() { /* insert code for the menu frame here */ }
};

// ...ditto for the battle and map classes, except that
// they each have their own code in their frame functions.

// In your main loop, you create a mode pointer, then create
// a new menu mode and store its address in the mode pointer.
// Then go into a loop, calling the frame() member function
// of the mode pointer. When you replace the menu object with
// a battle object, the battle object's frame() function will
// be called from the main loop and you don't have to worry.
// Just make sure that the pointer is pointing to the correct
// object before you call frame().

int main(int, char*)
{
mode* current_mode = new menu; // start in menu mode

for( ;; )
{
// add in your OS-specific way to check when to quit,
// so you don't annoy the user.

// add code to see if we need to change modes, since
// we can't do that within the frame() functions. This
// would be a good place for a global variable or struct
// that tells us whether to switch modes and which mode
// to switch to. You switch modes by deleteing the current
// mode and creating a new mode while assigning it to
// the current mode pointer. Like this example:
// delete current_mode; current_mode = new battle;

// the following code works no matter which mode you are in:
// do the work for the frame...
current_mode->frame();
}
}



Of course, you need to add code for timing, so you get smooth gameplay, and initializing/uninitializing your hardware (i.e., DirectDraw). You don't want to be destroying and re-creating DirectDraw every time you switch modes, so that goes into the main() function. Ditto for the other hardware stuff, and you may want to make those things into global variables, so that all modes can access them.

Happy Coding!



- null_pointer
Sabre Multimedia


Edited by - null_pointer on October 9, 2000 9:24:43 AM
The Alchemist
The Alchemist
i think that i get it null_pointer. but it go agaist the way that i´m making my program, and i will gonna remake too much code to implement something like that, actualy i´m making something more independent from the main game code. but thanks for the idea.

========
There´s no ugly woman
You that didn´t drink enough
A brazilian popular belief
"Everything works out in the end, if it doesn't then it is not the end"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.