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 / Question On Lists

Author
Message
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 18th Mar 2010 22:18
In my project I am trying to create an editor that will allow you to create your own levels. The way I am trying this is with a list to store all of the different objects in the game.

Basically I have all of the objects that I want in classes. For example I have a rigid box class and within that class I have an update function and an initialize function.

So if I make a list of the rigid box classes can I go through that list and basically tell every rigid box in the list to update and initialize?

Specifically for my my editor I want to initialize the box when the person clicks somewhere and then when they want to play the game it will update all of the boxes or objects.

Is it possible to do this with a list?
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 19th Mar 2010 08:43
Of course it's possible. You can do it with an array, a vector or even a linked list template.

Here is an example. This explains mainly about virtual functions, but virtual or not, accessing public functions of a class through a pointer stored in the vector is done the same way.

http://en.wikipedia.org/wiki/Virtual_function

The only catch is that if you have objects of different types (different classes), then either you use separate vectors for the different classes, or you have to have a common ancestor class (just like in this virtual example) which has common functions defined, otherwise you don't have a common interface to handle all objects the same way.
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 19th Mar 2010 21:39
Wow thanks that really cleared some stuff up for me. It kind of brings me to another question then. Every time I want to create a new rigid box I need to create a new sprite. I have a content manager that basically works like this:



So basically I use names rather than having to worry about the number associated with each sprite. I also do this with the dbSprite Function as well.

So my question is how would I dynamically change the names of all the sprites every time I create a new Rigid Body?

I was thinking that somewhere in my RigidBody class when call the content manager to draw and create a new sprite I can dynamically change it in the class. I'm just not to sure how I would do that. Anyone have some suggestions or a link that can help me?
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 21st Mar 2010 21:12 Edited at: 21st Mar 2010 21:19
I'm not sure I understand the question correctly, so you want to create a new image name every time when a new instance of the class is created? You can define a static variable within the class which works as a counter. Add 1 to it in the constructor every time when a new object of that class is created and if you make a string of that number and attach it to the image name, then the name will surely be unique.

Here's a bit more explanation about static class members (scroll down):

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

In this example the counter is decremented in the destructor but you probably won't want that if objects are created/deleted in random order, because you may end up with identical numbers.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 21st Mar 2010 21:30
P.S. I don't quite understand the necessity for image name when you already have a file name and a unique index which is generated by the content manager. That CurrentIndex variable basically does the same what I suggested above. Numbers are generally easier to handle in a program than strings, if you need to auto-generate the identifier anyway. But as I said above, I'm not sure I understood completely what you'd like to do.
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 24th Mar 2010 08:15
Hey, Yeah sorry I didn't realize how vague my last question was. Basically I want to create an editor that will allow you to create boxes I.E.(Crates, bricks) and after you put everything in your level you will be able to play it.

So how I was thinking of doing it was in my Box or "crate" class and here is a quick example of what they look like:



As you can see right now I am using the dbSprite function still which is what my content manager will replace.

So when I call the NewSprite function in my content manager I take the name of the Image and then I make a name for the sprite. So every time I create a new crate/box for instance I have it right now that I specify a new name. With the editor I need it to dynamically change the name of the sprite so it would be more like, "crate01" and then "crate02" without me haveing to actually hard code the names.

Not to sure if that makes more since. I could just be confusing you with bad programming. I'm still a little new to it all but I guess I have to start some where =).
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 25th Mar 2010 08:52 Edited at: 25th Mar 2010 08:57
In the light of your explanation, I think my suggestion above, the static counter which is incremented in the constructor, makes perfect sense. That will give you a new number every time when an instance of that class is created.

This method has one drawback, that it is not possible to re-use numbers when an object is deleted. If you would like to search for free numbers, e.g. to find that you have crate01 and crate03, but crate02 does not exist so you can use number 2 again, then it becomes more complicated. In that case you can try a vector which stores if an index is free or not, or maybe dbImageExist or dbSpriteExist functions, but then the index becomes linked to the Dark GDK identification numbers and that is not what you want here.

As long as you do not have so many sprites that the indexes will overflow the integer range I think the simple counter will do.
Mireben
15
Years of Service
User Offline
Joined: 5th Aug 2008
Location:
Posted: 25th Mar 2010 19:23
I've been thinking further about this and I think that sooner or later, you are going to need a vector, after all. If you are making an editor, that means you'll create and delete objects multiple times, then save the level, load it and continue modifying it again. Even if you don't run out of integers, with an ever-increasing counter the numbers will soon be large and that looks ugly in the editor interface (supposing the names are displayed for the user). So I suggest to trust the numbering to the content manager, or some other class which keeps track of which objects exist and which indexes are free to re-use.
Poof Master
14
Years of Service
User Offline
Joined: 14th Jan 2010
Location:
Posted: 25th Mar 2010 21:03
Hey thanks for the response. I have been working on it lately and am still trying to convert my code to be using the content manager. I will post the content manager that I am going to use and kind of explain how I think I am going to start the editor.



Now I know my content manager is still not that great seeing as how I am still a little new to programming but as you can see I am just making it so when I call LoadImage and Updatesprite I just have to put in names rather than the numbers. So for my editor I need to be able to create a new sprite and change it's name. So thats why I was asking how to change strings without actually manually changing them in my code? Which I think is where you directed me above I just haven't been able to look at it because I am still implementing the editor.

So do you think that content manager will work with an editor or should I redesign?

Thanks for all the responses so far I'm just still trying to get the hang of some of these concepts.

Login to post a reply

Server time is: 2024-07-13 10:19:18
Your offset time is: 2024-07-13 10:19:18