structures.. HELP!!! [solved]

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

structures.. HELP!!! [solved]

Post by Clodi » November 27th, 2012, 6:54 pm

:D
hi guys.. simple silly trivial question..

I know how to declare a structure. But, how to initialise it?

I declare it in "game.h" saying:

struct Hello
{
int wow;
int waaaaw;
};

then?????????????/
I don't seem to be able to find a way to initialise it in "game.cpp"...
awful story guys.. anybody ble to help??

thanks lot :roll: :D
Last edited by Clodi on January 26th, 2013, 7:47 pm, edited 1 time in total.

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: structures. HELP!!!

Post by LuX » November 27th, 2012, 7:37 pm

You don't need a separate initialization for it. After you have defined it make an object of it "Hello helloObj;" and in the game initialization just define the values as something "helloObj = { 10, 50 };"
ʕ •ᴥ•ʔ

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: structures. HELP!!!

Post by Clodi » November 28th, 2012, 4:59 am

No, mate. I need to initialise it. look..

lets say I want to have a structure for a few boxes that the player will control in turns (one after the other) and I am now just starting to program and I want to get the first object to move.

1) I declare the Struct in game.h

Code: Select all

struct Box
	{
		int x;
		int y;
		int vx;
		int vy;
		int size;
	};
2) I declare the user variable:

Code: Select all

Box PlayerBox;


3) In game.cpp I do the coding for an arrow key control thing..

Code: Select all

gfx.DrawBox( PlayerBox.x,PlayerBox.y,PlayerBox.size,255,255,255 );

	if ( kbd.DownIsPressed() ) { PlayerBox.y = PlayerBox.y + PlayerBox.vy; }
	if ( kbd.UpIsPressed() ) { PlayerBox.y = PlayerBox.y - PlayerBox.vy; }
	if ( kbd.RightIsPressed() ) { PlayerBox.x = PlayerBox.y + PlayerBox.vx; }
	if ( kbd.LeftIsPressed() ) { PlayerBox.x = PlayerBox.y - PlayerBox.vx; }
This will NOT work.
It doesn't work because the compiler doesn't know where to draw the box at the beginning because no values are being initialised
If I initialise the PlayerBox, then I can't move bacause I would experience the same problem that Chili did in his lessons (the "amnesia" thing..)
The frame is being built with the same values everytime. I need a way to initialise structures so that the value can then be changed. Same as what Chili did with normal variables for position, vellocity, etc.

Can you please tell me how to do this?
I am sure it's not too hard, it's just that I seem not to manage it.. :(

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: structures. HELP!!!

Post by LuX » November 28th, 2012, 2:10 pm

Read my comment again and do what I told you : -P

Besides, when you don't initialize a value it will get a random high value the is unpredictable ( unless you specify in the settings what the uninitialized value will be ) so the values will get something like 2340390358. Depending on the settings they may also get the maximum or minimum value ( which may explain why you can't add anything to it ).

In case you didn't understand, by using "helloObj = { 10, 50 };" is the same as typing "helloObj.wow = 10; helloObj.waaaaw = 50;"

But there is no way to "initialize" a structure as you would do for a class, however you can set a value for a structure in a class initialization, which is probably what you mean by "initializing a structure".
ʕ •ᴥ•ʔ

Musi
Posts: 106
Joined: November 25th, 2012, 1:06 am

Re: structures. HELP!!!

Post by Musi » November 28th, 2012, 4:28 pm

I't sounds like you're initializing the member values inside the ComposeFrame() function which runs every frame so you're setting them to the same value every frame.

Set your memeber values in the braces below the "Game" initialization at the top of game.cpp

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:	gfx ( hWnd ),
	kbd( kServer ),
{
     PlayerBox.x = 100;
     PlayerBox.y = 350;;
     PlayerBox.vx = 10;
     PlayerBox.vy  = 10;
     PlayerBox.size = 150;
}
This should only set them once.
Pretty much what lux said, but i couldnt get Object = { 100,350,10,10,150 }; to work in the initialization list.
Musi

There are 10 types of people that understand binary.
Those that do, and those that don't.

User avatar
XxWalKaxX
Posts: 244
Joined: June 11th, 2012, 7:15 pm

Re: structures. HELP!!!

Post by XxWalKaxX » November 28th, 2012, 7:00 pm

in game.h under the user variables, type the name of your struct which in your case is Hello, and then pick the variable you want to use to access the information in your struct...for instance..........

Hello hello;



Go to game.cpp to test by going to compose frame and typing hello. if your intellisense kicks in everything is done right.
What you call a bug...I call a new feature!

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: structures. HELP!!!

Post by LuX » November 28th, 2012, 7:30 pm

Now that I research this matter I see you can make a constructor for structures the same way you make for a class, so you could have something like:

Code: Select all

struct TEST 
{ 
    int a, b, c;

    TEST ( ) : a ( 0 ), b ( 0 ), c ( 0 ) { }
    TEST ( int n_a, int n_b, int n_c ) : a ( n_a ), b ( n_b ), c ( n_c ) { }
};
And then initialize the values by "test ( 10, 11, 12 )" in the game initialization list. But this should't really make any difference, other than speed, compared to the earlier method, so if you didn't get that working this won't work any better.

Also, the "test = { 10, 11, 12 };" method apparently only works when you use it like this: "TEST test = {10, 11, 12};" So it wont work if you already define "test" in the game class.

I suspect the problem is what Musi thinks. If you can't get it working on your own upload the project.
ʕ •ᴥ•ʔ

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: structures. HELP!!!

Post by Clodi » November 28th, 2012, 8:19 pm

Thank you so much Lux, thank you so much Musi.

Yes.
The problem is exactly what Musi said:

I was initialising (trying to ) in the list and not inside the braces.. ARGH!!!!

This works perfectly:

Game::Game( HWND hWnd,const KeyboardServer& kServer )
: gfx ( hWnd ),
kbd( kServer ),
{
PlayerBox.x = 100;
PlayerBox.y = 350;;
PlayerBox.vx = 10;
PlayerBox.vy = 10;
PlayerBox.size = 150;
}


Anyway, thank you very much Lux for trying to help me and research on that. I could see your point in the first reply, I just couldn't get it to work.
I will also try your "second version" tonight and see how it works and how it looks/work together with the code so far and see what's better.

Thanks again. Problem solved.

Post Reply