Object ID Numbers (for access and sorting)

Started by
3 comments, last by Khaiy 13 years, 1 month ago
I'm wondering if I should store my object ID codes as int or char arrays?
Right now my program reads in object data from text files. Every object is stored in it's own file with it's own properties (all strings) like so:

(pseudo example)
Object.name = "Unique name goes here"
Object.color = "red"
Object.VehicleType = "fighter jet"
Object.country = "United States"
Object.ID = "010505"

Here's the thing; Object.ID is not random, each digit means something and I need my program to parse it as such. It really should be read as '01', '05', '05'. The first "digit" is the color, the second is the type, and the third is the country of origin. This is easy enough to implement, I just use arrays and a for loop, and the program knows which element it's reading (Object.color = code[0], etc.). This way I can easily call only red objects, or only fighter jets, or only red fighter jets, and so forth.

These ID code strings can get pretty long so I'd prefer to keep them as C style char arrays. However, I'm also using functions to generate new objects, so if rand() gave me 030115 it would translate as "a green tank from Russia." This of course requires the use of int elements, which means I would have to typecast back and forth. Is it worth keeping them as string arrays? Can I compare int values to literals, removing the need for typecasting? Is there a better way of doing this altogether?

Right now I'm using text files and dice dry.gif. It suffices, but its crude, and once I bring in the QT interface I'll need something more clean and elegant. The idea is to get to the point where I could specify certain elements I want for sure, and let the rest be randomly chosen.

I know this is pretty vague, but if anyone understands what I'm trying to implement and has a better way, I'd really appreciate any direction you could provide.
Advertisement
Is there any particular reason that you need all of that information to be stored in a single variable? Or a reason that you need the information stored redundantly as distinct variables and then also as a single ID?

If I were you, I would write a function that randomizes each component of an object seperately (color, country, and vehicle) by generating an integer, and then have an algorithm convert the number to an appropriate string to set the object's properties (or set the properties directly, and have a function return an appropriate string to display to the user).

It'd also be pretty easy to write a function that accepts specific object properties on demand and then returns all results that have those properties. I wouldn't bother with the ID at all. If you really want (or need) the ID, I would put out another function that can check properties of an object and then spit out the ID.

You can parse the ID if you really want to (the implementation will depend on what language you're coding in), but that seems like a very convoluted way to access the information it represents.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~


Is there any particular reason that you need all of that information to be stored in a single variable? Or a reason that you need the information stored redundantly as distinct variables and then also as a single ID?

If I were you, I would write a function that randomizes each component of an object seperately (color, country, and vehicle) by generating an integer, and then have an algorithm convert the number to an appropriate string to set the object's properties (or set the properties directly, and have a function return an appropriate string to display to the user).

It'd also be pretty easy to write a function that accepts specific object properties on demand and then returns all results that have those properties. I wouldn't bother with the ID at all. If you really want (or need) the ID, I would put out another function that can check properties of an object and then spit out the ID.

You can parse the ID if you really want to (the implementation will depend on what language you're coding in), but that seems like a very convoluted way to access the information it represents.


I would have to agree with Khaiy that ID's are probably not the best solution to this problem. I would probably go for implementing Khaiy's latter example of some sort of function that accepts a query of object properties that can dictate to the function what objects in the collection to give back to me. Think of accessing the records in a database with SQL without the SQL same general concept. This allows for both more flexible and controllable system as well as being able to randomly generate selections from a lua script if you choose or even from your C code. It may be more work but it will give you more control over your games functionality.
Thanks for the suggestions, gave me some things to practice and think about.

I have two reasons for using an ID string: One, file format. It just seems like it would be easier to parse one string than several object variables, and there are several more properties than the ones I listed (the member variables I used were mostly for example purposes, and might not remain). Two, it's useful for lookup/indexing purposes. I could hold all this in one text file like so:
0002304563797
0274821034876
1002182722453
1217785499821
etc.

Then to load an object I just use a getline() function.
Keep in mind, these ID formats are just for object types, not multiple instances of them. The instances get their own file:

The Roadkiller 2500
0347356823 (a green american jeep with two automatic turrets that shoot bees and runs on Mr. Pibb)
"This vehicle was built during the desktop wars by Azurecat, who thought he knew what he was doing."

If I wanted to find all objects of type "jeep" I'd just use
while (!EndOfFile)
{
if (object[0] = '03')
{
LoadObjectToBuffer(); //or something like that.
}
NextLine();
}


For a pick-and-choose generator, would using globals be a bad idea?
bool g_fKeepColor = false;
bool g_fKeepType = false;
bool g_fKeepGuns = false;
etc.

Then I'd call
int GenerateVehicle(sVehicle obj)
{
int i = 4; //for string array access

if (g_fKeepColor == false)
{
obj.ID = rand();
}
i++;

if(g_fKeepType == false)

{
obj.ID = rand();
}
i++;
//and so on.
}

All I have to do is set my globals (which will likely be done with checkboxes or drop down listboxes).

I still have quite a bit to figure out, but this is basically how I figured it'd work. You mentioned using a function with multiple parameters to set which values I wanted to keep, where you referring to a constructor? If so I could probably give each object it's own Vehicle.fKeepWhatever variables, with it's own RandomGen() function. It just seems like unnecessary overhead at first glance, but then again I'm new to the OO design end of things. blink.gif

Thanks for the suggestions, gave me some things to practice and think about.

I have two reasons for using an ID string: One, file format. It just seems like it would be easier to parse one string than several object variables, and there are several more properties than the ones I listed (the member variables I used were mostly for example purposes, and might not remain). Two, it's useful for lookup/indexing purposes. I could hold all this in one text file like so:
0002304563797
0274821034876
1002182722453
1217785499821
etc.

Then to load an object I just use a getline() function.



1. You don't need to parse the string object variables because they already indicate what they are. Also, getting to the property you want will be easier without forcing your program to parse an ID and convert the code. Finding out the color of Object is as easy as:


string getProperty(object Object, string property)
{
return Object.property;
}


Using an ID you would need something like



int parseID(int segment, int ID)
{
// Segment is the portion of the ID you want
// You would parse the ID here to get just the two digit segment you want (a hassle, by the way)
return id_seg;
}

string convert_id_seg(int segment, int id_seg)
{
// Each segment will need its own list of strings to return, as values will repeat between segments
// You don't want to return the 02 equivalent of vehicle type if you're trying to get 02 color
// return string that corresponds to the ID code in the correct segment
}


Even without the actual code to do the parsing, it's already more complicated. With the code, it's going to be a lot more complicated. I don't care how many properties your objects have, it will never be easier to parse the ID than to get the properties directly. I'm not sure what you mean by file format. Once your program is running the objects will be stored in memory, you won't need to write them to a file unless you need them to persist from one run of your program to the next. And even then, the ID thing will still be more complicated.

2. Even writing everything to a text file will be easier without the ID. You can write a bunch of strings to a text file with a loop as easily as you can write integers. Plus, the ID code bit is absolutely unreadable. Someone looking at the code would already need to have memorized every combination of numbers for each ID segment to make any sense of it. A list of strings divided by an object heading will make sense to anyone who looks at it. And using the string method will be vastly more useful for looking things up-- if you want a value of 02 in the 7th segment, you can't just search the file since that set will appear so many times in different segments.

Loading an object from a text file is as easy as putting a getline() call still, and you still don't have to deal with parsing an ID.



Keep in mind, these ID formats are just for object types, not multiple instances of them. The instances get their own file:

The Roadkiller 2500
0347356823 (a green american jeep with two automatic turrets that shoot bees and runs on Mr. Pibb)
"This vehicle was built during the desktop wars by Azurecat, who thought he knew what he was doing."

If I wanted to find all objects of type "jeep" I'd just use
while (!EndOfFile)
{
if (object[0] = '03')
{
LoadObjectToBuffer(); //or something like that.
}
NextLine();
}
[/quote]

Looking for instances of objects would be much clearer with strings. To find all objects of type jeep, you would use something like (this is a conceptual example, it won't compile, btw)


for(int i =0; i < array.Length(); i++)
{
if (objectArray.type == jeep)
return objectArray.name;
}


Above I assumed that you are checking through things at runtime. To load things from a file would require a function to do so first, and then place each object in an array. But that's not going to be very difficult to do, and you'd still have to create that function if you use IDs.


For a pick-and-choose generator, would using globals be a bad idea?
bool g_fKeepColor = false;
bool g_fKeepType = false;
bool g_fKeepGuns = false;
etc.

Then I'd call
int GenerateVehicle(sVehicle obj)
{
int i = 4; //for string array access

if (g_fKeepColor == false)
{
obj.ID = rand();
}
i++;

if(g_fKeepType == false)

{
obj.ID = rand();
}
i++;
//and so on.
}


All I have to do is set my globals (which will likely be done with checkboxes or drop down listboxes).
[/quote]

I'm not sure how this code is supposed to work. However, you don't need globals for this. You can make a random vehicle generator function that uses temporary variables to set each property, and then returns a completed random vehicle object. You can pass properties as arguments to the function, in case you don't want the vehicle to be completely random (I'm assuming that this is what the listboxes/checkboxes would be for). You can load these completed objects into an array, and there's no need for you to try and set your own index. If you're trying to set each property of a vehicle as a value an array, you don't need to. The object already will have each variable you just need set it by name (Object.type = RandGen(type)).


I still have quite a bit to figure out, but this is basically how I figured it'd work. You mentioned using a function with multiple parameters to set which values I wanted to keep, where you referring to a constructor? If so I could probably give each object it's own Vehicle.fKeepWhatever variables, with it's own RandomGen() function. It just seems like unnecessary overhead at first glance, but then again I'm new to the OO design end of things. blink.gif
[/quote]

You could overload a constructor for a RandomGen function which can accept arguments for some properties and randomizes the rest. Or you could create a separate function that generates values for each property and then passes those values to a more generic constructor. There are lots of potential approaches, which one you take is a matter of style and taste.

However, all of these approaches will be roughly the same between the result of a parsed ID or a string as far as implementation. But parsing is still going to be a huge pain, and parsing the ID is what will produce unnecessary overhead.

-------R.I.P.-------

Selective Quote

~Too Late - Too Soon~

This topic is closed to new replies.

Advertisement