How to make it efficient :

I have an object that may have N components of different types.
Each type of component has its unique index from 0 to N-1.
Component of each type must be able to address any other component just by index, so the object has an array of N pointers to a component within certain order.
Components must be added dynamically.

When an object is processed, all of its components are processed.
How to do it effectively?
Array has too many zero pointers to make it look effectively to pass through all of it.
Make a list additionally, and then link new component to the closest existing component, or pass through additional array of indexes of existing components, or how?

Edit:
See my next post for details
Last edited on
I am sorry but i need a bit help to understand it better.

So basically you have an Object.
The Object may have different components.
The Object may have an unlimited amount of components.
Each component may have any type.
Each component has it's own ID (or index).
Each component can address all other components by index.
Componenty may be added dynamically.

Do i understand that correctly?
Are the types of the components known at compile-time?
Can an object have the same component twice?

When an object is processed, all of its components are processed.

What do you mean by processed?
What happens when processing?
How do you process an object?


Do you allready have some code we can help you to improve?
Last edited on
Let me explain all since the beginning :O
I have not much code yet, this is about 70% in my mind. Some ideas I got writing just now.)

1. Component class
An abstract class that represents an element of some doubly linked list, so called processing list
Has a pointer to previous element, a pointer to next element, a destructor that links previous and next elements together and proc() virtual method that is empty

2. Processing list
A list of components that can be processed (have proc() method), placed according to certain Pattern
Used for World processing

3. Object component
A child of Component class with certain fields and proc() method
All the childs except the Object form object component base and each has its own unique id, all from 0 to OBJECT_COMPONENT_BASE_COUNT

4. Pattern
An array of OBJECT_COMPONENT_BASE_COUNT (for object components) / STANDARD_OBJECT_BASE_COUNT (for standard objects) size that maps an id of a component to its place index of some processing list (as different lists may have different patterns)
All the patterns form pattern base and each has its own unique id, all from 0 to PATTERN_BASE_COUNT

5. Array of placements
An array of components that maps a place index to the last component of this place index of some processing list
Returns 0 if there is no component of this place index

6. Placement
A couple of certain pattern and an array of placements
Used for adding Components in right order

7. Object class : Component
Represents a component of the World
Has a placement for its component and may have a placement for its sub-objects (such objects and sub-objects like a window and a button inside the window, or a ship and some passengers on board, that are all fully functioning objects)
proc() method goes through all existing components calling their proc() methods

8. World class : Object

The thing I am asking for is that
I use a list for 'processing',
and an 'array of placements' for adding components

What you think?

Feel free to help me to improve my terminology )
Last edited on
The only answer I can come up with is this:
- Does it work?
(Yes
- fine.)
No
- not so fine.

My whole Idea of your Idea got completely crushed ^^
Can you please still try to answer my questions in my first post?
Just having to think about 1 object than a world with objects is a lot easier ^^


Now questions to your List and to your Array:
Of what type are they?
So i understand you can make Arrays and Lists with templates, but the type of each Node would have to be the same that way...
How do you manage it to work with various Component Types?
What kind of thing are these components you talk about, can you give an example? (something like mass, position?)

The rest of that doesn't get to my mind at the moment, I'll answer tomorrow
Last edited on
All the types of component are known at compile time
Objects can have the same component twice
Processing means calling proc() method :O

I am making a game

Example of processing list:

WORLD -> WORLD : background color component (render) -> WINDOW 1 -> BUTTON 1 -> BUTTON 1 : click ability component -> BUTTON 1 : image component (render) -> BUTTON 1 : title component (render) -> BUTTON 2 -> BUTTON 2 : click ability component -> ... -> WINDOW 2 -> ... -> UNIT 1 : position -> UNIT 1 : healthbar component (render) -> UNIT 1 : move ability component -> UNIT 1 : order list -> UNIT 1 : AI control component -> etc

Actually, only Components perform something essential, Objects just call all of them and move them together when the Object is moved somewhere. It may happen when 1 standard object transforms into another, with different place index. (For example, a unit becomes flying, so it is rendered over ground units)

Everything of processing list is Component type
When Component is processed it executes certain method that is written in child class
When Object is processed it executes certain method that is processing every Component of this object

Processing is done through list ('processing list'), but creating through array ('array of placements')

Edit:
I think now, I can get rid of 'patterns'
My project is too simple for this
Last edited on
What if I do it like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    class Component { //abstract
        Component * prev;
        Component * next;

        protected:
            virtual void proc() = 0;
            virtual ~Component() {
                prev->next = next;
                if (next != 0)
                    next->prev = prev;
            };

        public:
            void run() {
                proc();
                if (next != 0)
                    next->run();
            };
            void burn() {
                if (next != 0)
                    next->burn();
                delete this;
            };
    };

    class Object : private Component {
        Component * first_component;
        Component ** last_component_by_id;

        Object(int type_id) {
            //some code
        };
        void proc() {
            first_component->run();
        };
        ~Object() {
            first_component->burn();
        };
    };

Is it ok?
(For example, that self-deleting?)

Edit:
OMG, will rework all the concept tomorrow. This is shit.
Last edited on
Topic archived. No new replies allowed.