Reducing Large Number of Variables

Started by
4 comments, last by Daishim 22 years, 8 months ago
I am working on a game with a rather large amount of weapons and equipment items, is there an efficient way to only load into memory the ones needed at the current moment and as new ones are encountered the variables for them will be declared? ie, I have a Broadsword in the game, but the character doesn''t have one, so the damage, range, and other stats aren''t needed.

I know only that which I know, but I do not know what I know.
Advertisement
This is just a theory... Perhaps you could define a pointer struct ITEM *items (global) and keep count of the item amount and make a function for removing items and adding them somehow like this :

...
item_amount++;
add_item();
...
remove_item_a(item_number);
item_amount--;
remove_item_b();
...

add_item()
{
int i;
struct ITEM temp[item_amount]; /*The catch is that I''m not sure if you can use variables like this*/
for(i=0;itemp=items;<br>/*set the new item info to temp[item_amount]*/<br>*items=temp; /*Not sure about this either*/<br>}<br><br>remove_item_a(item_number)<br>{<br>int i;<br>struct ITEM temp[item_amount];<br>for(i=0;i<item_amount-1;i++)<br>temp=items;<br>for(i=item_number+1;i<item_amount;i++)<br>temp[item_number-1]=temp[item_number];<br>*items=temp;<br>}<br><br>remove_item_b()<br>{<br>int i;<br>struct ITEM temp[item_amount];<br>for(i=0;i<item_amount-1;i++)<br>temp=items;<br>*items=temp;<br>}<br><br>I dunno, maby it seems too good and easy to be true. Opinions anyone? Could use something like this myself.. </i>
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Don''t you have to use malloc and free, or new and delete?

If it''s relatively small, like 1000 ints (4 KB) then you could let it pass statically unless you''re targeting 8-16mb crowds.

If it''s a really large #, like 320,000 ints (1.3 MB) then you might want to allocate it using linked lists or some other dynamic variable system. If you have time and/or cash buy or borrow from a friend Andre LaMothe''s Tricks of the Windows Game Programming Gurus, he describes linked lists in detail. Or you could do a web search on linked lists. If you''re targeting 512MB crowds then you might not need this .

If it''s a 4kb structure, with 20,000 max (80MB) then you absolutely need linked lists unless you''re targeting the mainframe crowd.
What about my theoretical way of doing it? Can someone say if it''s a working solution?
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
ummm...
dunno how much it''ll help you, but i have
a good linked list unit (c/c++) from my
uni lessons, if you need it, just say the word

Gil
[email=gil@gilzu.com]Gil Zussman[/email]Check out Goose Chase, Coming this fall!http://www.gilzu.com
One way you could do it is to have a base class that is the basic weapon definition, which could just be a 16 bit int to use as an identifier, and if you don't want to use built in RTTI, a virtual function to return if it is the base class.
class base
{
public:
short int unique_id;
virtual bool isBase() { return true; };
}

and also have a derived class containing full stats
class derived : public base
{
// whatever stats you need
public:
virtual bool isBase() { return false; }
}

Then you have a list/array of pointers to class base, for each
weapon/armour type (basically it's just an array of pointers to ints at this point) when you come accross a weapon you access it in the array (I'd match the unique_id to the array posn so I could do something like....)
if (baseArray[obj.unique_id].isBase())
{
// derive the child using the parent
baseArray[obj.unique_id] = new derived(obj);
}

In the child's constructor load you stats from file. To really save memory, periodically check how recently an object was used & revert the pointer back to a base class if not recently used.

I'd appreciate comments, cause I just thought of this now.

Brad


Edited by - brad_beveridge on July 22, 2001 6:23:06 PM

This topic is closed to new replies.

Advertisement