How to instialize an array of classes( in progress )

The Partridge Family were neither partridges nor a family. Discuss.
indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: How to instialize an array of classes

Post by indus » January 19th, 2013, 3:21 pm

What I did and it works:
in the header file added a new constructor declaration

Code: Select all

class Particle
{
public:
	Particle( D3DGraphics *gfx, KeyboardClient *kbd );
	Particle(int, int, int, int, D3DGraphics *, KeyboardClient *);
	Particle(){}
	~Particle();
        ...
}
added implementation in the cpp

Code: Select all

Particle::Particle( int x, int y, int vx, int vy, D3DGraphics *gfx, KeyboardClient *kbd )
:    pGfx( gfx ),
     pKbd( kbd ),
	 pX (x),
	 pY (y),
	 vX (vx),
	 vY(vy)
{ 
	cR = 100;
	cG = 100;
	cB = 100;
	size = 10;
	health = 10;
}
adn finaly initialisation with random values in the Game.cpp

Code: Select all

Game::Game( HWND hWnd,const KeyboardServer& kServer,const MouseServer& mServer )
:	gfx( hWnd ),
	audio( hWnd ),
	kbd( kServer ),
	mouse( mServer )
{
	 srand( (unsigned int) time ( NULL ) );
	 for(int i = 0; i < 10; i++)
	 {
	     int x = ( rand() % 400 ) + 100;
	     int y = ( rand() % 400 ) + 100;
	     int vx = ( rand() % 20 ) - 10;
		 int vy = ( rand() % 20 ) -10;
		 Charlie[i] = Particle( x, y, vx, vy,&gfx, &kbd );
	 }
}

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

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 3:37 pm

thank you so much
I am reading now: you said
and do the whole random stuff in the Game constructor.
This proofs that I reply to people without even reading what they write..
..ignore me indus :D

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 19th, 2013, 6:52 pm

I cannot get this to work ive tried about 3 diffrent methods they all fail.
Computer too slow? Consider running a VM on your toaster.

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

Re: How to instialize an array of classes

Post by Clodi » January 19th, 2013, 7:00 pm

post your code mate

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 19th, 2013, 7:09 pm

Here is my latest attempt. I know there are many things wrong with this i just pulled this up in 5 mins.
Attachments
Income wars - Copy - Copy - Copy.rar
(642.25 KiB) Downloaded 182 times
Computer too slow? Consider running a VM on your toaster.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: How to instialize an array of classes

Post by indus » January 19th, 2013, 8:23 pm

This approach of creating array in the class is wrong. The last version was working correctly. The only problem with it was that you are creating surfacesequence which consists of multiple surfaces every time you call the Marine constructor. So if you make an array of 10 marines you create 10 times the same surfacesequence which on its own creates 10 times of the same 31 keyedseurfaces . That takes really long time. Instead you might try adding a marine constructor that takes a pointer to a SurfaceSequence as parameter. Than in the game class create one object of type Surfacesequence called marineSurfaceSequence for example and pass it the arguments you pass to the marine constructor now. So having it ready now you can use it when initializing your array of marines.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 19th, 2013, 8:56 pm

O I found my problem I was not sending the currentindex to move and attack. And I think it only creates one surfacesequence for the whole 10 marines. Because all im doing is making 10 vars for the surface sequence to draw. But I still have to sort out a few things.
Computer too slow? Consider running a VM on your toaster.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes

Post by cameron » January 19th, 2013, 9:30 pm

Hey can you look at this one for me I fixed it up but, nothing can attack yet.
Attachments
Income wars - Copy - Copy - Copy-WorkInProgress - Copy.rar
(644.13 KiB) Downloaded 182 times
Computer too slow? Consider running a VM on your toaster.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: How to instialize an array of classes( in progress )

Post by cameron » January 26th, 2013, 7:28 pm

Can anyone give me some feedback on the way I implemented an array into this? And how I can get the attack functions to work?
Computer too slow? Consider running a VM on your toaster.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: How to instialize an array of classes( in progress )

Post by indus » January 27th, 2013, 1:29 pm

I think you should start over with the project. Don't get me wrong. It is just too messy now. You have 2 bases and marines moving between them. For those 3 things you create 5-6 classes and almost each of them inherits from somewhere. And most of the inheritance does not make sense. Like for example why does the Base class inherit from KeyedSequence?
Put all of your classes in separate header files. And make the definitions in a corresponding .cpp files.
this ways it is much more easy for us and for you too to follow on your code. Doing this now will save you a lot of headache later when you have much more classes, methods, logic, code. I can see that you try to use everything chili is showing in his videos but that's not always necessary. You have to keep stuff as simple as possible.
Just a couple of examples. I like your design of Unit class and the Marine inheriting from it. But why pass the arguments needed by the SurfaceSequence in the Units constructor instead of directly passing it a surfacesequence or a pointer to one? Thats one of the reasons why you create classes and construct object of these classes.
Base and EnemyBase are actually the same thing. Make a class Base and pass it in the constructor an KeyedSurface object and the values for x, y, BaseHP and BaseDamage. Something like

Code: Select all

// create the needed surfaces
KeyedSurface goodBaseKeyedSurface(std::wstring(L"base.bmp"), D3DCOLOR_XRGB(0, 0, 0));
KeyedSurface badBaseKeyedSurface(std::wstring(L"enemybase.bmp"), D3DCOLOR_XRGB(0, 0, 0));
// pass them to the constructors of base and enemybase
Base goodBase(goodBaseKeyedSurface, 15, 350, 100, 1);
Base badBase(badBaseKeyedSurface, 1200, 325, 100, 1);
And then the array of marines. To me it makes much more sense that the Base is going to have the array of marines than create them in the Marine class itself. So add an array of Marine objects in the Base class. And let the base object control them.
As already mentioned. Im trying to be constructive in my critics and not trying to insult you in any way.
greets

Post Reply