Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Classes in DGDK/C++

Author
Message
CLICK
15
Years of Service
User Offline
Joined: 27th Feb 2009
Location:
Posted: 22nd Jun 2009 10:02
Hey guys just looking for some help as to how classes would work for my particular idea.

Basically i am creating a tower defence game.
I have a class called enemy this has attributes like hp, speed, resistance,position,etc.

I would like to be able to create infinite unique copies.
The best solution Ive been explained to create an array (or collection) of records. I can do this fine in Visual Basic however my knowledge of C++ is fairly limited.

ANY relevant advice as to how i can achieve this would be greatly appreciated.

People dont kill People, Lag Kills People
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 22nd Jun 2009 12:36
You would create a class(or struct) that stores the base information about each tower or unit, detailing things such as health, firing speed, movement speed etc. You then have a second class that is for your active game enemies, these store unique information such as current speed, current health etc. However, because things such as the unit's name never change(I assume) you wouldn't want to copy them over, because of this you should reference this base enemy class mentioned above. This can be done via reference or pointer, i.e. BaseEnemy& base or BaseEnemy* base. To answer your question, to copy the data across just write a bunch of methods to get the data from this base enemy class, or make the copyable data public(I'd do the former). It's then just a simple case of assigning a load of variables across in your enemy unit class constructor.

If you want to save some lines then you can also write a separate class that stores all the data that can be copied and inherit it from both your base enemy class and the active enemy class, then just assign it over from pointers to this parent class.

Krisacz
15
Years of Service
User Offline
Joined: 3rd Mar 2009
Location: UK, Manchester
Posted: 22nd Jun 2009 12:42
Instead of using arrays (which are fairly limited) I will suggest to use vectors with constructors. Vectors are some kind of dynamic arrays.
See here:
http://www.cplusplus.com/reference/stl/vector/

And small code example:



I wrote that code at work in notepad so i didn't have chance to check spelling etc. but general idea i guess it's ok.
I hope it's will help you, if you will have any other questions post it here.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 22nd Jun 2009 18:32
If you use STL, you will have to change the runtime to 'multi-threaded' instead of 'multi-threaded debug', or you will get linker errors

CLICK
15
Years of Service
User Offline
Joined: 27th Feb 2009
Location:
Posted: 23rd Jun 2009 03:36
So is a Vector like a collection in Visual Basic?

Thanks for that code snippet ill play around with it tonight and see how i go.

People dont kill People, Lag Kills People
Krisacz
15
Years of Service
User Offline
Joined: 3rd Mar 2009
Location: UK, Manchester
Posted: 23rd Jun 2009 10:51
I don't know VB very well but I assume it is It's like an array with that difference that you don't have to set size of if, it can expand and shrink where ever you want.
CLICK
15
Years of Service
User Offline
Joined: 27th Feb 2009
Location:
Posted: 24th Jun 2009 01:47
I had a few errors with the linking as foreseen by Diggesy, so after changing the debug to release it worked thanks.

People dont kill People, Lag Kills People

Login to post a reply

Server time is: 2024-11-25 08:34:50
Your offset time is: 2024-11-25 08:34:50