Thread: Dynamic Object Creation (and Management)...

  1. #1
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31

    Dynamic Object Creation (and Management)...

    For each 'item' on the screen, I want to store its data in variables within a class. All 'items' have the same variables, so I want to create an object (of the 'item's class) for each 'item' on the screen, and adjust the variables of each individual 'item' (i.e. variable "x_position", of object "ship1", of class "frieghter"... Though the class probably won't be that specific unless it's through inheritence).

    The problem is, I want to be able to delete and create these objects on run-time. I could create these as 'static' object arrays in the memory prior to their actual use in the game, but aside from limiting the number of 'items' I can have thoughout the game (unless I declared more arrays, which would require memory even after the destruction of the 'item'... Another memory draw-back), it would require more memory for 'items' not even present in the context of the game.

    So I was wondering how I could create object arrays (or just objects), that I could delete individually, while maintaining numerical order (like lists, vectors, and maps).

    In other words, referring to my previous 'example', create class "ships", with child-class "frieghter", and then create "ship1, ship2, ship3... ship'n'", and then be able to delete those individually (and free the memory used by them). Or, more simply, is there any way I can use a variable to name an object of class?
    If a=b, and b=c, then a=c, except where void and prohibited by law...

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I would create a vector of ship classes... each vector specific to the type of ship.

    or try a vector of vectors.. where the first dimension corrosponds to the type of ship.. second dimension is the specific ship itself.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Yes, you can create objects of user defined defined classes dynamically.

    >is there any way I can use a variable to name an object of class?

    No you can't use variables to name objects, but you can use variables as members of objects to do essentially the same thing. For example, if each object had a member variable of type int you could use it to keep track of how many objects you had at any given time or how many objects had been created since the beginning of the program or in what order they were added to the group of currently active objrcts, etc.
    You're only born perfect.

  4. #4
    Marxist-Trotskyist
    Join Date
    May 2005
    Location
    Many, many miles from the center of the Earth.
    Posts
    31
    Elad, do you mean instead of using static arrays as member variables, use vectors (or lists or whatever) as the member variables, and then have a plain int member variable to denote how many instances of the 'item' are being considered? Like, instead of having multiple instances of a class, and deleting or adding those to represent the number of 'items' and their properties, use one instance of each specific class (like The Brain was talking about), and then have the properties of each 'item' (x position, y position, health, et cetera) vectors within the classes, whose size is determined by a main variable indicating the number of said 'items' on screen (or in-game)?... 'Cause I think I see what you guys mean. Thanks for the replies, by the way.
    If a=b, and b=c, then a=c, except where void and prohibited by law...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Level creation, loading, management etc
    By Ariste in forum C++ Programming
    Replies: 1
    Last Post: 08-02-2005, 11:09 PM