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 / Types in C++?

Author
Message
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 4th Aug 2009 23:54
In my current game im porting to dgdk, i have a type:

and im curious how i can turn an object into an array sort of. Like basically how can i make it so in c++ i can say something like:


Just give me an example or a link please! its the only thing thats holding me back!

Slayer93
20
Years of Service
User Offline
Joined: 5th Aug 2004
Location: I wish I knew
Posted: 4th Aug 2009 23:59
Look into structures, this should get you started on it - http://www.cprogramming.com/tutorial/lesson7.html

Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 5th Aug 2009 00:00
Use an array.



http://www.cplusplus.com/doc/tutorial/arrays/

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 5th Aug 2009 00:12
how would i mix arrays and objects? or do i need structs?

puppyofkosh
17
Years of Service
User Offline
Joined: 9th Jan 2007
Location:
Posted: 5th Aug 2009 00:14 Edited at: 5th Aug 2009 00:14
You'd want an array of structs I think.

something like


wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 5th Aug 2009 00:16
thanks thats going to help alot!

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 5th Aug 2009 07:14
Quote: "vector<Weapon> ArrayofStructs(10);
"


Why the "vector" thing? he just needs this:



Just wondering what that vector thing is.

New Site! Check it out \/
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 5th Aug 2009 07:39
Vector is an STL container. Like an array, but a lot more flexible.

http://www.cplusplus.com/reference/stl/vector/

Your_Health = (My_Mood == HAPPY) ? 100 : NULL;
wickedly kick it
18
Years of Service
User Offline
Joined: 13th Jul 2006
Location: Fort-worth Texas
Posted: 5th Aug 2009 08:03
Now, i've been reading up on classes and arrays, they seem better for the type of engine im using where i'd have to do something like "weapon[1].shoot();"

Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 5th Aug 2009 17:52
You can't resize arrays, but you can resize vectors. You can still access a vector in exactly the same way you would an array, using []

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 5th Aug 2009 18:35
Oh ok sweet thanks for that link i will read up on vectors

New Site! Check it out \/
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 6th Aug 2009 18:06
I've ran into problems with vectors and static librarys. While it is true that vectors are more "flexable" than arrays. They are slower and more complicated. With all this extra "stuff" comes alot of extra "stuff" you might not be ready to deal with.

If you know you need 50 trees just declare int MyArrayOfTrees[50]; and be done with it. Simple. Easy.

I may be wrong but it sounds like your a beginner at C/C++. I really think you should firmly understand arrays and structs before you even consider using vectors or any STL stuff.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 6th Aug 2009 19:24
Quote: "They are slower and more complicated."


I doubt you'd notice any speed difference, the contents of a vector are stored in a contiguous list just like normal arrays so the only additional overhead is from calling the [] operator, which is most likely going to be inlined thus there should be no speed difference when accessing.

Also, while vectors may have some 'complicated' features, you don't have to use them; you can easily write code as shown above that does the same thing as arrays but is better in every way.

Quote: "If you know you need 50 trees just declare int MyArrayOfTrees[50]; and be done with it. Simple. Easy."


vector<int> MyArrayOfTrees(50); Simple, Easy, More Powerful.

Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th Aug 2009 19:24
If you're using a large array then it's better to dynamically allocate it (otherwise you're either adding unnecessary size to the exe or consuming valuable stack memory), and in that case it's better to use a vector so that you don't have to worry about managing the memory yourself. I don't think there's any speed difference if you use them exactly how you would an array.
david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 6th Aug 2009 19:39
Ok there is a slight speed difference, array is faster. I know for a fact you both have been around for awhile and know the ins and outs of c++ but this guy doesn't seem to. While I'm of the mind of dont tackle to much at once. I'm just saying that array is simpler to learn than vector. I mean come on, really guys unnecessary size, stack memory, lol, you have to be joking.

I have many a game programming book here that clearly states to just allocate it and be done with it. If you know you only need so many of SuchObject, then use the array.

@DC if he is only going to use it like you say.

vector<int> MyArrayOfTrees(50);
then
int MyArrayOfTrees[50]; Simple, Easy, Cleaner, Faster.

I'm all for STL, I think its great, I use it all the time, when its called for. Just because you can use a double linked list doesn't mean you have to or should. Next thing you know your going to put this guy into multiple inheritance and polymorphism.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th Aug 2009 19:55
Quote: "I mean come on, really guys unnecessary size, stack memory, lol, you have to be joking."


Are you saying this because you don't know the consequences? Fine, an enlarged EXE is not so much of a problem, but if you allocate too much memory on the stack your application will crash.

Compile and run this in debug mode to see what I mean:

david w
18
Years of Service
User Offline
Joined: 18th Dec 2005
Location: U.S.A. Michigan
Posted: 6th Aug 2009 20:03
LOL ok, I dont want to argue about it. you win whatever.
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 6th Aug 2009 20:34 Edited at: 6th Aug 2009 20:36
It's not like I'm arguing for the sake of it.

I partially agree with what you are saying but because vectors aren't necessarily hard to use (although their syntax is a little strange) and because they can avoid some potential problems and make things easier, I'd recommend them even to a beginner for certain things. Of course, that person should still understand regular arrays as they can be used a lot of the time.

And of course, let's not forget that vectors have bounds checking.
Diggsey
18
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 7th Aug 2009 02:20
When going from DBPro to C++, it is easier to use vectors because they are roughly equivalent to DBPro's arrays. (Resizeable, bounds checking, no risk of allocating too much stack memory, etc.)

Mista Wilson
16
Years of Service
User Offline
Joined: 27th Aug 2008
Location: Brisbane, Australia
Posted: 7th Aug 2009 05:14
I have to agree, I think it would be definately worth learning how to use the STL properly if you dont already, especially coming from DBPro to C++ as diggsey said.

I think though, given the amount of differing opinions in the thread that it may be a good idea to do some research on your own on the differences between normal array usage and STL containers and the efficiency and best uses for them both.

Here are some links on the topic of STL efficiency and general info about that kind of thing :

STL info:
http://www.devx.com/tips/Tip/13605

Efficiency info re C++ :
http://www.tantalon.com/pete/cppopt/appendix.htm

STL wiki :
http://en.wikipedia.org/wiki/Standard_Template_Library

Hope you find something that helps

If it ain't broke.... DONT FIX IT !!!

Login to post a reply

Server time is: 2024-10-01 10:40:37
Your offset time is: 2024-10-01 10:40:37