• Create Account

## Reducing Large Number of Variables

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Daishim  Members

244
Like
Likes
Like

Posted 19 July 2001 - 06:50 AM

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.

### #2Afterlife  Members

122
Like
Likes
Like

Posted 19 July 2001 - 07:50 AM

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++;
...
remove_item_a(item_number);
item_amount--;
remove_item_b();
...

{
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;i temp=items[i];
/*set the new item info to temp[item_amount]*/
}

remove_item_a(item_number)
{
int i;
struct ITEM temp[item_amount];
for(i=0;i temp[i]=items[i];
for(i=item_number+1;i temp[item_number-1]=temp[item_number];
*items=temp;
}

remove_item_b()
{
int i;
struct ITEM temp[item_amount];
for(i=0;i temp[i]=items[i];
*items=temp;
}

I dunno, maby it seems too good and easy to be true. Opinions anyone? Could use something like this myself..

### #3gph-gw  Members

122
Like
Likes
Like

Posted 19 July 2001 - 08:22 AM

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.

### #4Afterlife  Members

122
Like
Likes
Like

Posted 20 July 2001 - 01:32 AM

What about my theoretical way of doing it? Can someone say if it''s a working solution?

### #5Gilzu  Members

122
Like
Likes
Like

Posted 20 July 2001 - 01:40 AM

ummm...
a good linked list unit (c/c++) from my
uni lessons, if you need it, just say the word

Gil

122
Like
Likes
Like

Posted 22 July 2001 - 11:20 AM

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.