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 / Simple Question!

Author
Message
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 23rd May 2009 07:54
Complexed answer?

I was wondering if you could 'spawn' a class with a variable, that my have been confusing, maybe this will make you understand.



I'm not sure if thats possible, I tried similar code and it did not compile.
So I was wondering if there was another way to do what im trying to do?


In the grand scale of things i'm trying to access a class from dbPickObject, thus our classes will be object numbers startingat 500 incrementing every time we spawn a new version of our class.

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 23rd May 2009 17:12
So, you are trying to give each copy of your class a different number for a sprite, right?

In the constructor, put a static variable (which means the program will always remember it's last value and not re-initialize it) in the constructor of the class and increment it by one each time the constructor is called. For example:

That is the simplest method of getting a new number each time you make a copy of your class. The first copy of the class will have a sprite_number of 500, the second will have 501, third 502 etc. But just remember, the number will keep on going up from 500, no matter what.

#ifdef _DEBUG
FixBugs(All);
#endif
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 23rd May 2009 18:40
Quote: "But just remember, the number will keep on going up from 500, no matter what."


This is something that I've had to contend with when writing my Sprite class. In theory, as long as you don't call dbDeleteSprite() during the program or delete the Sprite objects in a weird sequence you should be able to decrement the sprite counter with the destructor. All objects should go out of scope in the reverse order in which they're created. The dbDeleteSprite() issue could be dealt with when that function is encapsulated in the Sprite class. Having the object (pointer?) deleted out of sequence? Dunno if that can be dealt with logically.

I very rarely use pointers to generate class objects unless I'm using some third party application that demands it.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 24th May 2009 01:21
I'm not exactly trying to do what you have explained.

I already can create my npc's with my class, but its a 3d program and when i click on the object in game it tells me that objects id, and from the id i would like to select that object id's class to retrieve stats about it which i have set previously.

get it? =]

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 24th May 2009 05:52 Edited at: 24th May 2009 05:54
So you are trying to find out the class of an object just by the object id? Why not just take my advice and do this at one point in your program:

Is that what you want? If you are asking me if dbPickObject() can return the name of class object that has an object id stored in it, well, it can't. But there are workarounds like above. You can also have an inline function inside your class that checks if dbPickObject() returns the object's id that is stored in the class. For example:

Those are just some ways. I'm sure there are plenty more.

#ifdef _DEBUG
FixBugs(All);
#endif
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 24th May 2009 06:14
Ok, that clears up things much, thank you,

I was also wondering if it were possible to run a function on all class objects of a certian type?

rather than
tnpc[1]->initialize()
tnpc[2]->initialize()
tnpc[3]->initialize()
...

could you do something along the lines of:

all tnpc->initialize()

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 24th May 2009 07:04 Edited at: 24th May 2009 07:06
You can use a for statement. http://www.cplusplus.com/doc/tutorial/control/

A for statement runs a block of code and then increments a variable at the end of the block every time. Once the condition is false, the for loop exits. You can do it like this:


And keep in mind, (if you don't already know), arrays are zero-based. An array of tnpc[100] will have values ranging from tnpc[0] to tnpc[99]. Any other value will throw an exception and most likely cause the program to exit.

#ifdef _DEBUG
FixBugs(All);
#endif
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 24th May 2009 07:09 Edited at: 24th May 2009 07:11
Assuming your pointers are in an array then (calling the array tnpcarray


for (int index = 0; index < sizeof (tnpcarray)/sizeof(tnpc*); index++) tnpc[index]->initialize();

Edit:
What you'd have to be careful of is that all elements of the array are occupied by valid pointers. If the number of elements is variable then you might look at using vectors instead of an array to hold your objects.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 24th May 2009 07:53
Great! Everything is working out perfect for now =D

Last question, if I do, tnpc npc[10]; Would there be a way to later in the program add onto the npc[10]? say add 10npc's making a total ov npc[20]?

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 24th May 2009 07:58
There are ways to fake an array using pointers and memory allocation but that's a bit painful to describe. That's why I recommended using vectors instead of arrays. With vectors you and add data to the end of the vector or expand the number of elements in the vector. Yet you can use index subscripting to access the elements of the vector.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 24th May 2009 10:42
I'll just stick with allocating 500npc's that should be more than enough for the time being ^_^



Thank you all for all your help! =D It has let me progress much further!

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D
prasoc
16
Years of Service
User Offline
Joined: 8th Oct 2008
Location:
Posted: 24th May 2009 11:52
use vectors! theyre like arrays but they have no limits, and thus will save more memory
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 24th May 2009 12:30
I am not familar with vectors! =[

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 24th May 2009 18:04
I'd recommend that you look vectors up on a tutorial site for C++. It's a rather involved subject to discuss in this forum. In brief, vectors act like arrays but you can expand them, shrink them and add elements by pushing them onto the vector. You can subscript them like arrays and use iterators to traverse them from beginning to end.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Bran flakes91093
16
Years of Service
User Offline
Joined: 13th Sep 2008
Location: Crazy Land
Posted: 24th May 2009 19:12
Quote: "I'd recommend that you look vectors up on a tutorial site for C++. It's a rather involved subject to discuss in this forum. In brief, vectors act like arrays but you can expand them, shrink them and add elements by pushing them onto the vector. You can subscript them like arrays and use iterators to traverse them from beginning to end."


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

#ifdef _DEBUG
FixBugs(All);
#endif
kklouzal
15
Years of Service
User Offline
Joined: 15th May 2009
Location: Arizona
Posted: 24th May 2009 21:43
thank you very much =D

I <3 C++ & Dark GDK 4 2 maek mai gamezorz! =D

Login to post a reply

Server time is: 2024-11-25 10:40:20
Your offset time is: 2024-11-25 10:40:20