need some help optimizing item search system

Started by
4 comments, last by jake_Ghost 19 years ago
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]
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.
Ra
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 CODES0 - Helm1 - Necklace2 - Armor3 - Glove4 - Waist5 - Pants6 - Feet7 - Weapon8 - Ranged Wapon9 - Shield *//* RANKING0 - Normal1 - Magic2 - Rare4 - 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 texturesprivate:	int ItemCount;			 // Holds the amount of items loaded	std::vector<Item> item;  // Holds all item data	std::string input;};extern cItem citem;


Jake
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.
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.
Ra
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]

This topic is closed to new replies.

Advertisement