Jump to content
  • Advertisement
Sign in to follow this  
jake_Ghost

need some help optimizing item search system

This topic is 4956 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm at the stage in my game where I'm adding items to it. Finally :D. But there's a big problem I'm have. At startup I load all the item data that I have saved in a file. Periodically through the game I need to look up item data/textures, the time it takes will most likely be very long, especially with anything is open that needs to see that data like my inventory. Heres an example, on my character screen I have the usual gear (armor, weapons, ect). For each of those slots I have to loop through all the items to find the item code I'm looking for which will give me all the item specs. Lets say I have 500 items, and 10 places a loop will search the item list, that means that my program could loop a total of 5000 times per frame looking for items!! Which would be extremely slow. Any help on how I could improve this search method would be greatly approved. Jake [Edited by - jake_Ghost on April 25, 2005 5:31:16 PM]

Share this post


Link to post
Share on other sites
Advertisement
If your item codes are mostly contiguous you could probably go with a simple array of classes/structs. You can the item code as an index into the array, giving you O(1) search time.

Share this post


Link to post
Share on other sites
I don't know if i could do that because each item has a set code :/. Here's my class. When I load an item, i just push the vector.


#include <windows.h> // Header File For Windows
#include <math.h> // Math Library Header File
#include <stdio.h> // Header File For Standard Input/Output
#include <mmsystem.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
#include <vector>

/* ITEM CODES
0 - Helm
1 - Necklace
2 - Armor
3 - Glove
4 - Waist
5 - Pants
6 - Feet
7 - Weapon
8 - Ranged Wapon
9 - Shield */


/* RANKING
0 - Normal
1 - Magic
2 - Rare
4 - Unique */


struct Item
{
float ItemNum;
int Armor;
unsigned int DamageMin;
unsigned int DamageMax;
unsigned int DPS;
float AttackSpeed;
std::string Name;
int Rank;
std::string Info;
std::string Bonus1;
std::string Bonus2;
std::string Bonus3;
std::string Bonus4;
std::string Bonus5;
std::string ItemPic;
float DropPercent;
};

class cItem
{
public:

// constructor
cItem();

// deconstructor
~cItem();

BOOL LoadItems(); // Loads all item data
BOOL InitItems(); // Loads all item textures

private:

int ItemCount; // Holds the amount of items loaded
std::vector<Item> item; // Holds all item data

std::string input;

};

extern cItem citem;




Jake

Share this post


Link to post
Share on other sites
You seem to be doing something like, having a unit and associating a name of an item with that unit, and then to check the texture of that item for instance you need to look the intire list of items.

That's wrong. =)

What you need to associante the unit with is a pointer to the item object. That way when you need to access anything from that item, you do it directly.

Share this post


Link to post
Share on other sites
I'm not entirely sure what you're searching with. Are you iterating through the list and looking for the matching item code so you can get the stats for the appropriate item? Are you matching strings with the item name? How do you know you have the right item?

What I would do here is this: I'd assign a unique ID number to each item, store that ID in the helm/pants/weapon slot, and use that ID as an index into an array containing all the items. That way the item ID is stored in each slot giving you direct access to any item stats when you need them.

Share this post


Link to post
Share on other sites
Quote:

I'm not entirely sure what you're searching with. Are you iterating through the list and looking for the matching item code so you can get the stats for the appropriate item? Are you matching strings with the item name? How do you know you have the right item?

What I would do here is this: I'd assign a unique ID number to each item, store that ID in the helm/pants/weapon slot, and use that ID as an index into an array containing all the items. That way the item ID is stored in each slot giving you direct access to any item stats when you need them.


Well pretty much I have a unique item number saved in ItemNum. And when I'm searching for an item I use that number.

Quote:

You seem to be doing something like, having a unit and associating a name of an item with that unit, and then to check the texture of that item for instance you need to look the intire list of items.

That's wrong. =)

What you need to associante the unit with is a pointer to the item object. That way when you need to access anything from that item, you do it directly.


How could I change my class to increase the speed by using the method that you suggested?

Jake

[Edited by - jake_Ghost on April 25, 2005 5:49:21 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!