0
Follow
0
View

Representing a game world, alternative to dynamic casting?

dashan0098 注册会员
2023-01-25 07:46

From the comments you've gotten so far, you can tell that at least some people think you should use separate arrays, and your argument for why it's one array doesn't really make sense, but sure...

The basic problem is that Items just flat out don't share a set of actions with Players, and the things you would do to each are different as well.

But if you insist on putting them into the same array, then you're going to need to detect what it is and end up with a typecast. Or do some polymorphism. For instance:

class Entity {
...
    virtual void doSomeItemAction() {}
    virtual void doSomePlayerAction() {}
}

class Item: public Entity {
...
     void doSomeItemAction() override { ... }
}

et cetera.

Frankly, I wouldn't do it this way. I'd use separate vectors. But your two choices are detect type and typecast or do weird polymorphism that doesn't really fit your problem space, but you could make it work.

About the Author

Question Info

Publish Time
2023-01-25 07:46
Update Time
2023-01-25 07:46

Related Question