Skip to main content
GameDev.net gamedev.net
🔒 Locked

Practical methods for object templates.

Started by ekrax Nov 2, 2004 at 6:28 PM 3 replies 700+ views
Original Post
ekrax
ekrax
hello. i have ran into a problem where i can't find a practical solution, practical in that it uses the minimal amount of memory in the end. basically i'm in the progress of writing my ever changing engine and world editor, and the problem is that i need a way to keep track of objects that i can create and the object instances that exsist (for ease of use in the world editor). well it's not that i can't solve the problem, it's i can't solve it without seeming to waste memory. i will explain and you will see. basically i have a "manager" which will control the instances of my objects, but also i have a "factory" (which i'm currently working on) which will contain a map with all the different types of objects that can be created. types of objects are not actual types. i mean i have an object type, ground type, scenery type, and others. all of which will be mapped using std::map and given a name (key). then i fill these maps with generic instances of these objects (basically different sprites, and eventually different events tied to them) then i can create these objects and keep track of their instances in another module. but it's bugging me becasue the mapped objects exsist but do nothing, they just sit their and wait to be duplicated for an instance. (really the factory holds instances, but i make other instances of these objects based on their properties). so its bothering me becasue i have multiple objects that are in memory all the time but dont really do anything activly. the whole purpose in this is in the world editor, say i want to make a tree well i want to select a tree and place it in the map. in the code i want to say ... manager.addInstance(factory.getObject("Tree01")); it will the get the object associated with "Tree01" return its self (not a pointer) ... so now i have an instance in the manager class. but it seems its sorta a waste of memory. if this made any sense at all, and you have a solution i would appreciate hearing it, thanks in advance.
snk_kid
snk_kid
Quote:
Original post by ekrax
but it's bugging me becasue the mapped objects exsist but do nothing, they just sit their and wait to be duplicated for an instance. (really the factory holds instances, but i make other instances of these objects based on their properties). so its bothering me becasue i have multiple objects that are in memory all the time but dont really do anything activly.


This is known as "proto-typical instances".

Anyways have a look at the loki library there is a generic factory type that is quite configurable to suit your needs might be useful just to give you an idea if you don't wont to use it.

[Edited by - snk_kid on November 2, 2004 7:10:01 PM]
leiavoia
leiavoia
yeah, that's the Prototype Pattern at work. That's what a standard pluggable factory uses as you already know. There isn't anything wrong with it unless you really are pressed for memory. If you are not, than you might just except it. The prototype strategy is one of the basic Gang of Four Design Patterns, so don't feel too bad about using it :-)

Personally i think it's a good idea for world creation, especially if you are doing so from some kind of external data. I don't think you are doing anything wrong or sloppy at all.

You might find this old thread of mine somewhat beneficial:

ekrax
ekrax
ahh good i was worried i was doing something terribly wrong. i'll be looking into that loki library. but for now i think im gonna go with my orginal plan (maybe working with XML later on instead of the custom text files i use now). i have one more quick question though, maybe more a matter of opinion.

since these objects will be "proto-typical" they wont exsist anywhere so their positions (x,y) will be NULL, 0, doesnt matter since they arent getting rendered or physically moved.

would it be really worth it to create another base class that gets rid of any geometry stuff (in my case only x, y, and a few interface functions dealing with them)? or is it good enough just to set x and y to 0 and dont worry about it?
leiavoia
leiavoia
I would just set the geo data to their "factory defaults". You will want some kind of Reset() or setter functions to activate the cloned objects of course. Just be carefull that the prototype object isn't introduced into the world somehow, isn't referenced by some internal list, clever smart_pointer setup, or tracking system ("There are now [1] XYZ objects in existance") and doesn't get deleted by those systems either.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.