Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Linking Objects to Classes

Author
Message
cope
16
Years of Service
User Offline
Joined: 26th Oct 2008
Location:
Posted: 27th Oct 2008 06:02
OK, here's my dilemma.

I'm attempting to create a class and link it directly to an object.

For example in the game I'm creating I spawn a lazer model every time the player hits space. With this lazer model I want to attach the player ID of the player that fired so when I do collision detection I know easily which player fired it so I can credit the kill properly. Also I want to know which player the model that gets hit belongs to, which should be similar since I could just store playerID on that object also.

My goal is quick access to this data without having to scan n objects searching for one that is connected to the object in question. One messy solution would be a massive array and utilizing the objectID to index an array if objects that are holding extra object data. I don't like this solution because of the wasted space and finite size. However infinite constructs like a linked list would require me to cycle through n/2 objects looking for a match, which I want to avoid. Being fairly new to C++ (I've been programming for over 10 years, I'm just not as experienced in C++) I was hoping someone would know of a cleaner way of doing this.

Thanks for looking at this.
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 27th Oct 2008 14:58
Well, you could have a member variable by reference of the player type and just pass the player object as a parameter. Alternatively, you could have an std::vector as a member variable for the player, of the lazer type, and handle it from there.

sydbod
16
Years of Service
User Offline
Joined: 14th Jun 2008
Location: Just look at the picture
Posted: 27th Oct 2008 15:27
I have been having serious thoughts about this area of code functionality also lately.

Here are just a few things to think about:

Quote: "Also I want to know which player the model that gets hit belongs to, which should be similar since I could just store playerID on that object also."

There is no reason why an Object_ID and a Player_ID could not have the same numeric value so that no look up would be required ..... or ... have then numerically offset eg: Player_ID 1 to 10 are Objects_ID 101 to 110 .

When you are interested in speed then you may also want to consider things like Cache misses within the CPU Cache memory.
If you have every entity (player, object type) in their own separate arrays of data structures, then you have a much higher chance of minimizing Cache misses, and therefor maximizing processing speed.

Quote: "My goal is quick access to this data without having to scan n objects searching for one that is connected to the object in question."

If you have a lazer object that has its data stored in a data structure within an array for all lazer objects, then the following happens.
The lazer object is checked for collision.
If a collision happens you have the ID of the object that gets collided.
You know what the player ID is becaused you have a fixed relationship between Player_ID and Object_ID so there is no searching through any tables for this data.
You know the Player_ID that launched the lazer object as this value is in the particular laser object data structure, so again there is no searching through any tables for this data.
You can process the damage to a player, and process the credits to the shooter without doing any table searching at all.

Also you may want to consider that with a predefined table of data structures, there is no creation time or clean up time for the data that is put into these tables. This makes things very fast.
cope
16
Years of Service
User Offline
Joined: 26th Oct 2008
Location:
Posted: 27th Oct 2008 18:03
OK I've implemented it in arrays, just as an example to clarify a bit.

First a simple definition..



Later in the code, when the player hits the fire button it spawns two lazers, and the assigns which player owns the models to the array. I've put in some dummy values for simplicity.



This is why in my case playerID = objectID wouldn't work. There's already 3 objects associated with the same playerID, and I'm pretty much guaranteed to have more than this when all is said and done.

Then in my collision code I've done the following, which gives me the playerID linked with the lazer. With further development I can make it link the other modelID (rightLazerImpact) to it's player ID as well and then credit the sources and receivers of the impact properly.



I've been looking into vectors, but I can't seem to find a good tutorial or documentation on how to insert into the middle of a vector. (like in the example above, inserting directly into the 21st position) Getting the objects at a certain position with a vector seems easy enough though using the vector.at() function.

Also I would rather have an array of custom classes instead of integers, but this was just a quick example.

So I guess my final question is.. "What C++ data structure should I use to accomplish the above functionality, but have it also be resizeable?" Are vectors the answer or should I be looking elsewhere? I like the array because it's indexable, but having a finite size makes me afraid I'll overflow it eventually.

I'm going to cut this off now because it's getting way too long, thanks for all of the insight so far guys.

Login to post a reply

Server time is: 2024-11-25 05:25:03
Your offset time is: 2024-11-25 05:25:03